# Unexpected search results

**URL:** https://discuss.elastic.co/t/unexpected-search-results/7380
**Category:** Elasticsearch
**Created:** [April 18, 2012, 2:02pm UTC](https://discuss.elastic.co/t/unexpected-search-results/7380 "2012-04-18T14:02:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Radek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radek/32/2907_2.png) [@Radek](https://discuss.elastic.co/u/Radek)
#### Post date: [April 18, 2012, 2:02pm UTC](https://discuss.elastic.co/t/unexpected-search-results/7380/1 "2012-04-18T14:02:19Z")

</div>

Hi,

I have a problem formulating my search query. Could someone please  
give me advice how to fix the following test case so that I find a  
result every time? Query string is variable for me. Searching by \_id  
and padding \_id with zeros is mandatory for the application from which  
I extracted this use case.

$curl -X PUT "[http://devel1:9200/test](http://devel1:9200/test)" -d '{"mappings" : {"item":  
{"\_id":{"index":"not\_analyzed","store":"yes"},"properties":{"str":  
{"type":"string"}}}}}'  
$curl -X PUT "[http://devel1:9200/test/item/0001](http://devel1:9200/test/item/0001)" -d '{"item":{"str":"a  
malibu house"}}'

$curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
"query": {  
"query\_string": {  
"query": "a house",

```
  "default_operator": "AND"
}

```

}  
}'  
=\> OK, 1 hit. However the fields property has been omited from the  
query.

$curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
"query": {  
"query\_string": {  
"query": "a house",  
"fields": [  
"\_id",  
"str"  
],  
"default\_operator": "AND"  
}  
}  
}'  
=\> No hits

$curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
"query": {  
"query\_string": {  
"query": "a house",  
"fields": [  
"str"  
],  
"default\_operator": "AND"  
}  
}  
}'  
=\> OK, 1 hit. I left out the \_id field

$curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
"query": {  
"query\_string": {  
"query": "a",  
"fields": [  
"\_id",  
"str"  
],  
"default\_operator": "AND"  
}  
}  
}'  
=\> No hits

Thanks in advance,

Radek

---

<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: [April 19, 2012, 2:51pm UTC](https://discuss.elastic.co/t/unexpected-search-results/7380/2 "2012-04-19T14:51:01Z")

</div>

Its problematic because effectively what happens is for the one with no  
hits ("a house" on \_id and str) an AND boolean query is created on "a"  
(dismax on \_id, and not on str because its a stop word so its removed  
during analysis) and "house" (dismax on \_id and str). So, there is on match  
on the first part, and because the operator is AND, it won't find anything.

This doesn't happen when you just query on str, because the analysis simply  
removes the "a", and there is no query generated against \_id (which is not  
analyzed).

You can build a bool query yourself between two query\_string, one on \_id  
and one on str, this will solve it. Note, you store the \_id, which you will  
always get back when you search, so not sure why you need to do it...

On Wed, Apr 18, 2012 at 5:02 PM, Radek [radek.dvorak@gmail.com](mailto:radek.dvorak@gmail.com) wrote:

> Hi,
> 
> I have a problem formulating my search query. Could someone please  
> give me advice how to fix the following test case so that I find a  
> result every time? Query string is variable for me. Searching by \_id  
> and padding \_id with zeros is mandatory for the application from which  
> I extracted this use case.
> 
> $curl -X PUT "[http://devel1:9200/test](http://devel1:9200/test)" -d '{"mappings" : {"item":  
> {"\_id":{"index":"not\_analyzed","store":"yes"},"properties":{"str":  
> {"type":"string"}}}}}'  
> $curl -X PUT "[http://devel1:9200/test/item/0001](http://devel1:9200/test/item/0001)" -d '{"item":{"str":"a  
> malibu house"}}'
> 
> $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> "query": {  
> "query\_string": {  
> "query": "a house",
> 
> ```
> "default_operator": "AND"
> 
> ```
> 
> }  
> }  
> }'  
> =\> OK, 1 hit. However the fields property has been omited from the  
> query.
> 
> $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> "query": {  
> "query\_string": {  
> "query": "a house",  
> "fields": [  
> "\_id",  
> "str"  
> ],  
> "default\_operator": "AND"  
> }  
> }  
> }'  
> =\> No hits
> 
> $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> "query": {  
> "query\_string": {  
> "query": "a house",  
> "fields": [  
> "str"  
> ],  
> "default\_operator": "AND"  
> }  
> }  
> }'  
> =\> OK, 1 hit. I left out the \_id field
> 
> $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> "query": {  
> "query\_string": {  
> "query": "a",  
> "fields": [  
> "\_id",  
> "str"  
> ],  
> "default\_operator": "AND"  
> }  
> }  
> }'  
> =\> No hits
> 
> Thanks in advance,
> 
> Radek

