Facets across several types


(fmpwizard) #1

Hi,

This is what I'm trying to do, taking the example from
http://www.elasticsearch.org/guide/reference/api/search/facets/
Imagine that the Tags are on a separate type, not on the same article
type.
You know which tags go to which article because both article and the
corresponding tags have the same _id value.

Now I can search across both types (in my real app I have 6 types in
my search), by just omitting the type on the query url, but using
facets, I only get values matching the type that returns values.

What I mean is that if the query term is just a tag, the facet will
return just information from the tags type, not from the articles
type. Is there a way to get facet values from "all" types?

In other words, looking at the example from the site, you search for
"T*" and still get facet values for the tags, even though the tags are
not on the Article type.

I hope this was clear.

Thanks

Diego


(Shay Banon) #2

Not sure I understand... . Facets are only aggregated for actual hits.

On Sun, Nov 6, 2011 at 6:15 AM, fmpwizard fmpwizard@gmail.com wrote:

Hi,

This is what I'm trying to do, taking the example from
http://www.elasticsearch.org/guide/reference/api/search/facets/
Imagine that the Tags are on a separate type, not on the same article
type.
You know which tags go to which article because both article and the
corresponding tags have the same _id value.

Now I can search across both types (in my real app I have 6 types in
my search), by just omitting the type on the query url, but using
facets, I only get values matching the type that returns values.

What I mean is that if the query term is just a tag, the facet will
return just information from the tags type, not from the articles
type. Is there a way to get facet values from "all" types?

In other words, looking at the example from the site, you search for
"T*" and still get facet values for the tags, even though the tags are
not on the Article type.

I hope this was clear.

Thanks

Diego


(fmpwizard) #3

Let me try again.

See https://gist.github.com/1349360

The articles and tags have the same _id value because they are
related. Think of then as a one to one relationship from a database
point of view.

I then search for articles that start with T, how can I get a facet
with the tags for the two articles that match my query?

Thanks for taking the time
Diego

On Nov 7, 10:52 pm, Shay Banon kim...@gmail.com wrote:

Not sure I understand... . Facets are only aggregated for actual hits.

On Sun, Nov 6, 2011 at 6:15 AM, fmpwizard fmpwiz...@gmail.com wrote:

Hi,

This is what I'm trying to do, taking the example from
http://www.elasticsearch.org/guide/reference/api/search/facets/
Imagine that the Tags are on a separate type, not on the same article
type.
You know which tags go to which article because both article and the
corresponding tags have the same _id value.

Now I can search across both types (in my real app I have 6 types in
my search), by just omitting the type on the query url, but using
facets, I only get values matching the type that returns values.

What I mean is that if the query term is just a tag, the facet will
return just information from the tags type, not from the articles
type. Is there a way to get facet values from "all" types?

In other words, looking at the example from the site, you search for
"T*" and still get facet values for the tags, even though the tags are
not on the Article type.

I hope this was clear.

Thanks

Diego


(fmpwizard) #4

If I run the regular search like here:

I get this result:

(same gist :slight_smile: )

The facet area is empty, I understand why it is empty, but I was
looking for a way around it, because in my use case, I know how the
articles and tags are related (using the id of each document type)

One way around it is by adding a __id field to each document on
articles and tags that has the same document id. I then do a regular
search not asking for the facets, just the articles with title of T*,
then I parse the _id fields of those results, and query the index
again, but this time looking for the _id values on the manually
updated __id fields (using the default OR operator) and this time
asking for a facet. This works, but I wonder if there is a cleaner
way.

Thanks

Diego

On Nov 8, 9:52 pm, fmpwizard fmpwiz...@gmail.com wrote:

Let me try again.

Seehttps://gist.github.com/1349360

The articles and tags have the same _id value because they are
related. Think of then as a one to one relationship from a database
point of view.

I then search for articles that start with T, how can I get a facet
with the tags for the two articles that match my query?

Thanks for taking the time
Diego

On Nov 7, 10:52 pm, Shay Banon kim...@gmail.com wrote:

Not sure I understand... . Facets are only aggregated for actual hits.

On Sun, Nov 6, 2011 at 6:15 AM,fmpwizardfmpwiz...@gmail.com wrote:

Hi,

This is what I'm trying to do, taking the example from
http://www.elasticsearch.org/guide/reference/api/search/facets/
Imagine that the Tags are on a separate type, not on the same article
type.
You know which tags go to which article because both article and the
corresponding tags have the same _id value.

Now I can search across both types (in my real app I have 6 types in
my search), by just omitting the type on the query url, but using
facets, I only get values matching the type that returns values.

What I mean is that if the query term is just a tag, the facet will
return just information from the tags type, not from the articles
type. Is there a way to get facet values from "all" types?

In other words, looking at the example from the site, you search for
"T*" and still get facet values for the tags, even though the tags are
not on the Article type.

I hope this was clear.

Thanks

Diego


(Clinton Gormley) #5

Hiya

Now I can search across both types (in my real app I have 6 types in
my search), by just omitting the type on the query url, but using
facets, I only get values matching the type that returns values.

What I mean is that if the query term is just a tag, the facet will
return just information from the tags type, not from the articles
type. Is there a way to get facet values from "all" types?

The search API accepts a 'query' and a 'filter'.

The facets are calculated on the results of the 'query'. The top-level
'filter' parameter allows you to post-process the query results, after
the facets are calculated.

So if you run your query across both types (tags and articles), then you
set the filter to

{ "type": { "value": "articles" }}

... then your final result set should just include the articles, but
your facets should take the tags into account

clint


(fmpwizard) #6

Thanks Clint, maybe I didn't fully understand your answer, but I
didn't get it to work.

These are the steps I'm doing:

Create data:


Search using filter:

Desired output:

Thanks

Diego

The search API accepts a 'query' and a 'filter'.

The facets are calculated on the results of the 'query'. The top-level
'filter' parameter allows you to post-process the query results, after
the facets are calculated.

So if you run your query across both types (tags and articles), then you
set the filter to

{ "type": { "value": "articles" }}

... then your final result set should just include the articles, but
your facets should take the tags into account

clint


(Clinton Gormley) #7

Hi Diego

On Wed, 2011-11-09 at 12:20 -0800, fmpwizard wrote:

Thanks Clint, maybe I didn't fully understand your answer, but I
didn't get it to work.

Create data:
https://gist.github.com/1349360
Search using filter:
https://gist.github.com/1352851
Desired output:
https://gist.github.com/1352852

Right. That won't work. The query only matches docs with 't*' in the
_all field, which excludes the tag docs.

This, however, would:

I use a bool clause to include 'articles-with-t*' and all 'tags', run
the facets on the tags, and filter the results to only include the
articles.

But frankly, you'd be better doing this in two separate queries

clint


(fmpwizard) #8

Hi Clint,

This, however, would:

https://gist.github.com/1354663

I use a bool clause to include 'articles-with-t*' and all 'tags', run
the facets on the tags, and filter the results to only include the
articles.

But frankly, you'd be better doing this in two separate queries

Thanks for the example and I'll go ahead and use two separate queries.

Diego

clint


(system) #9