2015-12-02 123 views
-1

我要建立在ElasticSearch搜索,但我卡住這個:分析儀 '&' 和 '和'

查詢爲:

  • H和M
  • ^h &中號
  • ^h &中號

需要找到一個文件與此變量值:

  • ^h &中號

如何處理呢?

回答

0

您應該使用Pattern Replace Char Filter並將其附加到您的分析儀。

舉例來說,這將是最小的再現:

POST /hm 
{ 
    "index": { 
    "analysis": { 
     "char_filter": { 
     "my_pattern": { 
      "type": "pattern_replace", 
      "pattern": "(\\s+)?&(\\s+)?|(\\s+)?and(\\s+)?", 
      "replacement": "and" 
     } 
     }, 
     "analyzer": { 
     "custom_with_char_filter": { 
      "tokenizer": "standard", 
      "char_filter": [ 
      "my_pattern" 
      ] 
     } 
     } 
    } 
    } 
} 

它將取代&and可選多個空格周圍and。所以,現在你可以檢查該分析儀的工作原理通過運行這些語句:

GET /hm/_analyze?analyzer=custom_with_char_filter&text=h%26m 
GET /hm/_analyze?analyzer=custom_with_char_filter&text=h %26 m 
GET /hm/_analyze?analyzer=custom_with_char_filter&text=handm 

所有這些帶回很同理:

{ 
    "tokens": [ 
    { 
     "token": "handm", 
     "start_offset": 0, 
     "end_offset": 5, 
     "type": "<ALPHANUM>", 
     "position": 1 
    } 
    ] 
} 

這意味着只要你搜索任何這些:

  • HandM
  • H和M
  • ħ&中號
  • ^h &中號

它會帶來同樣的結果。

+0

感謝您的快速回復!附加問題:我無法使用搜索分析器,因爲我使用了模糊搜索(H&M〜),並且它們在默認情況下不進行分析。所以我需要創建一個索引分析器?或者你看到其他的可能性? –

+0

我認爲你仍然可以使用我給出的,然後使用[match](https://www.elastic.co/guide/en/elasticsearch/guide/current/fuzzy-match-query.html) fuzziness'。你怎麼看?匹配查詢可以使用分析字段。 –