elasticsearch - 在 Elasticsearch 中,为什么我的查询字符串过度匹配?
问题描述
我插入我的数据如下:
PUT my_index2/doc/1
{
"key": "party",
"str_val": "THE TWITTER INC"
}
PUT my_index2/doc/2
{
"key": "party",
"str_val": "twitter inc"
}
这个查询:
POST my_index2/_search
{
"query": {
"query_string": {
"default_field": "str_val",
"query": "*twitter*"
}
}
}
正确返回两个结果。如果我将查询稍微更改为:
POST my_index2/_search
{
"query": {
"query_string": {
"default_field": "str_val",
"query": "*the twitter*"
}
}
}
我只期待一个结果,因为*the twitter*
应该只匹配“THE TWITTER INC”而不是“twitter inc”。但是会返回两个结果。
为什么我的搜索返回的匹配项太多?
解决方案
那是因为default_operator
for query_string是OR
. 您始终可以使用explain:true
来了解结果是如何返回的。如下所示:
POST my_index2/_search
{
"query": {
"query_string": {
"default_field": "str_val",
"query": "*the twitter*"
}
},
"explain": true
}
对于您的情况,您需要明确提及AND
仅返回两个术语匹配的文档,即the
ANDtwitter
POST my_index2/_search
{
"query": {
"query_string": {
"default_field": "str_val",
"query": "*the twitter*",
"default_operator": "AND"
}
}
}
推荐阅读
- angular - Angular - 用于同一组件中的两个元素时,ngx剪贴板抛出错误
- flutter - 取消飞镖中的请求
- python - 没有数据库的烧瓶登录
- android-studio - 如何获取当前连接的 wi-fi 网络的 SSID 和 PASSWORD
- sql - 如何在 SQL 中存储 Json 查询?
- azure - Docker:你可以拥有的 build-arg 的数量有限制吗?
- websocket - 房间里的 Socket Ids 不稳定
- python - 使用验证集测试模型的泛化能力
- python - Python:如何使用 openCV 修复图像中的杂乱区域?
- c# - 在 C# 和 ASP.NET MVC 中上传大文件