elasticsearch - 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 查询。
解决方案
该match_bool_prefix
查询已专门创建用于该search_as_you_type
字段(请参阅#35600)。
由于您正在搜索电子邮件前缀(或整个电子邮件),因此您可以简单地使用prefix
查询,因为电子邮件地址永远不会由多个术语组成,特别是因为您正在使用分uax_url_email
词器对其进行分析。match_bool_prefix
所以在这种情况下使用查询是没有意义的。
推荐阅读
- automation - Testack white 找不到窗口的孩子
- python - dtaidistance的distance_matrix_fast函数很慢
- reactjs - 使用 react-router-dom 在子组件中路由
- java - Unity Google Play 排行榜错误:java.lang.ClassNotFoundException
- javascript - 如何使用 createElement 修复 adapt.min.js 文件?当站点使用 SSL 时产生错误
- jquery - 谷歌应用脚本数据返回为未定义
- r - 在图中寻找最短路径的“无法到达某些顶点”问题
- python - 我找不到 TypeError 的任何解决方案:'bool' 类型的对象没有 len() 错误
- mysql - MySQL将多个值拆分为多行
- c - C中三个整数之间的比较