elasticsearch,bucket-sort,Sorting,elasticsearch,Bucket Sort" /> elasticsearch,bucket-sort,Sorting,elasticsearch,Bucket Sort" />

Sorting ElasticSearch-在子聚集上排序

Sorting ElasticSearch-在子聚集上排序,sorting,elasticsearch,bucket-sort,Sorting,elasticsearch,Bucket Sort,我是elasticsearch的新手,我正在尝试按子集合进行排序。也就是说,我的结果应该首先根据子聚合进行排序。我已经尝试了很多方法来实现这类功能,但都不起作用。有人能帮忙吗 { "aggs": { "distinct_part": { "terms": { "field": "part", "size": 1000

我是elasticsearch的新手,我正在尝试按子集合进行排序。也就是说,我的结果应该首先根据子聚合进行排序。我已经尝试了很多方法来实现这类功能,但都不起作用。有人能帮忙吗

{
  "aggs": {
    "distinct_part": {
      "terms": {
        "field": "part",
        "size": 1000
      }
    },
      "aggs": {
    "distinct_manufacturer": {
      "terms": {
        "field": "manufacturer",
        "size": 1000
      }
    }
  }
}

我试图根据制造商进行排序,我的整个结果应该根据制造商进行排序吗?有人能告诉我如何做到这一点吗?

我试着用您的查询在本地进行测试。如果我能很好地理解你的问题,我做了一个小小的更正。我在索引“子分类”中摄取了以下数据:

注:零件和制造商均映射为文本

GET subsorting/_search
{
  "size": 0,
  "aggs": {
    "distinct_part": {
      "terms": {
        "field": "part.keyword",
        "size": 1000
      },
      "aggs": {
        "distinct_manufacturer": {
          "terms": {
            "field": "manufacturer.keyword",
            "order": {
              "_key": "asc"
            }, 
            "size": 1000
          }
        }
      }
    }
  }
}
如果“零件”和“制造商”字段都映射为关键字,则从查询中删除“.keywords”

如果按升序排序,上述查询的响应如下:

"aggregations" : {
  "distinct_part" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
      {
        "key" : "motor",
        "doc_count" : 4,
        "distinct_manufacturer" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "brandA",
              "doc_count" : 2
            },
            {
              "key" : "brandB",
              "doc_count" : 1
            },
            {
              "key" : "brandC",
              "doc_count" : 1
            }
          ]
        }
      },
      {
        "key" : "car",
        "doc_count" : 3,
        "distinct_manufacturer" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "brandA",
              "doc_count" : 1
            },
            {
              "key" : "brandB",
              "doc_count" : 1
            },
            {
              "key" : "brandC",
              "doc_count" : 1
            }
          ]
        }
      }
    ]
  }
}
如果您需要按降序排列结果,以下是响应,其中
“\u key:“desc”

链接:

你能展示一下你的尝试吗?
"aggregations" : {
  "distinct_part" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
      {
        "key" : "motor",
        "doc_count" : 4,
        "distinct_manufacturer" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "brandA",
              "doc_count" : 2
            },
            {
              "key" : "brandB",
              "doc_count" : 1
            },
            {
              "key" : "brandC",
              "doc_count" : 1
            }
          ]
        }
      },
      {
        "key" : "car",
        "doc_count" : 3,
        "distinct_manufacturer" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "brandA",
              "doc_count" : 1
            },
            {
              "key" : "brandB",
              "doc_count" : 1
            },
            {
              "key" : "brandC",
              "doc_count" : 1
            }
          ]
        }
      }
    ]
  }
}
"aggregations" : {
  "distinct_part" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
      {
        "key" : "motor",
        "doc_count" : 4,
        "distinct_manufacturer" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "brandC",
              "doc_count" : 1
            },
            {
              "key" : "brandB",
              "doc_count" : 1
            },
            {
              "key" : "brandA",
              "doc_count" : 2
            }
          ]
        }
      },
      {
        "key" : "car",
        "doc_count" : 3,
        "distinct_manufacturer" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "brandC",
              "doc_count" : 1
            },
            {
              "key" : "brandB",
              "doc_count" : 1
            },
            {
              "key" : "brandA",
              "doc_count" : 1
            }
          ]
        }
      }
    ]
  }
}