【问题标题】:How to add multiple Fields in the Bucket sort Elastic search query如何在桶排序弹性搜索查询中添加多个字段
【发布时间】:2021-05-11 23:13:10
【问题描述】:

下面是查询。在此查询中,我需要再添加一个字段 category.keyword。 我发现很难添加一个额外的字段。请修改我的查询并在查询中添加类别关键字字段。

我的要求是显示问题列表和带有计数的类别列表。

{
  "size": 0,
  "aggs": {
    "genres": {
      "terms": {
        "field": "question.keyword",
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "bucket_truncate": {
          "bucket_sort": {
            "from": 0,
            "size": 10
          }
        }
      }
    }
  }
}

索引映射详细信息

  "mapping": {
    "properties": {
      "answer": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "category": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "id": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "question": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "relavence_score": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "source": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
}
      },
      "timestamp": {
        "type": "long"
      },
      "id": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-dsl


    【解决方案1】:

    您可以在同一级别拥有多个聚合,在您的情况下为 bucket_truncatecategories

    {
      "size": 0,
      "aggs": {
        "questions": {
          "terms": {
            "field": "question.keyword",
            "order": {
              "_count": "desc"
            }
          },
          "aggs": {
            "categories": {
              "terms": {
                "field": "category.keyword",
                "order": {
                  "_count": "desc"
                }
              }
            },
            "bucket_truncate": {
              "bucket_sort": {
                "from": 0,
                "size": 10
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 嗨@Evaldas Buinauskas,非常感谢您的快速回复。您的查询分别给我输出问题和类别。实际上我的要求是我需要一起显示问题和类别..
    • 你能澄清说一起是什么意思吗? @Prabhudas8703
    • 表示除了问题,我们还需要显示类别。例如:"key" : "Is jumper good?", "doc_count" : 23, "genres" : { " doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "common questions", "doc_count" : 23 } ] } }
    • @Prabhudas8703 现在怎么样?
    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2018-11-03
    • 2020-02-21
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多