Elasticsearch:获取bool查询中不匹配的should元素的报告

时间:2015-03-16 23:19:47

标签: elasticsearch

我正在寻找一种获取无法比拟的查询报告并显示它的方法。 例如,我有两个用户对象

用户1:

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

用户2:

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

当我执行bool查询时,匹配规则必须在查询中且子规则在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,但似乎没有为该用例提供结果,并且解析起来似乎很复杂。

1 个答案:

答案 0 :(得分:1)

您需要使用named queries。 使用相同的,创建一个像下面这样的bool查询 -

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

在结果部分中,它将告知哪些命名查询匹配。 您可以使用此信息来制作您要查找的统计数据。