在非数字(关键字)字段上进行存储桶排序

时间:2019-10-17 10:44:22

标签: elasticsearch elasticsearch-aggregation

我有产品索引,每个产品都有categorycategory_owner。我想在我的GUI表上显示按category_owner排序的所有类别,但是该表必须是可分页的。

我知道有一种使用bucket_sort对聚合进行排序(和分页)的方法。例子

GET products/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "unique_categories": {
      "terms": {
        "field": "category",
        "size": 1000
      },
      "aggs": {
        "inner": {
          "avg": {
            "field": "price"
          }
        },
        "soring_agg": {
          "bucket_sort": {
            "sort": [
              {
                "inner": {"order": "asc"}
              }
            ],
            "from": 1,
            "size": 1
          }
        }
      }
    }
  }
}

但是有什么方法可以对非数字字段进行排序?例如,我可以创建大小为1的term聚合或top_hists搜索

      "aggs": {
        "inner": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "@timestamp": {"order": "desc"}
              }
            ],
            "_source": {
              "includes": ["category_owner"]
            }
          }
        },
        "soring_agg": {
          "bucket_sort": {
            "sort": [
              {
                "inner": {"order": "asc"}
              }
            ]
          }
        }
      }

Bu我遇到了错误buckets_path must reference either a number value or a single value numeric metric aggregation, got: [InternalTopHits] at aggregation [inner]

terms聚合也不起作用

        "inner": {
          "terms": {
            "field": "category_owner",
            "size": 1
          }
        },

buckets_path must reference either a number value or a single value numeric metric aggregation, got: [StringTerms] at aggregation [inner]

是否可以根据所有者分类的独特类别创建分页?

编辑: 我可以为每个所有者创建所有uniqe类别,但我必须在我的应用程序中手动对其进行排序和分页:(

  "aggs": {
    "owners": {
      "terms": {
        "field": "category_owner",
        "size": 100000
      },
      "aggs": {
        "categories": {
          "terms": {
            "field": "category",
            "size": 1000
          }
        }
      }
    }
  }

0 个答案:

没有答案