# Query not returning expected result

**URL:** <https://discuss.elastic.co/t/query-not-returning-expected-result/5858>\
**Category:** Elasticsearch\
**Created:** [November 13, 2011, 2:19am UTC](https://discuss.elastic.co/t/query-not-returning-expected-result/5858 "2011-11-13T02:19:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tyler\_Schroeder](https://discuss.elastic.co/u/Tyler_Schroeder)\
**Post date:** [November 13, 2011, 2:19am UTC](https://discuss.elastic.co/t/query-not-returning-expected-result/5858/1 "2011-11-13T02:19:06Z")

</div>

I have the following query:  
{"filtered":{"query":{"query\_string":{"query":"\*"}},"filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"4e669ec4139ad42bb8428031"}}]}}}

Which accurately returns the following result:  
{ id: '4e7d7847a3e2486722000003',  
firstName: 'Tets',  
lastName: 'Test2',  
email: '',  
username: 'vailripper',  
userType: 'member',  
birthdate: '',  
homePhone: '',  
gender: 'Female',  
active: true,  
tags: ['sport-light', 'a test tag'],  
tenant: '4e669ec4139ad42bb8428031' }

However, when I add a a terms filter to filter on the 'a test tag' tag, it  
doesn't return anything:

{"filtered":{"query":{"query\_string":{"query":"\*"}},"filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"4e669ec4139ad42bb8428031"}},{"terms":{"tags":["a  
test tag"]}}]}}}

In addition, if I add just a 'test' tag filter, it returns any tag with  
'test' in its name, I was under the impression that it should only match on  
the entire fields value?

{"filtered":{"query":{"query\_string":{"query":"\*"}},"filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"4e669ec4139ad42bb8428031"}},{"terms":{"tags":["test"]}}]}}}

I Appreciate the Help,  
Tyler

---

<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 13, 2011, 8:50am UTC](https://discuss.elastic.co/t/query-not-returning-expected-result/5858/2 "2011-11-13T08:50:59Z")

</div>

Are tags analyzed? (if you did not explicitly mapped them as not analyzed,  
then they are). If so, then what happens is that they get analyzed, and  
broken down into several terms ("test one" -\> "test", "one"). Terms/term  
filter does not do analysis, so "test one" will not match on anything  
(since there isn't a term called "test one"), but "test" will (but it will  
also match on a tag with "test two"). You can mark it as not analyzed in  
the mappings, or use multi\_field mapping to have it mapped both as analyzed  
and not analyzed.

On Sun, Nov 13, 2011 at 4:19 AM, Tyler Schroeder [vailripper@gmail.com](mailto:vailripper@gmail.com)wrote:

> I have the following query:
> 
> {"filtered":{"query":{"query\_string":{"query":"\*"}},"filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"4e669ec4139ad42bb8428031"}}]}}}
> 
> Which accurately returns the following result:  
> { id: '4e7d7847a3e2486722000003',  
> firstName: 'Tets',  
> lastName: 'Test2',  
> email: '',  
> username: 'vailripper',  
> userType: 'member',  
> birthdate: '',  
> homePhone: '',  
> gender: 'Female',  
> active: true,  
> tags: ['sport-light', 'a test tag'],  
> tenant: '4e669ec4139ad42bb8428031' }
> 
> However, when I add a a terms filter to filter on the 'a test tag' tag, it  
> doesn't return anything:
> 
> {"filtered":{"query":{"query\_string":{"query":"\*"}},"filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"4e669ec4139ad42bb8428031"}},{"terms":{"tags":["a  
> test tag"]}}]}}}
> 
> In addition, if I add just a 'test' tag filter, it returns any tag with  
> 'test' in its name, I was under the impression that it should only match on  
> the entire fields value?
> 
> {"filtered":{"query":{"query\_string":{"query":"\*"}},"filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"4e669ec4139ad42bb8428031"}},{"terms":{"tags":["test"]}}]}}}
> 
> I Appreciate the Help,  
> Tyler

---

<div class="post-metadata">

**Author:** ![Tyler\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tyler\_Schroeder](https://discuss.elastic.co/u/Tyler_Schroeder)\
**Post date:** [November 14, 2011, 12:52am UTC](https://discuss.elastic.co/t/query-not-returning-expected-result/5858/3 "2011-11-14T00:52:07Z")

</div>

Yes, I'm using default the default mappings. In that case, shouldn't a  
query with a ferms filter on tags, with the value of "a test tag" match a  
record which has a tags value of "a test tag"? For example:

{"filtered":{"query":{"query\_string":{"query":"\*"}},"  
filter":{"and":[{"term":{"active":true}},{"term":{"tenant":"  
4e669ec4139ad42bb8428031"}},{"terms":{"tags":["a test tag"]}}]}}}

I guess I'm still confused as to why that query wouldn't match a record  
like the one in my original post. It seems that even if it were matching on  
an analyzed field, all 3 terms ("a", "test", "tag") exist in the tags  
array, therefore it should match?

---

<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 15, 2011, 7:02am UTC](https://discuss.elastic.co/t/query-not-returning-expected-result/5858/4 "2011-11-15T07:02:44Z")

</div>

("a", "test", "tag") are the analyzed terms and exists in the index (well,  
"a" is not because its a stop word). But, when you try and match on a  
_single_ term of "test tag", it won't match on either "test" or "tag".

On Mon, Nov 14, 2011 at 2:52 AM, Tyler Schroeder [vailripper@gmail.com](mailto:vailripper@gmail.com)wrote:

> Yes, I'm using default the default mappings. In that case, shouldn't a  
> query with a ferms filter on tags, with the value of "a test tag" match a  
> record which has a tags value of "a test tag"? For example:
> 
> {"filtered":{"query":{"query\_ **string":{"query":"\*"}},"**  
> filter":{"and":[{"term":{"\*\*active":true}},{"term":{" **tenant":"**  
> 4e669ec4139ad42bb8428031"}},{"\*\*terms":{"tags":["a test tag"]}}]}}}
> 
> I guess I'm still confused as to why that query wouldn't match a record  
> like the one in my original post. It seems that even if it were matching on  
> an analyzed field, all 3 terms ("a", "test", "tag") exist in the tags  
> array, therefore it should match?

---

<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:48am UTC](https://discuss.elastic.co/t/query-not-returning-expected-result/5858/5 "2017-07-06T03:48:39Z")

</div>


