# Logic of search in list of documents

**URL:** https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379
**Category:** Elasticsearch
**Created:** [May 11, 2011, 5:55am UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379 "2011-05-11T05:55:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![elvis\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elvis_2/32/3225_2.png) [@elvis\_2](https://discuss.elastic.co/u/elvis_2)
#### Post date: [May 11, 2011, 5:55am UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/1 "2011-05-11T05:55:32Z")

</div>

Hello!

Could you give me an advice? I am a novice in Elasticsearch.

I have these documents:  
{  
document1: [{location: foo, type: 3}, {location: baz, type: 1}],  
document2: [{location: foo, type: 1}, {location: bar, type: 2}]  
}

I want to find document with location "foo" and type 2 and I expect to  
get only document2, but when I use bool query (with "must") I receive  
both of docmunents.

What methods do you know to receive only document2?

---

<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: [May 11, 2011, 6:28am UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/2 "2011-05-11T06:28:57Z")

</div>

Hi Elvis

> Could you give me an advice? I am a novice in Elasticsearch.
> 
> I have these documents:  
> {  
> document1: [{location: foo, type: 3}, {location: baz, type: 1}],  
> document2: [{location: foo, type: 1}, {location: bar, type: 2}]  
> }
> 
> I want to find document with location "foo" and type 2 and I expect to  
> get only document2, but when I use bool query (with "must") I receive  
> both of docmunents.

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"and" : {  
"filters" : [  
{  
"term" : {  
"type" : 2  
}  
},  
{  
"term" : {  
"location" : "foo"  
}  
}  
]  
}  
}  
}  
}  
}  
'

---

<div class="post-metadata">

### Author: ![elvis\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elvis_2/32/3225_2.png) [@elvis\_2](https://discuss.elastic.co/u/elvis_2)
#### Post date: [May 11, 2011, 8:25am UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/3 "2011-05-11T08:25:20Z")

</div>

Thank you very much!  
It helps me.

But why Bool filter and And filter have different behaviours?

---

<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: [May 11, 2011, 8:31am UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/4 "2011-05-11T08:31:54Z")

</div>

On Wed, 2011-05-11 at 01:25 -0700, Elvis wrote:

> Thank you very much!  
> It helps me.
> 
> But why Bool filter and And filter have different behaviours?

I think you must have formatted your query incorrectly. The and/or  
filters perform better than the bool filter, which is why I used it.

Here is the same thing with the bool filter:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"bool" : {  
"must" : [  
{  
"term" : {  
"type" : 2  
}  
},  
{  
"term" : {  
"location" : "foo"  
}  
}  
]  
}  
}  
}  
}  
}  
'  
clint

---

<div class="post-metadata">

### Author: ![elvis\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elvis_2/32/3225_2.png) [@elvis\_2](https://discuss.elastic.co/u/elvis_2)
#### Post date: [May 11, 2011, 12:00pm UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/5 "2011-05-11T12:00:18Z")

</div>

Sorry, but you advice was helpless.  
I have two docs:

doc1: {family\_members: [{name: Anna, twin: true}]}  
doc2: {family\_members: [{name: Anna, twin: false}, {name: Olga, twin:  
true}]}

And I try to find twin Anna. My query is:

{  
"query":{  
"constant\_score":{  
"filter":{  
"and":[  
{  
"query":{  
"query\_string":{  
"query":"Anna",  
"default\_operator":"AND",  
"default\_field":"family\_members.name"  
}  
}  
},  
{  
"query":{  
"query\_string":{  
"query":"True",  
"default\_operator":"AND",  
"default\_field":"family\_members.twin"  
}  
}  
}  
]  
}  
}  
}  
}

But it returns doc1 and doc2. I expect to get only doc1. What is my  
mistake?

---

<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: [May 11, 2011, 12:22pm UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/6 "2011-05-11T12:22:20Z")

</div>

Hi Elvis

> doc1: {family\_members: [{name: Anna, twin: true}]}  
> doc2: {family\_members: [{name: Anna, twin: false}, {name: Olga, twin:  
> true}]}

ES doesn't relate the values like you expect. So the above two docs are  
actually stored more like this:

doc1:  
family\_members:  
name: ['Anna']  
twin: [true]  
doc2:  
family\_members:  
name: ['Anna','Olga']  
twin: [true,false]

which means that your query won't work.

You could store the docs as:

doc1:  
family\_members:  
singles: ,  
twins: ['Anna']  
doc2:  
family\_members:  
singles: ['Anna'],  
twins: ['Olga']

then search for:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"field" : {  
"family\_members.twins" : "Anna"  
}  
}  
}  
'

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, 4:06am UTC](https://discuss.elastic.co/t/logic-of-search-in-list-of-documents/4379/7 "2017-07-06T04:06:30Z")

</div>


