【问题标题】:What type to use for terms aggregation?术语聚合使用什么类型?
【发布时间】:2022-08-05 09:46:08
【问题描述】:

我有一个 Elasticsearch 术语聚合来获取一些字段的唯一值:

    {
      // query removed for brevity
      aggs: {
        provinces: {
          terms: { field: \"shipping_address.province.keyword\" }
        },
        shipping_carriers: {
          terms: { field: \"fulfillments.tracking_company.keyword\" }
        },
        shipping_methods: {
          terms: { field: \"shipping_lines.title.keyword\" }
        }
      }
    }

这导致aggregations 看起来像这样的响应:

{
  \"aggregations\" : {
    \"shipping_carriers\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 0,
      \"buckets\" : [
        {
          \"key\" : \"FedEx\",
          \"doc_count\" : 31
        },
        //...removed for brevity
      ]
    },
    \"provinces\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 8,
      \"buckets\" : [
         //...removed for brevity
      ]
    },
    \"shipping_methods\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 0,
      \"buckets\" : [
         //...removed for brevity
      ]
    },
    \"shipping_codes\" : {
      \"doc_count_error_upper_bound\" : 0,
      \"sum_other_doc_count\" : 0,
      \"buckets\" : [
         //...removed for brevity
      ]
    }
  }
}

我正在使用带有 Typescript 的 Javascript library

我对 Typescript 还是有点陌生​​,我不知道要为 buckets 使用什么类型,所以我可以以 string 的身份访问 key

      // ???: what are the correct types to use here?
      const aggs = results.aggregations as Record<
        string,
        AggregationsTermsAggregateBase<AggregationsCompositeBucketKeys>
      >;
      
      // ???: `any[]` is bad, what are the proper types to use?
      const province_buckets = aggs.provinces.buckets as any[];
      const carrier_buckets = aggs.shipping_carriers.buckets as any[];
      const method_buckets = aggs.shipping_methods.buckets as any[];

      return new OrderFilters({
        provinces: province_buckets.map((bucket) => bucket.key),
        shipping_carriers: carrier_buckets.map((bucket) => bucket.key),
        shipping_methods: method_buckets.map((bucket) => bucket.key)
      });

    标签: typescript elasticsearch


    【解决方案1】:

    我遇到了同样的问题,并在 Elastic repo 中找到了一个 PR - https://github.com/elastic/elasticsearch-js/pull/1596 -> test/unit/api.test.ts

    interface Aggregations {
      unique: T.AggregationsTermsAggregateBase<{ key: string }>
    }
    
    const response = await client.search<Doc, Aggregations>({
        index: 'test',
        allow_no_indices: true,
        query: { match_all: {} },
        aggregations: {
          unique: {
            terms: {
              field: 'foo'
            }
          }
        }
      })
    
      t.equal(response.hits.hits[0]._source?.foo, 'bar')
      t.ok(Array.isArray(response.aggregations?.unique.buckets))
    

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多