0

As a newcomer to OpenSearch, I am exploring it for my business needs. Specifically, I am searching for a search engine that enables grouping of search results based on their fields (e.g. country attribute, and grouping based on which country).

I noticed that Elastic Search supports this functionality either through collapse (ref 1) or aggregations (ref 2). Thus, I am interested to learn whether OpenSearch offers a similar feature. If you could also give an example of a query accomplishing this outcome that would be great. Thank you.

ref 1: https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html ref 2: How to get latest values for each group with an Elasticsearch query?

Bonus question: for each grouping, what could be the maximum number of results shown? Would there be a way to show unlimited results?

1 Answers1

0

Have you looked at Bucket aggregations in OpenSearch yet?

For example, you can use the terms aggregation to group search results based on a specific field, such as "country". Here's a basic example query (that you can customize to suit your specific needs):

GET index_name/_search
{
  "size": 0,
  "aggs": {
    "group_by_country": {
      "terms": {
        "field": "country",
        "size": 10
      }
    }
  }
}

The "size" parameter is set to 0, which means we don't want to retrieve any hits, only the aggregation results.

The result of this query will be a list of buckets, one for each unique value of the "country" field, along with the count of documents in each bucket.

The "size" parameter in the terms aggregation is set to 10, which means that the top 10 buckets will be returned based on the number of documents in each bucket.

From the documentation: "By default, OpenSearch does not generate more than 10,000 buckets. You can change this behavior by using the size attribute, but keep in mind that the performance might suffer for very wide queries consisting of thousands of buckets."

So, you can adjust the "size" parameter in the terms aggregation to any value that suits your needs, depending on the number of unique values in the "country" field and how many buckets you want to display. Just keep in mind that larger values may impact performance and increase the response time of the query.

Seasers
  • 466
  • 2
  • 7