ElasticSearch 多字段聚合和 Top 10 问答统计

Aggregate by multiple fields and Top 10 questions and answers count in ElasticSearcch

当我们执行少数记录的记录时,只会出现问题,不会显示每个问题的答案在数据库中,我们有所有问题的答案。请让我知道查询以获得前 10 条记录的问题和答案。下面是错误。

GET logstash-sdc-questionrecords/_search?q=来源:website_portal

{
  "aggs": {
    "genres": {
      "terms": {
        "field": "question.keyword",
        "order": {
          "_count": "desc"
        },
        "size": 10
      },
      "aggs": {
        "genres": {
          "terms": {
            "field": "answer.keyword",
            "order": {
              "_count": "desc"
            },
            "size": 10
          }
        }
      }
    }
  }
}

enter image description here

我从你的问题中了解到,你需要显示前 10 个问题及其答案,为了实现你需要使用子聚合和 top-hits will solve your use-case,而目前你正在使用两个不同的顶级术语聚合。

您的搜索查询应如下所示

{
  "aggs": {
    "genres": {
      "terms": {
        "field": "question.keyword",
        "size": 10
      },
      "aggs": {
        "top_answer_hits": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": [
                "answer"
              ]
            }
          }
        }
      }
    }
  }
}