Alec
Alec

Reputation: 1496

ElasticSearch. How to get counts for several ranges in one query?

Currently I am getting count for a range of values via this query:

$ curl -XGET 'http://localhost:9200/myindex/mytype/_count' -d '{
    range:{myfield:{gt:"start_val",lt:"end_val"}}
}
'

Now I have several ranges, and need counts for each range. Can I get them with one query, rather then re-querying each time?

I looked into multi-search with search_type=count But probably it's not the right approach to follow... (it gave me just some aggregated count rather than grouping by... looks like I misused it)

EDIT: I've found that range facet would have been amazing, but unfortunately my values are neither numbers, nor dates, they're just strings...

EDIT2: This is what I ended up with, based on the accepted answer:

$ curl -XGET 'http://localhost:9200/myindex/mytype/_search?search_type=count' -d '{
    facets : {
        range1 : {
            filter : {
                range:{myfield:{gt:"start_val1",lt:"end_val1"}}
            }
        },
        range2 : {
            filter : {
                range:{myfield:{gt:"start_val2",lt:"end_val2"}}
            }
        }
    }
}
'

Upvotes: 0

Views: 1539

Answers (1)

DeH
DeH

Reputation: 571

Heres a link where the creator of ES gives a solution (i.e : several filter facets)

solution

So, one filter facet per range should work alright.

Here's the link toward the doc :

doc api

hope it helps

Upvotes: 1

Related Questions