ElasticSearch: restrict results but not facet counts


(Felix Schwarz) #1

I'm struggling writing an ElasticSearch query with facet counts. This is very
likely a simple question but I'm confused by all the facet filters and filter
facets.

The query itself needs two conditions:

  • a condition which restricts the document set, excluded documents should
    not be used for the facet count ("only display documents for a specific
    user").
  • a condition which restricts the result set but excluded documents should
    be part of the facet count ("user filters by tag but should see other tags
    in the facet count")

Example documents:
{"user": "admin", "tag": "baz"}
{"user": "editor", "tag": "foo"}
{"user": "editor", "tag": "bar"}
( curl -XPUT 'http://localhost:9200/blog/post/1' -d '...' )

query:
{
"query" : {
"constant_score": {
"filter": {"term": {"user": "editor"} }
}
},
"facets" : {
"tag" : { "terms" : {"field" : "tag"} }
}
}

The result of this query is ok, I see a facet count for tags foo+bar correctly.

Now I'd like to extend the query so the results only display documents with
tag "foo" BUT keep the facet count as before (so tag "bar" should also appear
in the facet count).

If I modify the constant_score filter like this:
"filter": {
"and": [{"term": {"user": "editor"}}, {"term": {"tag": "foo"}}]
}

Then obviously only tag "foo" will appear in the facet count so I need to add
the {"term": {"tag": "foo"}} filter somewhere else.

How do I do that in ElasticSearch?

Felix

--
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/5371CAA8.5070801%40oss.schwarz.eu.
For more options, visit https://groups.google.com/d/optout.


(David Pilato) #2

Hey Felix,

You need to add your second filter as a "post_filter":

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html#search-request-post-filter

HTH

--
David Pilato | Technical Advocate | Elasticsearch.com
@dadoonet | @elasticsearchfr

Le 13 mai 2014 à 10:43:15, Felix Schwarz (felix.schwarz@oss.schwarz.eu) a écrit:

I'm struggling writing an ElasticSearch query with facet counts. This is very
likely a simple question but I'm confused by all the facet filters and filter
facets.

The query itself needs two conditions:

  • a condition which restricts the document set, excluded documents should
    not be used for the facet count ("only display documents for a specific
    user").
  • a condition which restricts the result set but excluded documents should
    be part of the facet count ("user filters by tag but should see other tags
    in the facet count")

Example documents:
{"user": "admin", "tag": "baz"}
{"user": "editor", "tag": "foo"}
{"user": "editor", "tag": "bar"}
( curl -XPUT 'http://localhost:9200/blog/post/1' -d '...' )

query:
{
"query" : {
"constant_score": {
"filter": {"term": {"user": "editor"} }
}
},
"facets" : {
"tag" : { "terms" : {"field" : "tag"} }
}
}

The result of this query is ok, I see a facet count for tags foo+bar correctly.

Now I'd like to extend the query so the results only display documents with
tag "foo" BUT keep the facet count as before (so tag "bar" should also appear
in the facet count).

If I modify the constant_score filter like this:
"filter": {
"and": [{"term": {"user": "editor"}}, {"term": {"tag": "foo"}}]
}

Then obviously only tag "foo" will appear in the facet count so I need to add
the {"term": {"tag": "foo"}} filter somewhere else.

How do I do that in ElasticSearch?

Felix

--
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/5371CAA8.5070801%40oss.schwarz.eu.
For more options, visit https://groups.google.com/d/optout.

--
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/etPan.5371e7e1.189a769b.a994%40MacBook-Air-de-David.local.
For more options, visit https://groups.google.com/d/optout.


(Felix Schwarz) #3

Am 13.05.2014 11:37, schrieb David Pilato:

You need to add your second filter as a "post_filter":
(...)

Thank you very much, that did it. (I noticed that just "filter" would work as
well in my case).

Felix

--
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/5373247C.6000907%40oss.schwarz.eu.
For more options, visit https://groups.google.com/d/optout.


(system) #4