# Using Facet filter in array

**URL:** <https://discuss.elastic.co/t/using-facet-filter-in-array/10212>\
**Category:** Elasticsearch\
**Created:** [January 2, 2013, 8:38am UTC](https://discuss.elastic.co/t/using-facet-filter-in-array/10212 "2013-01-02T08:38:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)\
**Post date:** [January 2, 2013, 8:38am UTC](https://discuss.elastic.co/t/using-facet-filter-in-array/10212/1 "2013-01-02T08:38:42Z")

</div>

I have an elasticsearch document named "user", which contains an array  
field - "companies". I want to get all the company names that contains  
"xyz" in their name. I have set the custom mapping which tokenizes the data  
in the companies array.  
Below is the mapping defined for companies array:

field :companies, type: 'string', index: 'analyzed', analyzer:  
'name\_analyzer' do  
field :companies\_lower, type: 'string', index: 'analyzed', analyzer:  
'lower\_keyword'  
field :companies\_untouched, type: 'string', index: 'not\_analyzed'  
end

(_name\_analyzer_ is the custom analyzer defined by me)

I have these two ES documents:

{"companies":["xyzz","abc"]}  
{"companies":["abcd","abcde"]}

Now, when I perform this query:

{"query"=\>{"match\_all"=\>{}},  
"facets"=\>  
{"tags"=\>  
{"terms"=\>{"field"=\>"companies\_untouched"},  
"facet\_filter"=\>{"term"=\>{"companies"=\>"xyz"}}}}}

This is the result that I get:

{"tags"=\>  
{"\_type"=\>"terms",  
"missing"=\>0,  
"total"=\>3,  
"other"=\>0,  
"terms"=\>  
[{"term"=\>"xyzz", "count"=\>1},  
{"term"=\>"abc", "count"=\>1}]}}

But I need only the matching result like below:

{"tags"=\>  
{"\_type"=\>"terms",  
"missing"=\>0,  
"total"=\>3,  
"other"=\>0,  
"terms"=\>  
[{"term"=\>"xyzz", "count"=\>1}]}}

## i.e the results should only contain the array values that are matching my search query. Is there something wrong that I am doing here?

Thanks,  
Aash

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 2, 2013, 1:07pm UTC](https://discuss.elastic.co/t/using-facet-filter-in-array/10212/2 "2013-01-02T13:07:12Z")

</div>

You can filter out unwanted facet values using "Regex Patterns" or "Term  
Scripts"  
see [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)  
for more information.

On Wednesday, January 2, 2013 3:38:42 AM UTC-5, geeky\_sh wrote:

> I have an elasticsearch document named "user", which contains an array  
> field - "companies". I want to get all the company names that contains  
> "xyz" in their name. I have set the custom mapping which tokenizes the data  
> in the companies array.  
> Below is the mapping defined for companies array:
> 
> field :companies, type: 'string', index: 'analyzed', analyzer:  
> 'name\_analyzer' do  
> field :companies\_lower, type: 'string', index: 'analyzed', analyzer:  
> 'lower\_keyword'  
> field :companies\_untouched, type: 'string', index: 'not\_analyzed'  
> end
> 
> (_name\_analyzer_ is the custom analyzer defined by me)
> 
> I have these two ES documents:
> 
> {"companies":["xyzz","abc"]}  
> {"companies":["abcd","abcde"]}
> 
> Now, when I perform this query:
> 
> {"query"=\>{"match\_all"=\>{}},  
> "facets"=\>  
> {"tags"=\>  
> {"terms"=\>{"field"=\>"companies\_untouched"},  
> "facet\_filter"=\>{"term"=\>{"companies"=\>"xyz"}}}}}
> 
> This is the result that I get:
> 
> {"tags"=\>  
> {"\_type"=\>"terms",  
> "missing"=\>0,  
> "total"=\>3,  
> "other"=\>0,  
> "terms"=\>  
> [{"term"=\>"xyzz", "count"=\>1},  
> {"term"=\>"abc", "count"=\>1}]}}
> 
> But I need only the matching result like below:
> 
> {"tags"=\>  
> {"\_type"=\>"terms",  
> "missing"=\>0,  
> "total"=\>3,  
> "other"=\>0,  
> "terms"=\>  
> [{"term"=\>"xyzz", "count"=\>1}]}}
> 
> ## i.e the results should only contain the array values that are matching my search query. Is there something wrong that I am doing here?
> 
> Thanks,  
> Aash

--

---

<div class="post-metadata">

**Author:** ![revdev](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@revdev](https://discuss.elastic.co/u/revdev)\
**Post date:** [January 2, 2013, 6:45pm UTC](https://discuss.elastic.co/t/using-facet-filter-in-array/10212/3 "2013-01-02T18:45:01Z")

</div>

Also look into "Nested Type" and "Nested  
Queries" [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
but note that each array element is stored as a separate document and hence  
will take more storage.

On Wednesday, January 2, 2013 12:38:42 AM UTC-8, geeky\_sh wrote:

> I have an elasticsearch document named "user", which contains an array  
> field - "companies". I want to get all the company names that contains  
> "xyz" in their name. I have set the custom mapping which tokenizes the data  
> in the companies array.  
> Below is the mapping defined for companies array:
> 
> field :companies, type: 'string', index: 'analyzed', analyzer:  
> 'name\_analyzer' do  
> field :companies\_lower, type: 'string', index: 'analyzed', analyzer:  
> 'lower\_keyword'  
> field :companies\_untouched, type: 'string', index: 'not\_analyzed'  
> end
> 
> (_name\_analyzer_ is the custom analyzer defined by me)
> 
> I have these two ES documents:
> 
> {"companies":["xyzz","abc"]}  
> {"companies":["abcd","abcde"]}
> 
> Now, when I perform this query:
> 
> {"query"=\>{"match\_all"=\>{}},  
> "facets"=\>  
> {"tags"=\>  
> {"terms"=\>{"field"=\>"companies\_untouched"},  
> "facet\_filter"=\>{"term"=\>{"companies"=\>"xyz"}}}}}
> 
> This is the result that I get:
> 
> {"tags"=\>  
> {"\_type"=\>"terms",  
> "missing"=\>0,  
> "total"=\>3,  
> "other"=\>0,  
> "terms"=\>  
> [{"term"=\>"xyzz", "count"=\>1},  
> {"term"=\>"abc", "count"=\>1}]}}
> 
> But I need only the matching result like below:
> 
> {"tags"=\>  
> {"\_type"=\>"terms",  
> "missing"=\>0,  
> "total"=\>3,  
> "other"=\>0,  
> "terms"=\>  
> [{"term"=\>"xyzz", "count"=\>1}]}}
> 
> ## i.e the results should only contain the array values that are matching my search query. Is there something wrong that I am doing here?
> 
> Thanks,  
> Aash

--

---

<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, 2:58am UTC](https://discuss.elastic.co/t/using-facet-filter-in-array/10212/4 "2017-07-06T02:58:02Z")

</div>


