SteveL
SteveL

Reputation: 3389

Elastic Search, limit results size by buckets count instead of documents

I have millions of log documents for about thousand of classifieds, I want to search all of the log entries and put each one of them the the correct bucket where every bucket is a classified(with the unique id of the classified).I know how to limit the number of documents but is there a way to limit the number of buckets instead?

{  
   "size":10 #this limits by the docs length
    "aggregations": {
      "clfds": {
         "terms": {
            "field": "clsfd_id"
         }
      }
    },
   "sort":[  
      {  
         "clsfd_id":{  
            "order":"asc"
         }
      },
   ],
   "query":{  
      "filtered":{  
         "query":{  
            "match_all":{  

            }
         },
         "filter":{  
            "bool":{  
               "should":[  
                    #filled dynamically
               ],
            }
         }
      }
   }
}

Upvotes: 0

Views: 2381

Answers (1)

Or Weinberger
Or Weinberger

Reputation: 7472

I'm not sure if this is what you're asking, but you can simply use the size attribute inside your terms aggregation to limit the number of returned buckets:

{  
   "size":10 #this limits by the docs length
    "aggregations": {
      "clfds": {
         "terms": {
            "size": 50,
            "field": "clsfd_id"
         }
      }
    },
   "sort":[  
      {  
         "clsfd_id":{  
            "order":"asc"
         }
      },
   ],
   "query":{  
      "filtered":{  
         "query":{  
            "match_all":{  

            }
         },
         "filter":{  
            "bool":{  
               "should":[  
                    #filled dynamically
               ],
            }
         }
      }
   }
}

If you would like to see the actual documents under the aggregation bucket, you can use the top_hits aggregation:

{
  "aggs": {
    "clfds": {
      "terms": {
        "field": "clsfd_id",
        "size": 50
      },
      "aggs": {
        "top_clfds_hits": {
          "top_hits": {
            "sort": [
              {
                "clsfd_id": {
                  "order": "asc"
                }
              }
            ],
            "size": 10
          }
        }
      }
    }
  }
}

Upvotes: 2

Related Questions