【问题标题】:Requiring Phrase Matches in Elasticsearch SimpleStringQuery在 Elasticsearch SimpleStringQuery 中要求短语匹配
【发布时间】: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


    【解决方案1】:

    与我的预期相反,以下查询将返回所有 3 个结果

    默认情况下,查询中的单词与OR 运算符组合:参见simple_query_stringdocumentationdefault_operator 参数的说明。您的第二个查询被解释为 "my phrase" OR other OR terms,因此它将返回所有 3 个结果:每个文档至少包含一个术语 "my phrase"otherterms

    如何更改我的查询,以便搜索“我的短语”其他词条不会返回第 3 个文档,因为它不包含短语搜索,但因为它包含额外的搜索,所以将第 2 个文档评分高于第 1 个文档短语之外的术语?

    AFAIK,simple_query_string 搜索无法做到这一点。您可以尝试使用query_string 搜索,它具有名为boolean operators 的功能。使用该功能,您可以编写提供所需结果的查询:

    {
      "query": {
        "query_string": {
          "query": "+\"my phrase\" other terms",
          "fields": ["question", "background"]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多