Elasticsearch:获取布尔查询中不匹配的应该元素的报告

Elasticsearch: Get report of unmatched should elements in a bool query

我正在寻找一种方法来获取不匹配的应该查询的报告并显示它。 例如我有两个用户对象

用户 1:

{
"username": "user1"
"docType": "user"
"level": "Professor"
"discipline": "Sciences"
"sub-discipline": "Mathematical"
}

用户 2:

{
"username": "user1"
"docType": "user"
"level": "Professor"
"discipline": "Sciences"
"subDiscipline": "Physics"
}

当我执行 bool 查询时,匹配学科在 must 查询中,子学科在 should 查询中

bool:
  must: [{
    term: { "doc.docType": "user" }
  },{
    term: { "doc.level": "professor" }
  },{
    term: { "doc.discipline": "sciences" }
  }],
  should: [{
    term: { "subDiscipline": "physics" }
  }]

如何在我的结果中得到不匹配的元素:

Result 1: user1 match 100%
Result 2: user2 match 70% (unmatch subdiscipine "physics")

我查看了 explainApi,但似乎没有为该用例提供结果,而且解析起来似乎非常复杂。

您将需要为此使用 named queries。 使用相同的方法,创建如下所示的 bool 查询 -

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "SourceName": {
              "query": "CNN",
              "_name": "sourceMatch"
            }
          }
        },
        {
          "match": {
            "author": {
              "query": "qbox.io",
              "_name": "author"
            }
          }
        }
      ]
    }
  }
}

在结果部分,它会告诉哪些所有命名查询匹配。 您可以使用此信息来编造您正在寻找的统计数据。