我们如何在弹性搜索查询中使很少的标记成为短语

How can we make few tokens to be phrase in elastic search query

我想搜索被视为短语的部分查询。例如我要搜索 "Can you show me documents for Hospitality and Airline Industry" 在这里,我希望航空业被视为 phrase.I 在 multi_match 中找不到任何此类设置。 即使我们尝试使用 "Can you show me documents for Hospitality and \"Airline Industry\"" 来使用 multi_match 查询。默认分析器将其分解为单独的 tokens.I 不想更改我发现的 analyser.Also 的设置我们可以在 simple_query_string 中执行此操作,但这会导致我们无法像在 multi_match 布尔查询中那样应用过滤器选项,因为我也想在某些字段上应用过滤器。

search_text="Can you show me documents for Hospitality and Airline Industry" 现在我想将 Airline Industry 作为短语传递,以针对 2 个字段搜索我的索引文档。 好吧,假设我有这样的现有代码。

If filter:
qry={
    “query":{
        “bool”:{
            “must”:{
                "multi_match":{
                "query":search_text,
                "type":"best_fields",
                "fields":["TITLE1","TEXT"],
                "tie_breaker":0.3,
                }
            },
            “filter”:{“terms”:{“GRP_CD”:[“1234”,”5678”]     }
    }
    }

else:
qry={

    "query":{
        "multi_match":{
        "query":search_text',
        "type":"best_fields",
        "fields":["TITLE1",TEXT"],
        "tie_breaker":0.3
        }
    }
}

'但后来我意识到这段代码没有将 Airline Industry 作为一个短语处理,即使我正在传递这样的搜索字符串 "Can you show me documents for Hospitality and \"航空业\""

根据弹性搜索文档,我了解到这个查询可能会处理这个问题

qry={"query":{
"simple_query_string":{
"query":"Can you show me documents for Hospitality and \"Airline Industry\"",
"fields":["TITLE1","TEXT"] }
} }

但现在我的问题是,如果用户想要应用过滤器怎么办……使用上述过滤器查询我无法传递短语,并且布尔查询无法使用 simple_query_string'

您始终可以使用 boolean query 组合查询。让我们逐个了解这个案例。在进入案例之前,我想澄清一件事,那就是关于过滤器。布尔查询的过滤子句的行为就像一个 must 子句,但不同之处在于过滤子句内的任何查询(甚至是另一个带有 must/should 子句的布尔查询)都有过滤上下文。过滤上下文意味着,那部分查询将不会被考虑用于分数计算。

现在让我们继续讨论案例:

案例 1:只有查询没有过滤器。

{
  "query": {
    "bool": {
      "must": [
        {
          "simple_query_string": {
            "query": "Can you show me documents for Hospitality and \"Airline Industry\"",
            "fields": [
              "TITLE1",
              "TEXT"
            ]
          }
        }
      ]
    }
  }
}

请注意查询与您在问题中指定的相同。我在这里所做的只是将它包装在 bool 查询中。这不会对查询进行任何逻辑更改,但这样做将使以编程方式向过滤器子句添加查询变得更加容易。

案例二:带过滤器的词组查询。

{
  "query": {
    "bool": {
      "must": [
        {
          "simple_query_string": {
            "query": "Can you show me documents for Hospitality and \"Airline Industry\"",
            "fields": [
              "TITLE1",
              "TEXT"
            ]
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "GRP_CD": [
              "1234",
              "5678"
            ]
          }
        }
      ]
    }
  }
}

这样您就可以将查询(查询上下文)与过滤器结合起来。