elasticsearchelasticsearch-2.0

ElasticSearch search, get unique categories of returned products


In an eshop with thousands of products we have a searchbar at the top. The expected output of the search is a list of categories in which there are products matching the query.

For example searching for 'iphone' should return a list of categories where there are products with that keyword. e.g. - Mobile phones - Batteries for phones - Case for phones - etc.

What I did is search through the products index for the keyword, then get the results, pluck the category_id of each product, remove duplicates and do a /_mget in the categories index with the ids I should display.

This however seems to be inneffient since the first search might return 10k results (if it is too generic) which I then loop through to get its category_id.

I am looking for better ways to do the above.

Any ideas on how to make the above more effiecient?


Solution

  • Take a look into Elasticsearch Aggregations. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html

    A good place to start would be with a Terms Aggregation which is a bucket aggregation https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html.

    An example:

    GET /_search
    {
        "query": {...},
        "aggs" : {
            "categories" : {
                "terms" : { "field" : "category_name" }
            }
        }
    }
    

    The response should look something like this where it puts the field value and a count into buckets.

    {
        ...
        "aggregations" : {
            "categories" : {
                "doc_count_error_upper_bound": 0, 
                "sum_other_doc_count": 0, 
                "buckets" : [ 
                    {
                        "key" : "Mobile phones",
                        "doc_count" : 6
                    },
                    {
                        "key" : "Batteries for phones",
                        "doc_count" : 3
                    },
                    {
                        "key" : "Cases for phones",
                        "doc_count" : 2
                    }
                ]
            }
        }
    }