How to match a single word from a search query in Elasticsearch

落花浮王杯 提交于 2019-12-13 00:28:51

问题


Lets say I have a field called complexId and it has a value with special characters: fruit/1a.445/2.10.mango. So when I search for something like Complex ID fruit/1a.445/2.10.mango, I should be able to get the result.

What I want is, I want elastic search to match at least one among these 3 words:

1. Complex 
2. ID 
3. fruit/1a.445/2.10.mango

eg:- Below are my two documents:

{
   "name": "Joe",
   "complexId": "fruit/1a.445/2.10.mango"
},
{
   "name": "Matt",
   "complexId": "car/35.6a5/chevy.20"
}

The user searches for something like blah blah blah fruit/1a445/mango blah blah and he should get the below result:

{
   "name": "Matt",
   "complexId": "fruit/1a.445/2.10.mango"
}

I tried this but it fails to give me any result:

{
   "query": {
      "bool": {
          "match" : 
          {
              complexId : "blah blah blah fruit/1a.445/2.10.mango blah blah"
          }
      }
   }
}

The field complexId has a standard Analyzer.

Any help would be appreciated.


回答1:


A Standard Analyzer would not work in your case.

Since you are having special characters, it will be ignored while storing into ElasticSearch. Try using the whitespace Analyzer instead.

POST _analyze
{
  "analyzer": "whitespace",
  "text": "blah blah blah fruit/1a.445/2.10.mango blah blah"
}

Play around with different Analyzers and find the one that is best suited for your needs.

You could create your own custom Analyzer as well.



来源:https://stackoverflow.com/questions/46261677/how-to-match-a-single-word-from-a-search-query-in-elasticsearch

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