Histogram
A histogram is a type of OpenSearch aggregation that groups different items in a particular range bucket based on values provided by the user. They differ from a typical range aggregation because Histograms do not require the definition of every specific interval explicitly. Users can set the maximum and minimum values and define the intervals for each bucket. Histograms are essentially an easier way to define the ranges.
For example, in a price-based histogram, users can set the minimum value at $10, the maximum value at $1000, and set the interval value at 10. In this scenario, 100 buckets will be created within the ranges of 10-20, 20-30, and so on.
The API call of a histogram is:
POST https://api.upstartcommerce.com/v1/search/aggregation
To understand the concept of Histogram better, consider the following example code block. In it an example histogram for the property of "price" is created:
Notice that the type field correlates NoChannel Search aggregations called "Managed Aggregation". Metadata for histograms is generated after its creation.
The following is the explanation of the important parameters involved in the code block above.
- field: This value defines the property on which the buckets will be created. In this example, the value is set to "price". This means that the buckets of items will be created on the basis of their prices.
- interval: This value defines the interval size of each bucket. For example, it is set to 10 in the above example. This means that a bucket will be created from 1 to 10, 11 to 20, and so on.
- min_doc_count: This field controls the total number of documents retrieved in a specific interval. In the example above the value is set to 1. This means that the retrieved document should contain at least 1 product that falls in the range.
- extended_bounds: In this block, users set the max and min values for the ranges. In the example, the minimum value is 1 and the maximum is at 100.
Remember that Histogram can round-off the values of item properties to the nearest number. This helps each product fit in the nearest bucket. For example, if you set the interval value at 5 on the property of prices ranging from 1 to 20, all items that cost $12 will be placed in the bucket with value 10 automatically.