编写查询以同时获得前 10 名和排除 & 包含

write the query to get both Top 10 and Exclude & Include

我必须使用 3 个字段(答案、问题;关键字、来源)

  1. 我的要求是我需要排除 Field answer = "UNHANDLED"

  2. 我需要字段question.keyword

  3. 我需要包含 Field source = "sonax"

我使用下面的查询来获取输出。但是在应用字段 answer = "UNHANDLED" 之后,我仍然在数据中得到未处理的记录。

{
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "answer": "UNHANDLED"
        }
      },
      "must": {
        "term": {
          "source": "sonax"
        }
      }
    }
  },
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "question.keyword"
      },
      "aggs": {
        "top_faq_hits": {
          "top_hits": {
            "_source": {
              "includes": [
                "answer"
              ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

此致, 帕布

添加包含索引数据、搜索查询和搜索结果的工作示例

索引数据:

{
  "question": "c",
  "answer": "UNHANDLED",
  "source": "sonax"
}
{
  "question": "b",
  "answer": "c",
  "source": "titan"
}
{
  "question": "b",
  "answer": "q",
  "source": "sonax"
}
{
  "question": "d",
  "answer": "a",
  "source": "volvo"
}

搜索查询:

{
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "answer.keyword": "UNHANDLED"
        }
      },
      "must": {
        "term": {
          "source.keyword": "sonax"
        }
      }
    }
  },
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "question.keyword"
      },
      "aggs": {
        "top_faq_hits": {
          "top_hits": {
            "_source": {
              "includes": [
                "source"
              ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations": {
    "top_tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "b",
          "doc_count": 1,
          "top_faq_hits": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.6931471,
              "hits": [
                {
                  "_index": "65567523",
                  "_type": "_doc",
                  "_id": "3",
                  "_score": 0.6931471,
                  "_source": {
                    "source": "sonax"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }