Elasticsearch:在同一字段上精确匹配多个短语

Elasticsearch: Exact multiple match phrases on same field

我想要以下查询 return 结果,其中由 OR 分隔的精确短语在特定字段中匹配。

{
  "query": {
    "nested": {
      "path": "positions",
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "default_field": "positions.companyname",
                "query": "microsoft OR gartner OR IBM"
              }
            },
            {
              "query_string": {
                "default_field": "positions.title",
                "query": "(Chief Information Security Officer) OR (Chief Digital Officer)"
              }
            }
          ]
        }
      },
      "inner_hits": {
        "highlight": {
          "fields": {
            "positions.title": {}
          }
        }
      }
    }
  }
}

结果应包含确切的首席信息安全官首席数字官

但不是 首席 数字 营销 主管Chief Information Officer 因为目前正在returned.

此外,该字段不一定包含要搜索的确切词组。 例如:

"CIO Chief Information Officer" -> 错误

"Head at Digital - Chief Digital Officer" -> 真

"Former lead Chief Information Security Officer" -> 真

"Chief Information Officer" -> 错误

我想我想表达的意思是这些短语应该始终彼此相邻(接近)。

对于您的用例,我建议您使用 match_phrase query inside a bool query's should 子句。

像这样的东西应该可以工作:

GET Whosebug/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "text": "Chief Information Security Officer"
          }
        },
        {
          "match_phrase": {
            "text": "Chief Digital Officer"
          }
        }
      ]
    }
  }
}

这个查询就可以做到。

{
  "query": {
    "nested": {
      "path": "positions",
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "default_field": "positions.companyname",
                "query": "microsoft OR gartner OR IBM"
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "match_phrase": {
                      "positions.title": "chief information security officer"
                    }
                  },
                  {
                    "match_phrase": {
                      "positions.title": "chief digital officer"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

match_phrase 确保搜索的是确切的短语。要匹配同一字段上的多个短语,请使用 bool 运算符和 should 条件。