分析仪 '&' 和 '和'

分析仪 '&' 和 '和'

问题描述:

我要建立在ElasticSearch搜索,但我卡住这个:分析仪 '&' 和 '和'

查询为:

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

需要找到一个文件与此变量值:

  • ^h &中号

如何处理呢?

您应该使用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'。你怎么看?匹配查询可以使用分析字段。 –