partial matching from middle in completion suggestion elasticsearch

天涯浪子 提交于 2021-01-27 18:09:47

问题


I have a field named search_suggest having the below

search_suggest: {
   type: "completion",
   analyzer: "simple",
   payloads: true,
   preserve_separators: false,
   preserve_position_increments: false,
  max_input_length: 50
}

It has values indexed as

{
  input: [
   "apple iphone 6"
  ],
  output: "apple iphone 6",
  weight: 5,
  payload: {
   category: "mobiles"
  }
}

If I searched for apple ,It is giving me results. But If I search for iphone it is not giving me any results.

Is there any way in completion suggester to do this?. Do i have to index input as

  • apple iphone 6
  • iphone 6
  • 6

I am aware of edge-ngram suggester. But the cons is it will suggest duplicates also.

Please help.


回答1:


If anyone still looking for answers,

Completion suggester is suitable for prefix matches. So, in input, you can provide the possible suffixes of your phrase. This will allow you to do prefix searches even if you start from the middle, aka sub string searches.

For example:

{
  "text" : "Courtyard by Marriot Munich City",
  "text_suggest" : {
    "input": [
      "Courtyard by Marriot Munich City",
      "by Marriot Munich City",
      "Marriot Munich City",
      "Munich City",
      "City"
    ],
    "output" : "Courtyard by Marriot Munich City",
    "weight" : 11,
    "payload": { "id" : 314159 }
  }
}

As you can see, wherever you start within "Courtyard by Marriot Munich City" you will get results. (Except may be for "by" because in most cases it will be discarded as a stop word).

Going up to 4-5 steps are well enough for general search strings. Also, if you handle stop-words with a filter, no need to worry about stop words in input.

Sample index analyzer

{
  "settings" : {
    "analysis" : {
      "filter" : {
        "suggester_stop" : {
          "type" : "stop",
          "stopwords" : "_english_",
          "remove_trailing" : false,
          "ignore_case" : true
        },
        "suggester_stemmer" : {
          "type" : "stemmer",
          "name" : "light_english"
        }
      },
      "analyzer" : {
        "suggester_analyzer" : {
          "type" : "custom",
          "tokenizer" : "standard",
          "char_filter" : ["html_strip"],
          "filter" : [
            "standard",
            "lowercase",
            "suggester_stop",
            "suggester_stemmer"
          ]
        }
      }
    }
  }
}

This will solve the problem you mentioned in one of your comments:

Then if I suggest for "apple ip", It won't give result. How about iphone 6?

{
  "text_suggest" : {
    "input": [
      "apple iphone 6",
      "iphone 6"
    ],
    "output" : "apple iphone 6",
    "weight" : 11
  }
}

You will get search results for both "apple ip", "iphone 6" etc. However you will not get result for "apple 6" which is not that common for people to search anyway.




回答2:


This should solve your problem

{
 "input": [ "apple", "iphone", "6" ]
}


来源:https://stackoverflow.com/questions/38178565/partial-matching-from-middle-in-completion-suggestion-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!