Elasticsearch : Search with special character Open & Close parentheses

风流意气都作罢 提交于 2019-12-13 03:47:51

问题


Hi I am trying to search a word which has these characters in it '(' , ')' in elastic search. I am not able to get expected result.

This is the query I am using

{
"query": {
    "query_string" : {
        "default_field" : "name",
        "query" : "\\(Pas\\)ta\""
    }
}}

In the results I am getting records with "PASTORS" , "PAST", "PASCAL", "PASSION" first. I want to get name 'Pizza & (Pas)ta' in the first record in the search result as it is the best match.

Here is the analyzer for the name field in the schema

"analysis": {
  "filter": {
    "autocomplete_filter": {
      "type": "edge_ngram",
      "min_gram": "1",
      "max_gram": "20"
    }
  },
  "analyzer": {
    "autocomplete": {
      "type": "custom",
      "tokenizer": "standard",
      "filter": [
        "lowercase",
        "autocomplete_filter"
      ]
    }
  }

 "name": {
        "analyzer": "autocomplete",
        "search_analyzer": "standard",
        "type": "string"
   },

Please help me to fix this, Thanks


回答1:


You have used standard tokenizer which is removing ( and ) from the tokens generated. Instead of getting token (pas)ta one of the token generated is pasta and hence you are not getting match for (pas)ta.

Instead of using standard tokenizer you can use whitespace tokenizer which will retain all the special characters in the name. Change analyzer definition to below:

  "analyzer": {
    "autocomplete": {
      "type": "custom",
      "tokenizer": "whitespace",
      "filter": [
        "lowercase",
        "autocomplete_filter"
      ]
    }
  }


来源:https://stackoverflow.com/questions/57321426/elasticsearch-search-with-special-character-open-close-parentheses

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