2

My use case : I have a search bar when the user can type his query. I want to show multiple types of search suggestions to the user in addition to a regular query suggestion. For example, in the screenshot below, as you can see in this screenshot, you can see there are company sector, companies, and schools suggestions.

enter image description here

This is currently implemented using completion suggesters and the following mappings (this is code from our Ruby implementation, but I believe you should be able to understand it easily)

{
  _source: '',
  suggest: {
    text: query_from_the_user, # User query like "sec" to find "security" related matches 
    'school_names': {
      completion: {
        field: 'school_names_suggest',
      },
    },
    'companies': {
      completion: {
        field: 'company_name.suggest',
      },
    },
    'sectors': {
      completion: {
        field: sector_field_based_on_current_language(I18n.locale),
             # uses 'company_sector.french.suggest' when the user browses in french
      },
    },
  },
}

Here are my mappings (this is written in Ruby as but I believe it shouldn't be too hard to mentally convert this to Elasticsearch JSON config

indexes :company_name, type: 'text' do
  indexes :suggest, type: 'completion'
end
indexes :company_sector, type: 'object' do
  indexes :french, type: 'text' do
    indexes :suggest, type: 'completion'
  end
  indexes :english, type: 'text' do
    indexes :suggest, type: 'completion'
  end
end
indexes :school_names_suggest, type: 'completion'
# sample Indexed JSON 
{ 
  company_name: "Christian Dior Couture",
  company_sector: {
    english: 'Milk sector',
    french: 'Secteur laitier'
  },
  school_names_suggest: ['Télécom ParisTech', 'Ecole Centrale Paris']
}

The problem is the suggestion is not powerful enough and cannot autocomplete based on the middle of a sentence and provide additional results even after a perfect match. Here are some scenarios that I need to capture with my ES implementation

CASE 1 - Matching by prefix in the middle of a sentence

# documents
[{ company_name: "Christian Dior Couture" }]
# => A search term "Dior" should return this document because it matches by prefix on the second word

CASE 2 - Provide results even after a perfect match

# documents
[
  { company_name: "Crédit Agricole" },
  { company_name: "Crédit Agricole Pyrénées Gascogne" },
]
# => A search term "Crédit Agricole" should return both documents (using the current implementation it only returns "Crédit Agricole"

Can I implement this using suggesters in Elasticsearch ? Or do I need to fall back to multiple search that would take advantage of the new search-as-you-type data type using a query as mentionned in the doc ?

I am using elasticsearch 7.1 on AWS and the Ruby driver (gem elasticsearch-7.3.0)

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

0 Answers0