RangeFilter on Nested/Child Facet Counts


(Benji Smith) #1

Imagine I have three different types of data stored in my ElasticSearch
index: users, posts, and replies. Each post belongs to a particular user.
Each reply belongs to a particular user and post.

I'd like to write a query to find all users with a particular number of
posts (e.g., find all users with exactly one post, or all users with
between ten and twenty posts).

Along those same lines, I'd like to find all users with posts having a
particular number of replies. Basically, I'd like to filter documents based
on facet counts, which seems like a pretty reasonable thing to do, but I
can't figure out any way to do it.

I've tried to model this problem with embedded objects, nested objects, and
parent/child objects, but the query DSL doesn't seem to support these kinds
of queries, no matter how I model the data.

Any advice?

Benji Smith

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/055da68c-8641-4592-a0f3-a8f82ded29f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #2

If you can denormalize, you should be able to do script filters (query or
aggregations, whichever you need). It's not gonna be the fastest thing in
the world but consider this example:

POST http://localhost:9200/user/doc
{ "name": "user1", "posts": ["1"] }

POST http://localhost:9200/user/doc
{ "name": "user2", "posts": ["1", "2"] }

POST http://localhost:9200/user/doc
{ "name": "user3", "posts": ["1", "2", "3"] }

Then this aggregation:

POST http://localhost:9200/user/_search
{
"size": 0,
"aggs": {
"exactly_one_post": {
"filter": {
"script": {
"script": "doc['posts'].values.size() == 1"
}
}
},
"one_or_more_posts": {
"filter": {
"script": {
"script": "doc['posts'].values.size() >= 1"
}
}
},
"between_one_and_two_posts": {
"filter": {
"script": {
"script": "doc['posts'].values.size() >= 1 &&
doc['posts'].values.size() <= 2"
}
}
}
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7619b100-f19e-41af-aa66-df46aa9785b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #3