# Filter by terms in an array

**URL:** <https://discuss.elastic.co/t/filter-by-terms-in-an-array/13154>\
**Category:** Elasticsearch\
**Created:** [August 9, 2013, 5:24pm UTC](https://discuss.elastic.co/t/filter-by-terms-in-an-array/13154 "2013-08-09T17:24:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pletcher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pletcher/32/2172_2.png) [@Pletcher](https://discuss.elastic.co/u/Pletcher)\
**Post date:** [August 9, 2013, 5:24pm UTC](https://discuss.elastic.co/t/filter-by-terms-in-an-array/13154/1 "2013-08-09T17:24:54Z")

</div>

Hi all,

I'm having trouble getting the syntax right for filtering based on the  
terms that are contained in an array. I have documents that look like this:

```
     {
        "_index": "category",
        "_type": "category",
        "_id": "16",
        "_score": 1,
        "_source": {
           "id": 16,
           "label": "Musical Toys",
           "small_label": "musical-toys",
           "active": 1,
           "cat_id": "1234_1234_1234_1234",
           "display_ages": [
              "Baby",
              "Preschool & Toddler"
           ],
           "display_age_ids": [
              1,
              2
           ],
           "display_ages_big_label": [
              "Baby",
              "Preschool & Toddler"
           ],
           "display_ages_small_label": [
              "baby",
              "preschool-and-toddler"
           ],
           "sort_order": null
        }
     }

```

And I am trying to filter based on the strings in  
\_source.display\_ages\_small\_label; but the following filter returns no hits:

{  
"filter":  
{  
"terms":  
{  
"display\_ages\_small\_label": ["baby"]  
}  
}  
}

Based on my understanding of the documentation, I'd expect this filter to  
return the above document and any other documents with "baby" in  
display\_ages\_small\_label--which is exactly what I'd want to happen. What am  
I doing wrong, and how should I fix this?

Thanks!

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Pletcher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pletcher/32/2172_2.png) [@Pletcher](https://discuss.elastic.co/u/Pletcher)\
**Post date:** [August 9, 2013, 5:32pm UTC](https://discuss.elastic.co/t/filter-by-terms-in-an-array/13154/2 "2013-08-09T17:32:03Z")

</div>

I think I might have figured out the problem. The above filter works, but  
when I filter on "preschool-and-toddler", the query fails, presumably due  
to unescaped dashes. Now I'm having trouble figuring out how best to escape  
them. (This is probably also causing a problem when I index the data, so  
I'll have to go back and fix that as well.)

Any suggestions?

Thanks again!

On Friday, August 9, 2013 10:24:54 AM UTC-7, Charles Pletcher wrote:

> Hi all,
> 
> I'm having trouble getting the syntax right for filtering based on the  
> terms that are contained in an array. I have documents that look like this:
> 
> ```
> {
> "_index": "category",
> "_type": "category",
> "_id": "16",
> "_score": 1,
> "_source": {
> "id": 16,
> "label": "Musical Toys",
> "small_label": "musical-toys",
> "active": 1,
> "cat_id": "1234_1234_1234_1234",
> "display_ages": [
> "Baby",
> "Preschool & Toddler"
> ],
> "display_age_ids": [
> 1,
> 2
> ],
> "display_ages_big_label": [
> "Baby",
> "Preschool & Toddler"
> ],
> "display_ages_small_label": [
> "baby",
> "preschool-and-toddler"
> ],
> "sort_order": null
> }
> }
> 
> ```
> 
> And I am trying to filter based on the strings in  
> \_source.display\_ages\_small\_label; but the following filter returns no hits:
> 
> {  
> "filter":  
> {  
> "terms":  
> {  
> "display\_ages\_small\_label": ["baby"]  
> }  
> }  
> }
> 
> Based on my understanding of the documentation, I'd expect this filter to  
> return the above document and any other documents with "baby" in  
> display\_ages\_small\_label--which is exactly what I'd want to happen. What am  
> I doing wrong, and how should I fix this?
> 
> Thanks!

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Pletcher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pletcher/32/2172_2.png) [@Pletcher](https://discuss.elastic.co/u/Pletcher)\
**Post date:** [August 9, 2013, 5:39pm UTC](https://discuss.elastic.co/t/filter-by-terms-in-an-array/13154/3 "2013-08-09T17:39:13Z")

</div>

Sorry to keep replying to myself, but maybe someone will have a similar  
problem in the future.

I realized that I didn't actually need to search in the array, just to  
filter by known terms, so I switched its index property to "not\_analyzed"  
and now everything works as expected.

On Friday, August 9, 2013 10:32:03 AM UTC-7, Charles Pletcher wrote:

> I think I might have figured out the problem. The above filter works, but  
> when I filter on "preschool-and-toddler", the query fails, presumably due  
> to unescaped dashes. Now I'm having trouble figuring out how best to escape  
> them. (This is probably also causing a problem when I index the data, so  
> I'll have to go back and fix that as well.)
> 
> Any suggestions?
> 
> Thanks again!
> 
> On Friday, August 9, 2013 10:24:54 AM UTC-7, Charles Pletcher wrote:
> 
> > Hi all,
> > 
> > I'm having trouble getting the syntax right for filtering based on the  
> > terms that are contained in an array. I have documents that look like this:
> > 
> > ```
> > {
> > "_index": "category",
> > "_type": "category",
> > "_id": "16",
> > "_score": 1,
> > "_source": {
> > "id": 16,
> > "label": "Musical Toys",
> > "small_label": "musical-toys",
> > "active": 1,
> > "cat_id": "1234_1234_1234_1234",
> > "display_ages": [
> > "Baby",
> > "Preschool & Toddler"
> > ],
> > "display_age_ids": [
> > 1,
> > 2
> > ],
> > "display_ages_big_label": [
> > "Baby",
> > "Preschool & Toddler"
> > ],
> > "display_ages_small_label": [
> > "baby",
> > "preschool-and-toddler"
> > ],
> > "sort_order": null
> > }
> > }
> > 
> > ```
> > 
> > And I am trying to filter based on the strings in  
> > \_source.display\_ages\_small\_label; but the following filter returns no hits:
> > 
> > {  
> > "filter":  
> > {  
> > "terms":  
> > {  
> > "display\_ages\_small\_label": ["baby"]  
> > }  
> > }  
> > }
> > 
> > Based on my understanding of the documentation, I'd expect this filter to  
> > return the above document and any other documents with "baby" in  
> > display\_ages\_small\_label--which is exactly what I'd want to happen. What am  
> > I doing wrong, and how should I fix this?
> > 
> > Thanks!

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:21am UTC](https://discuss.elastic.co/t/filter-by-terms-in-an-array/13154/4 "2017-07-06T02:21:49Z")

</div>


