【发布时间】:2020-09-18 21:35:37
【问题描述】:
我正在使用 Elasticsearch 7.7 和 python elasticsearch_dsl 包版本 7.0.0 创建一个简单的搜索引擎。我正在使用 simple_query_string 搜索,因为我想启用最常见的搜索功能(布尔运算符、短语搜索),而不必自己解析查询。除了词组匹配功能外,这在很大程度上运行良好。
如果查询中存在词组匹配,我想确保所有结果都包含词组匹配。例如。 google 的工作原理 - 如果我搜索 "green eggs" and ham,将没有不包含“绿鸡蛋”的结果。
假设我的索引中有 3 个文档:
{
"question":"I love my phrase",
"background: "dont you"
},
{
"question":"I love my phrase",
"background: "and other terms"
},
{
"question":"I have other terms",
"background: "and more"
}
我现在看到的:
正如预期的那样,以下查询仅返回前两个文档,其中一个字段中包含“我的短语”。
{
'simple_query_string':
{
'query': '"my phrase"',
'fields': ['question', 'background']
}
}
与我的预期相反,以下查询将返回所有 3 个结果,其中第 3 个结果高于第 1 个。
{
'simple_query_string':
{
'query': '"my phrase" other terms',
'fields': ['question', 'background']
}
}
如何更改我的查询,以便搜索“我的短语”其他词条不会返回第 3 个文档,因为它不包含短语搜索,但因为它包含额外的搜索,所以将第 2 个文档的评分高于第 1 个文档短语之外的术语?
我尝试过但没有奏效的方法:
'query': '"my phrase" AND (other terms)''query': '"my phrase" AND other terms'
谢谢
【问题讨论】:
标签: elasticsearch elasticsearch-dsl