【问题标题】:How to search omitting whitespace on elasticsearch如何在elasticsearch上搜索省略空格
【发布时间】:2019-06-06 00:24:46
【问题描述】:

Elasticsearch 菜鸟在这里试图理解一些东西

我有这个问题

{
  "size": 10,
  "_source": "pokemon.name",
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "multi_match": {
            "_name": "name-match",
            "type": "phrase",
            "fields": ["pokemon.name"],
            "operator": "or",
            "query": "pika"
          }
        },
        {
          "multi_match": {
            "_name": "weight-match",
            "type": "most_fields",
            // I use multi_match because I'm not sure how can I change it to match
            "fields": ["pokemon.weight"],
            "query": "10kg"
          }
        }
      ]
    }
  }
}

问题是pokemon.weight 在值和单位10 Kg 之间有一个空格。所以我需要忽略空格才能匹配10kg

我尝试更改标记器,遗憾的是它可以决定在哪里拆分但不能删除字符。无论如何我不知道如何使用它,文档也不是很有帮助,解释了理论而不是如何使用它。

谢谢!任何学习资源将不胜感激。

【问题讨论】:

    标签: elasticsearch tokenize


    【解决方案1】:

    您需要使用char filter 定义自定义分析器。您将在其中将space 字符替换为empty 字符,以便在您的情况下生成的令牌10g 变为10g。我在本地尝试过,对我来说效果很好。

    用于了解 analysis 在 ES 中如何工作的额外链接以及带有 char filters 的自定义分析器示例。

    下面是我的自定义分析器来实现所需的令牌:-

    {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "standard",
              "char_filter": [
                "my_char_filter"
              ]
            }
          },
          "char_filter": {
            "my_char_filter": {
              "type": "mapping",
              "mappings": [
                "\\u0020=>"
              ]
            }
          }
        }
      }
    }
    

    现在使用相同的分析器,它生成了下面的令牌,我使用analyze api 确认。

    端点:- http://{{your_hostname}}:9500/{{your_index_name}}/_analyzer

    正文:-

    {
        "analyzer" : "my_analyzer",
        "text" : "10 g"
    }
    

    结果:-

    {
        "tokens": [
            {
                "token": "10g",
                "start_offset": 0,
                "end_offset": 4,
                "type": "<ALPHANUM>",
                "position": 0
            }
        ]
    }
    

    【讨论】:

    • 这必须非常小心地使用,并且只能在非常选定的字段上使用,如果您注意到只有一个为长文本生成的标记。
    • @Kamal ,是的,因为它删除了所有空格,感谢您提供主动建议:-)
    • 谢谢!这真的很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多