Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

elasticsearch - Aggregation on top N results

Problem:

If I search for "iphone" I get 400 product results and the product category aggregation I have returns the top 3 categories in the results set.

Those categories would include smartphones, phone cases and mobile phone accessories.

If I search "iphone 6" I get 1400 results because of the extra "6" returns matches to more products. The product category aggregation now returns the top 3 categories for all those results.

The top 3 product categories will now be everything from cables to computer monitors.

What I need to do is get the top 3 categories for the top 100 results.


What I've tried:

I've tried using the top_hits aggregation within the top category aggregation but that only returns the top products in each category.

Something like this:

{
    "aggs": {

        "product_categories": {
            "terms": {
                "field": "product_category",
                "size": 10,
            }
        }        
        "aggs": {
            "top-categories": {
                "top_hits": {
                    "size" : 3
                }
            }
        }
    }
}

I've also tried creating a top_hits aggregation with a sub-aggregation within to get the top categories but that doesn't work either.

{
    "aggs": {
        "top-categories": {
            "top_hits": {
                "size" : 100
            }
            "aggs": {
                "product_categories": {
                    "terms": {
                        "field": "product_category",
                        "size": 3,
                    }
                }
            }
        }
    }
}

Can anyone help me with this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try using a filter aggregation based on a limit filter, and nest your terms aggregation in it.

Be aware that the limit is applied at shard level (see the documentation).

However, this should do the job for your case, with a query like :

{
  "aggs": {
    "limit_results": {
      "filter": {
        "limit": {
          "value": 100
        }
      },
      "aggs": {
        "product_categories": {
          "terms": {
            "field": "product_category",
            "size": 10
          }
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...