首页 > 解决方案 > Elasticsearch match_bool_prefix 查询失败,并在查询结束时出现与号

问题描述

我遇到了一个奇怪的问题,match_bool_prefix(一个术语)查询与前缀查询的工作方式不同。据我了解 match_bool_prefix 应该分析我的查询,然后为每个术语创建一个多术语查询,最后一个应该是前缀查询。就我而言,我的查询是电子邮件地址的一部分,并以 @ 结尾。这是我的例子:

创建索引

curl --location --request PUT 'http://localhost:9200/testindex' \
--header 'Content-Type: application/json' \
--data-raw '{
  "settings":{
    "analysis": {
        "analyzer": {
            "default": {
                "tokenizer": "uax_url_email",
                "filter": ["lowercase"]
            }
        }
    }
  },
    "mappings": {
        "properties": {
            "email":{
                "type":"text"
            }
        }
    }
}
'

添加数据

curl --location --request PUT 'http://localhost:9200/testindex/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email":"tester@gmail.com"
}'

查询失败

curl --location --request POST 'http://localhost:9200/testindex/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "match_bool_prefix": {
            "email": "tester@"
        }
    }
}'

工作查询

curl --location --request POST 'http://localhost:9200/testindex/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "prefix": {
            "email": {
                "value": "tester@"
            }
        }
    }
}'

使用“tester”一词进行查询适用于这两个查询,这表明它使用的是前缀。在 match_bool_prefix 查询上使用另一个分析器(关键字或空格)也可以正常工作。这让我觉得 Elasticsearch 做的不对。根据文档, match_bool_prefix 应该将查询分析为标记,在我的情况下,根据此分析查询将去除 @:

curl --location --request POST 'http://localhost:9200/testindex/_analyze' \
--header 'Content-Type: application/json' \
--data-raw '{
    "explain": "false",
    "analyzer":"default",
    "text" : "tester@"
}
'

results

{
    "tokens": [
        {
            "token": "tester",
            "start_offset": 0,
            "end_offset": 6,
            "type": "<ALPHANUM>",
            "position": 0
        }
    ]
}

所以这就是 match_bool_prefix 查询的实际样子,但这个查询效果很好:

{
  "query": {
    "bool" : {
      "should": [
        { "prefix": { "email": "tester"}}
      ]
    }
  }
}

任何帮助都将不胜感激,因为我正在处理一个更大的查询,但结果让我怀疑我是否正确使用了 match_bool_prefix 查询。

标签: elasticsearch

解决方案


match_bool_prefix查询已专门创建用于该search_as_you_type字段请参阅#35600)。

由于您正在搜索电子邮件前缀(或整个电子邮件),因此您可以简单地使用prefix查询,因为电子邮件地址永远不会由多个术语组成,特别是因为您正在使用分uax_url_email词器对其进行分析。match_bool_prefix所以在这种情况下使用查询是没有意义的。


推荐阅读