aggs clause

更新时间:
复制 MD 格式

Description

A keyword query can return tens of thousands of documents. Users are unlikely to browse all documents to find the information they need. In many cases, users are more interested in statistical information.

Syntax

{
  "aggs" : [
    {
      "group_key": "field",
      "agg_fun" : ["func1", "func2"],
      "agg_filter" : "filter_expression",
      "agg_range" : [number1, number2],
      "max_group" : number,
      "order_by":"count"
    } 
  ]
}
  • group_key: Required. `field` specifies the name of the field to use for statistics. This field must be configured as a property field, and its type must be integer or string.

  • agg_fun: Required. `func` can be one of the following five system functions: `count()`, `sum(id)`, `max(id)`, `min(id)`, or `distinct_count(id)`. These functions calculate the number of documents, the sum of the ID field, the maximum value of the ID field, the minimum value of the ID field, and the approximate distinct count of the ID field, respectively. You can perform statistics using multiple functions at the same time.

  • agg_filter: Optional. Specifies that statistics are calculated only for documents that meet specific conditions. The `agg_filter` parameter is a conditional expression. For more information, see the filter clause.

  • range: Optional. Specifies statistics by segment, which can be used for distribution statistics. Only a single `range` parameter is supported. This parameter calculates statistics for the range from `number1` to `number2` and for values greater than `number2`. Distribution statistics are not supported for string fields.

  • max_group: Optional. The maximum number of groups to return. The default value is 1000.

  • order_by: Optional. Sorts the statistical results. The only valid value for this parameter is `count`. If you do not specify this parameter, the results are sorted by `group_key` in lexicographic order by default.

Examples:

  • Simple statistics example

    {
      "aggs" : [
        {
          "group_key": "group_id",
          "agg_fun" : ["sum(price)"]
    		} 
      ]
    }
    
    Example of a returned result (statistical results only):
    {
      result: {
        facet: [
          {
            key: "group_id",
            items: [
              {
                value: 43,
                sum: 81
              },
              {
                value: 63,
                sum: 91
              }
            ]
          }
        ]
      }
    },

  • Statistics for multiple items

    {
      "aggs" : [
        {
          "group_key": "company_id",
          "agg_fun" : ["sum(id)", "max(id)", "min(id)"]
    		} 
      ]
    }

  • Statistics based on multiple group keys

    {
      "aggs" : [
        {
          "group_key": "group_id",
          "agg_fun" : ["sum(price)"]
    		},
        {
          "group_key": "company_id",
          "agg_fun" : ["count()"]
    		} 
      ]
    }

  • Statistics for data that meets a condition

    # Calculate statistics only for data where price is greater than 100
    {
      "aggs" : [
        {
          "group_key": "group_id",
          "agg_fun" : ["sum(price)"],
          "agg_filter" : "price > 100"
    		} 
      ]
    }

  • Statistics by class

    {
      "aggs" : [
        {
          "group_key": "company_id",
          "agg_fun" : ["distinct_count(brand)"]
    		}
      ]
    }
    Note: If the statistical function is distinct_count, approximate statistics based on the HyperLogLog (HLL) algorithm are enabled. The loss of precision for this type of statistic is typically within 1%.

    Notes

    • Any field used in an `aggs` clause must be configured as a property field in the application schema.

    • The results of the `aggs` clause are returned in the `facet` node of the search results. The name of the value field is the same as the function name in `agg_fun`, such as `sum` or `count`.

    • The statistical information from this clause is returned in the `facet` node. To return and display the `facet` content, you must set the `format` parameter in the `config` clause to `fulljson`.

    • Due to performance limitations of the DPI engine, the `aggs` clause guarantees accurate statistics for up to 100,000 retrieved documents per shard by default. The accuracy of statistics is not guaranteed for results that contain more than 100,000 documents. You can change this threshold by adjusting the cluster configuration.

    • If many groups are returned, for example, if `max_group` is set to a value in the tens of thousands, the QRS worker may consume a large amount of memory and cause an out-of-memory (OOM) error.