# Facets across several types

**URL:** https://discuss.elastic.co/t/facets-across-several-types/5781
**Category:** Elasticsearch
**Created:** [November 6, 2011, 4:15am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781 "2011-11-06T04:15:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![fmpwizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmpwizard/32/2675_2.png) [@fmpwizard](https://discuss.elastic.co/u/fmpwizard)
#### Post date: [November 6, 2011, 4:15am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/1 "2011-11-06T04:15:11Z")

</div>

Hi,

This is what I'm trying to do, taking the example from  
[http://www.elasticsearch.org/guide/reference/api/search/facets/](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

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [November 8, 2011, 3:52am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/2 "2011-11-08T03:52:51Z")

</div>

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

On Sun, Nov 6, 2011 at 6:15 AM, fmpwizard [fmpwizard@gmail.com](mailto:fmpwizard@gmail.com) wrote:

> Hi,
> 
> This is what I'm trying to do, taking the example from  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](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

---

<div class="post-metadata">

### Author: ![fmpwizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmpwizard/32/2675_2.png) [@fmpwizard](https://discuss.elastic.co/u/fmpwizard)
#### Post date: [November 9, 2011, 2:52am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/3 "2011-11-09T02:52:27Z")

</div>

Let me try again.

See [facet across multiple types · GitHub](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](mailto: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](mailto:fmpwiz...@gmail.com) wrote:
> 
> > Hi,
> 
> > This is what I'm trying to do, taking the example from  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](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

---

<div class="post-metadata">

### Author: ![fmpwizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmpwizard/32/2675_2.png) [@fmpwizard](https://discuss.elastic.co/u/fmpwizard)
#### Post date: [November 9, 2011, 5:29am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/4 "2011-11-09T05:29:58Z")

</div>

If I run the regular search like here:

> <https://gist.github.com/fmpwizard/1350503>

I get this result:

> <https://gist.github.com/fmpwizard/1350503>

(same gist 🙂 )

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](mailto: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](mailto: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](mailto:fmpwiz...@gmail.com) wrote:
> > 
> > > Hi,
> 
> > > This is what I'm trying to do, taking the example from  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](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

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [November 9, 2011, 9:06am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/5 "2011-11-09T09:06:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fmpwizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmpwizard/32/2675_2.png) [@fmpwizard](https://discuss.elastic.co/u/fmpwizard)
#### Post date: [November 9, 2011, 8:20pm UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/6 "2011-11-09T20:20:49Z")

</div>

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:

> <https://gist.github.com/fmpwizard/1349360>

Search using filter:

> <https://gist.github.com/fmpwizard/1352851>

Desired output:

> <https://gist.github.com/fmpwizard/1352852>

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

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [November 10, 2011, 11:39am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/7 "2011-11-10T11:39:37Z")

</div>

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:  
> [facet across multiple types · GitHub](https://gist.github.com/1349360)  
> Search using filter:  
> [3rd.sh · GitHub](https://gist.github.com/1352851)  
> Desired output:  
> [desired.json · GitHub](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:

> <https://gist.github.com/clintongormley/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

clint

---

<div class="post-metadata">

### Author: ![fmpwizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmpwizard/32/2675_2.png) [@fmpwizard](https://discuss.elastic.co/u/fmpwizard)
#### Post date: [November 10, 2011, 3:17pm UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/8 "2011-11-10T15:17:21Z")

</div>

Hi Clint,

> This, however, would:
> 
> [gist:1354663 · GitHub](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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 3:49am UTC](https://discuss.elastic.co/t/facets-across-several-types/5781/9 "2017-07-06T03:49:09Z")

</div>


