# Terms query not returning documents if string has spaces

**URL:** <https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584>\
**Category:** Elasticsearch\
**Created:** [September 27, 2016, 9:24am UTC](https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584 "2016-09-27T09:24:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RamSailendraVarma](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@RamSailendraVarma](https://discuss.elastic.co/u/RamSailendraVarma)\
**Post date:** [September 27, 2016, 9:24am UTC](https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584/1 "2016-09-27T09:24:54Z")

</div>

Hi,

I have a scenario where i need to get documents related to multiple keywords.  
here's the query:

{  
"query":{  
"filtered": {  
"query" : {  
"constant\_score" : {  
"filter" : {  
"bool" : {  
"should" : [  
{ "terms" : {"allTags" : ["woodland shoes"]}}  
]  
}  
}  
}  
},  
"filter" : {  
"bool" : {  
"should" : [  
{ "terms" : {"allTags" : ["amazon", "snapdeal"]}}  
]  
}  
}  
}  
}  
}

in the mapping for allTags i have used my own analyzer with white space tokenizer also still  
this doesn't return any documents, however

{  
"query":{  
"filtered": {  
"query" : {  
"constant\_score" : {  
"filter" : {  
"bool" : {  
"should" : [  
{ "terms" : {"allTags" : ["woodlandshoes"]}}  
]  
}  
}  
}  
},  
"filter" : {  
"bool" : {  
"should" : [  
{ "terms" : {"allTags" : ["amazon","snapdeal"]}}  
]  
}  
}  
}  
}  
}

returns documents.

Can any explain me why its behaving differently.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 27, 2016, 2:54pm UTC](https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584/2 "2016-09-27T14:54:04Z")

</div>

This is due to the fact that the `terms` aggregation does not apply analysis. You need to use the `match` query if you want analysis to be applied.

---

<div class="post-metadata">

**Author:** ![ctvisani](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@ctvisani](https://discuss.elastic.co/u/ctvisani)\
**Post date:** [September 28, 2016, 12:54pm UTC](https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584/3 "2016-09-28T12:54:23Z")

</div>

You can also use terms query with custom analyzer e.g. keyword.

---

<div class="post-metadata">

**Author:** ![RamSailendraVarma](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@RamSailendraVarma](https://discuss.elastic.co/u/RamSailendraVarma)\
**Post date:** [September 29, 2016, 3:59am UTC](https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584/4 "2016-09-29T03:59:25Z")

</div>

Hi,

```
thanks @jpountz for the reply. i have used match query and its working
```

---

<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 5, 2017, 10:16pm UTC](https://discuss.elastic.co/t/terms-query-not-returning-documents-if-string-has-spaces/61584/5 "2017-07-05T22:16:20Z")

</div>


