【问题标题】:Elastic Search doesn't respect size when using aggregations使用聚合时,Elastic Search 不考虑大小
【发布时间】:2019-12-12 15:24:01
【问题描述】:

我是 Elastic Search 的新手,如果答案很明显,请见谅。

我已修改查询以使用 aggs 来显示“不同”的结果。但是,在添加 aggs 之后,size 似乎不再起作用 - 无论我将大小设置为多少,它总是返回 10 个结果。

有人知道我如何同时使用 aggs 和 size 吗?

我的查询是:

{
    "size": "15",
    "from": "0",
    "query": {
      "bool": {
        "filter": [
          {
            "term": {
              "category": "Cars"
            }
          },
          {
            "term": {
              "location": "Sydney"
            }
          },
          {
            "term": {
              "status": true
            }
          }
        ]
      }
    },
   "sort": [
      {
        "_score": "desc"
      },
      {
        "brand": "asc"
      }
    ],
    "aggs": {
        "brand": {
            "terms": {
                "field": "brand",
                "order": {
                    "price": "asc"
                }
            },
            "aggs": {
                "brand": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "price": {
                                    "order": "asc"
                                }
                            }
                        ]
                    }
                },
                "price": {
                    "min": {
                        "field": "price"
                    }
                }
            }
        }
    }
  }

【问题讨论】:

  • 您在此使用了嵌套聚合,并且您只设置了内部聚合的大小而不设置外部聚合。是不是你总是得到外部聚合结果为 10 因为默认是 10

标签: elasticsearch


【解决方案1】:

你在查询之前提到的size参数,用于设置查询命中的大小,不会影响聚合桶的大小。 就像您在子聚合中提到的那样,在父聚合中使用 size 参数为“size”:1 获得前 10 个 aggs 的修改后查询是:

{
    "size": "15",
    "from": "0",
    "query": {
      "bool": {
        "filter": [
          {
            "term": {
              "category": "Cars"
            }
          },
          {
            "term": {
              "location": "Sydney"
            }
          },
          {
            "term": {
              "status": true
            }
          }
        ]
      }
    },
   "sort": [
      {
        "_score": "desc"
      },
      {
        "brand": "asc"
      }
    ],
    "aggs": {
        "brand": {
            "terms": {
                "field": "brand",
                "size": 10, 
                "order": {
                    "price": "asc"
                }
            },
            "aggs": {
                "brand": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "price": {
                                    "order": "asc"
                                }
                            }
                        ]
                    }
                },
                "price": {
                    "min": {
                        "field": "price"
                    }
                }
            }
        }
    }
  }

希望这会有所帮助。

【讨论】:

  • 谢谢我根据您的回答让它工作了,但需要将最后一个“大小”参数更改为 1 以避免重复。我已经编辑了你的答案。
猜你喜欢
  • 2014-06-29
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 2021-02-04
  • 2015-09-08
相关资源
最近更新 更多