---

<div class="post-metadata">

### Author: ![Radek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radek/32/2907_2.png) [@Radek](https://discuss.elastic.co/u/Radek)
#### Post date: [April 29, 2012, 11:37am UTC](https://discuss.elastic.co/t/unexpected-search-results/7380/3 "2012-04-29T11:37:43Z")

</div>

Thanks for the answer. I will try building the bool query. I store the  
\_id field to  
make it indexed; the document is searchable by id as well. The \_id  
used to  
be indexed automatically some time ago.

On 19 dub, 16:51, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Its problematic because effectively what happens is for the one with no  
> hits ("a house" on \_id and str) an AND boolean query is created on "a"  
> (dismax on \_id, and not on str because its a stop word so its removed  
> during analysis) and "house" (dismax on \_id and str). So, there is on match  
> on the first part, and because the operator is AND, it won't find anything.
> 
> This doesn't happen when you just query on str, because the analysis simply  
> removes the "a", and there is no query generated against \_id (which is not  
> analyzed).
> 
> You can build a bool query yourself between two query\_string, one on \_id  
> and one on str, this will solve it. Note, you store the \_id, which you will  
> always get back when you search, so not sure why you need to do it...
> 
> On Wed, Apr 18, 2012 at 5:02 PM, Radek [radek.dvo...@gmail.com](mailto:radek.dvo...@gmail.com) wrote:
> 
> > Hi,
> 
> > I have a problem formulating my search query. Could someone please  
> > give me advice how to fix the following test case so that I find a  
> > result every time? Query string is variable for me. Searching by \_id  
> > and padding \_id with zeros is mandatory for the application from which  
> > I extracted this use case.
> 
> > $curl -X PUT "[http://devel1:9200/test](http://devel1:9200/test)" -d '{"mappings" : {"item":  
> > {"\_id":{"index":"not\_analyzed","store":"yes"},"properties":{"str":  
> > {"type":"string"}}}}}'  
> > $curl -X PUT "[http://devel1:9200/test/item/0001](http://devel1:9200/test/item/0001)" -d '{"item":{"str":"a  
> > malibu house"}}'
> 
> > $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> > "query": {  
> > "query\_string": {  
> > "query": "a house",
> 
> > ```
> > "default_operator": "AND"
> > 
> > ```
> > 
> > }  
> > }  
> > }'  
> > =\> OK, 1 hit. However the fields property has been omited from the  
> > query.
> 
> > $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> > "query": {  
> > "query\_string": {  
> > "query": "a house",  
> > "fields": [  
> > "\_id",  
> > "str"  
> > ],  
> > "default\_operator": "AND"  
> > }  
> > }  
> > }'  
> > =\> No hits
> 
> > $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> > "query": {  
> > "query\_string": {  
> > "query": "a house",  
> > "fields": [  
> > "str"  
> > ],  
> > "default\_operator": "AND"  
> > }  
> > }  
> > }'  
> > =\> OK, 1 hit. I left out the \_id field
> 
> > $curl -X POST "[http://devel1:9200/test/\_search](http://devel1:9200/test/_search)" -d '{  
> > "query": {  
> > "query\_string": {  
> > "query": "a",  
> > "fields": [  
> > "\_id",  
> > "str"  
> > ],  
> > "default\_operator": "AND"  
> > }  
> > }  
> > }'  
> > =\> No hits
> 
> > Thanks in advance,
> 
> > Radek

---

<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:30am UTC](https://discuss.elastic.co/t/unexpected-search-results/7380/4 "2017-07-06T03:30:44Z")

</div>


