# Problems trying to restrict query to match a single field

**URL:** https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650
**Category:** Elasticsearch
**Created:** [December 10, 2010, 6:07pm UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650 "2010-12-10T18:07:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![John\_Kirkley](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@John\_Kirkley](https://discuss.elastic.co/u/John_Kirkley)
#### Post date: [December 10, 2010, 6:07pm UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/1 "2010-12-10T18:07:28Z")

</div>

Hi,

Thanks for the great search index API. Just having a difficulty  
with a fairly straight forward boolean OR query.

I am trying to match a single field with my query, but I am  
getting hits on other fields as well. I cannot figure out how to  
limit the results to the single field.

Here is an example index entry:

{  
"song\_id": 3233490,  
"band\_id": 158406,  
"song\_title": "J. S. Bach: Siciliano from BWV 1031",  
"genre\_id": 1016,  
"genre\_name": "Baroque",  
"band\_name": "Virtually Baroque Players"  
}

The following querys return 0 results:

```
{"query":
         {"term" : { "genre_id": "1016" }}
}

{"query":{"bool":
		  {"must":[
			   {"query_string" : { "query" : "genre_id:1016" }}
		   ]}
		 },
                    "size": "100"
}

```

I managed to get the following query to work,

```
{"query":{"bool":{"must":
				  [
					  {"query_string" :
					   { "query" : "genre_id: or 1016" }
					  }
				  ]
				 }},
                     "size": "100"
}

```

however, it is matching other fields as well.

This query does not return results either:

```
{"query":{"bool":{"must":
				  [
					  {"query_string" :
					   { "query" : "genre_id:1016 or genre_id:1027" }
					  }
				  ]
				 }},
                   "size": "100"
}
```

---

<div class="post-metadata">

### Author: ![John\_Kirkley](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@John\_Kirkley](https://discuss.elastic.co/u/John_Kirkley)
#### Post date: [December 10, 2010, 8:40pm UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/2 "2010-12-10T20:40:22Z")

</div>

I think I have solved this problem: I needed to add quotes around  
numeric field arguments.

Elastic search will accept the bare integer fields and add them,  
but then it does not handle searching them properly. Perhaps  
this is a type issue?

On Dec 10, 1:07 pm, John Kirkley [john.fredrick.kirk...@gmail.com](mailto:john.fredrick.kirk...@gmail.com)  
wrote:

> Hi,
> 
> Thanks for the great search index API. Just having a difficulty  
> with a fairly straight forward boolean OR query.
> 
> I am trying to match a single field with my query, but I am  
> getting hits on other fields as well. I cannot figure out how to  
> limit the results to the single field.
> 
> Here is an example index entry:
> 
> {  
> "song\_id": 3233490,  
> "band\_id": 158406,  
> "song\_title": "J. S. Bach: Siciliano from BWV 1031",  
> "genre\_id": 1016,  
> "genre\_name": "Baroque",  
> "band\_name": "Virtually Baroque Players"
> 
> }
> 
> The following querys return 0 results:
> 
> ```
> {"query":
> {"term" : { "genre_id": "1016" }}
> }
> 
> {"query":{"bool":
> {"must":[
> {"query_string" : { "query" : "genre_id:1016" }}
> ]}
> },
> "size": "100"
> }
> 
> ```
> 
> I managed to get the following query to work,
> 
> ```
> {"query":{"bool":{"must":
> [
> {"query_string" :
> { "query" : "genre_id: or 1016" }
> }
> ]
> }},
> "size": "100"
> }
> 
> ```
> 
> however, it is matching other fields as well.
> 
> This query does not return results either:
> 
> ```
> {"query":{"bool":{"must":
> [
> {"query_string" :
> { "query" : "genre_id:1016 or genre_id:1027" }
> }
> ]
> }},
> "size": "100"
> }
> 
> ```

---

<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: [December 11, 2010, 12:00am UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/3 "2010-12-11T00:00:56Z")

</div>

Those queries should work... . Can you gist a recreation of this (using curl)?  
On Friday, December 10, 2010 at 10:40 PM, John Kirkley wrote:

> I think I have solved this problem: I needed to add quotes around  
> numeric field arguments.
> 
> Elastic search will accept the bare integer fields and add them,  
> but then it does not handle searching them properly. Perhaps  
> this is a type issue?
> 
> On Dec 10, 1:07 pm, John Kirkley [john.fredrick.kirk...@gmail.com](mailto:john.fredrick.kirk...@gmail.com)  
> wrote:
> 
> > Hi,
> > 
> > Thanks for the great search index API. Just having a difficulty  
> > with a fairly straight forward boolean OR query.
> > 
> > I am trying to match a single field with my query, but I am  
> > getting hits on other fields as well. I cannot figure out how to  
> > limit the results to the single field.
> > 
> > Here is an example index entry:
> > 
> > {  
> > "song\_id": 3233490,  
> > "band\_id": 158406,  
> > "song\_title": "J. S. Bach: Siciliano from BWV 1031",  
> > "genre\_id": 1016,  
> > "genre\_name": "Baroque",  
> > "band\_name": "Virtually Baroque Players"
> > 
> > }
> > 
> > The following querys return 0 results:
> > 
> > {"query":  
> > {"term" : { "genre\_id": "1016" }}  
> > }
> > 
> > {"query":{"bool":  
> > {"must":[  
> > {"query\_string" : { "query" : "genre\_id:1016" }}  
> > ]}  
> > },  
> > "size": "100"  
> > }
> > 
> > I managed to get the following query to work,
> > 
> > {"query":{"bool":{"must":  
> > [  
> > {"query\_string" :  
> > { "query" : "genre\_id: or 1016" }  
> > }  
> > ]  
> > }},  
> > "size": "100"  
> > }
> > 
> > however, it is matching other fields as well.
> > 
> > This query does not return results either:
> > 
> > {"query":{"bool":{"must":  
> > [  
> > {"query\_string" :  
> > { "query" : "genre\_id:1016 or genre\_id:1027" }  
> > }  
> > ]  
> > }},  
> > "size": "100"  
> > }

---

<div class="post-metadata">

### Author: ![John\_Kirkley](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@John\_Kirkley](https://discuss.elastic.co/u/John_Kirkley)
#### Post date: [December 11, 2010, 7:25pm UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/4 "2010-12-11T19:25:13Z")

</div>

As I stated above, the problem has been solved, but allow me to  
explain in more detail how:

When I originally created the index, I used bare integers in  
the json (not wrapped in quotes). For example,

$\> curl -XPUT '[http://localhost:9200/myindex/mytype/33](http://localhost:9200/myindex/mytype/33)' -d '  
{  
"song\_id": 1000987,  
"band\_id": 140534,  
"song\_title": "Aftermath",  
"genre\_id": 1396,  
"genre\_name": "Down Tempo",  
"band\_name": "Band X"  
}'

With the index thus created the above queries where not working,  
except for this one:

```
    {"query":{"bool":{"must":
                                      [
                                              {"query_string" :
                                               { "query" :

```

"genre\_id: or 1016 or 1034 or 1129" }  
}  
]  
}},  
"size": "100"  
}

This query ignores the genre\_id field specification and just  
matches every field (not what I wanted, but since most of the  
genre\_ids where unique it 'kinda' worked, but occasionally  
it did match other fields)

I fixed it by re-indexing, this time wrapping the bare integer  
arguments in quotes, as follows:

curl -XPUT '[http://localhost:9200/myindex/mytype/33](http://localhost:9200/myindex/mytype/33)' -d '  
{  
"song\_id": "1000987",  
"band\_id": "140534",  
"song\_title": "Aftermath",  
"genre\_id": "1396",  
"genre\_name": "Down Tempo",  
"band\_name": "Band X"  
}'

Now all of the queries work as expected.

This seems to point to a discrepancy on how the parser/index  
generator is handling bare integers vs. quoted ones.

On Dec 10, 7:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Those queries should work... . Can you gist a recreation of this (using curl)?
> 
> On Friday, December 10, 2010 at 10:40 PM, John Kirkley wrote:
> 
> > I think I have solved this problem: I needed to add quotes around  
> > numeric field arguments.
> 
> > Elastic search will accept the bare integer fields and add them,  
> > but then it does not handle searching them properly. Perhaps  
> > this is a type issue?
> 
> > On Dec 10, 1:07 pm, John Kirkley [john.fredrick.kirk...@gmail.com](mailto:john.fredrick.kirk...@gmail.com)  
> > wrote:
> 
> > > Hi,
> 
> > > Thanks for the great search index API. Just having a difficulty  
> > > with a fairly straight forward boolean OR query.
> 
> > > I am trying to match a single field with my query, but I am  
> > > getting hits on other fields as well. I cannot figure out how to  
> > > limit the results to the single field.
> 
> > > Here is an example index entry:
> 
> > > {  
> > > "song\_id": 3233490,  
> > > "band\_id": 158406,  
> > > "song\_title": "J. S. Bach: Siciliano from BWV 1031",  
> > > "genre\_id": 1016,  
> > > "genre\_name": "Baroque",  
> > > "band\_name": "Virtually Baroque Players"
> 
> > > }
> 
> > > The following querys return 0 results:
> 
> > > {"query":  
> > > {"term" : { "genre\_id": "1016" }}  
> > > }
> 
> > > {"query":{"bool":  
> > > {"must":[  
> > > {"query\_string" : { "query" : "genre\_id:1016" }}  
> > > ]}  
> > > },  
> > > "size": "100"  
> > > }
> 
> > > I managed to get the following query to work,
> 
> > > {"query":{"bool":{"must":  
> > > [  
> > > {"query\_string" :  
> > > { "query" : "genre\_id: or 1016" }  
> > > }  
> > > ]  
> > > }},  
> > > "size": "100"  
> > > }
> 
> > > however, it is matching other fields as well.
> 
> > > This query does not return results either:
> 
> > > {"query":{"bool":{"must":  
> > > [  
> > > {"query\_string" :  
> > > { "query" : "genre\_id:1016 or genre\_id:1027" }  
> > > }  
> > > ]  
> > > }},  
> > > "size": "100"  
> > > }

---

<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: [December 11, 2010, 8:36pm UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/5 "2010-12-11T20:36:33Z")

</div>

Hi John,

You don't need to solve the problem by having the numeric values as strings. Its perfectly ok to have it as numeric values. Its just a matter of how to query it (it just happens to work for you in the text case). I gisted several examples of how to do what you want: [https://gist.github.com/737631](https://gist.github.com/737631).

-shay.banon  
On Saturday, December 11, 2010 at 9:25 PM, John Kirkley wrote:

> As I stated above, the problem has been solved, but allow me to  
> explain in more detail how:
> 
> When I originally created the index, I used bare integers in  
> the json (not wrapped in quotes). For example,
> 
> $\> curl -XPUT '[http://localhost:9200/myindex/mytype/33](http://localhost:9200/myindex/mytype/33)' -d '  
> {  
> "song\_id": 1000987,  
> "band\_id": 140534,  
> "song\_title": "Aftermath",  
> "genre\_id": 1396,  
> "genre\_name": "Down Tempo",  
> "band\_name": "Band X"  
> }'
> 
> With the index thus created the above queries where not working,  
> except for this one:
> 
> {"query":{"bool":{"must":  
> [  
> {"query\_string" :  
> { "query" :  
> "genre\_id: or 1016 or 1034 or 1129" }  
> }  
> ]  
> }},  
> "size": "100"  
> }
> 
> This query ignores the genre\_id field specification and just  
> matches every field (not what I wanted, but since most of the  
> genre\_ids where unique it 'kinda' worked, but occasionally  
> it did match other fields)
> 
> I fixed it by re-indexing, this time wrapping the bare integer  
> arguments in quotes, as follows:
> 
> curl -XPUT '[http://localhost:9200/myindex/mytype/33](http://localhost:9200/myindex/mytype/33)' -d '  
> {  
> "song\_id": "1000987",  
> "band\_id": "140534",  
> "song\_title": "Aftermath",  
> "genre\_id": "1396",  
> "genre\_name": "Down Tempo",  
> "band\_name": "Band X"  
> }'
> 
> Now all of the queries work as expected.
> 
> This seems to point to a discrepancy on how the parser/index  
> generator is handling bare integers vs. quoted ones.
> 
> On Dec 10, 7:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Those queries should work... . Can you gist a recreation of this (using curl)?
> > 
> > On Friday, December 10, 2010 at 10:40 PM, John Kirkley wrote:
> > 
> > > I think I have solved this problem: I needed to add quotes around  
> > > numeric field arguments.
> > 
> > > Elastic search will accept the bare integer fields and add them,  
> > > but then it does not handle searching them properly. Perhaps  
> > > this is a type issue?
> > 
> > > On Dec 10, 1:07 pm, John Kirkley  
> > > wrote:
> > 
> > > > Hi,
> > 
> > > > Thanks for the great search index API. Just having a difficulty  
> > > > with a fairly straight forward boolean OR query.
> > 
> > > > I am trying to match a single field with my query, but I am  
> > > > getting hits on other fields as well. I cannot figure out how to  
> > > > limit the results to the single field.
> > 
> > > > Here is an example index entry:
> > 
> > > > {  
> > > > "song\_id": 3233490,  
> > > > "band\_id": 158406,  
> > > > "song\_title": "J. S. Bach: Siciliano from BWV 1031",  
> > > > "genre\_id": 1016,  
> > > > "genre\_name": "Baroque",  
> > > > "band\_name": "Virtually Baroque Players"
> > 
> > > > }
> > 
> > > > The following querys return 0 results:
> > 
> > > > {"query":  
> > > > {"term" : { "genre\_id": "1016" }}  
> > > > }
> > 
> > > > {"query":{"bool":  
> > > > {"must":[  
> > > > {"query\_string" : { "query" : "genre\_id:1016" }}  
> > > > ]}  
> > > > },  
> > > > "size": "100"  
> > > > }
> > 
> > > > I managed to get the following query to work,
> > 
> > > > {"query":{"bool":{"must":  
> > > > [  
> > > > {"query\_string" :  
> > > > { "query" : "genre\_id: or 1016" }  
> > > > }  
> > > > ]  
> > > > }},  
> > > > "size": "100"  
> > > > }
> > 
> > > > however, it is matching other fields as well.
> > 
> > > > This query does not return results either:
> > 
> > > > {"query":{"bool":{"must":  
> > > > [  
> > > > {"query\_string" :  
> > > > { "query" : "genre\_id:1016 or genre\_id:1027" }  
> > > > }  
> > > > ]  
> > > > }},  
> > > > "size": "100"  
> > > > }
> > 
> > [.fredrick.kirk...@gmail.com](mailto:.fredrick.kirk...@gmail.com)\>

---

<div class="post-metadata">

### Author: ![John\_Kirkley](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@John\_Kirkley](https://discuss.elastic.co/u/John_Kirkley)
#### Post date: [December 11, 2010, 11:20pm UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/6 "2010-12-11T23:20:53Z")

</div>

Hi Shay,

Thank you for the examples. I tried them out and they were  
successful.

I then went back and tried the same with my index with no  
success. Puzzled at this I eventually tried creating a  
completely new index. Now everything works.

Clearly the original index was corrupted somehow.

Thanks again!

On Dec 11, 3:36 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Hi John,
> 
> You don't need to solve the problem by having the numeric values as strings. Its perfectly ok to have it as numeric values. Its just a matter of how to query it (it just happens to work for you in the text case). I gisted several examples of how to do what you want:[https://gist.github.com/737631](https://gist.github.com/737631).
> 
> -shay.banon
> 
> On Saturday, December 11, 2010 at 9:25 PM, John Kirkley wrote:
> 
> > As I stated above, the problem has been solved, but allow me to  
> > explain in more detail how:
> 
> > When I originally created the index, I used bare integers in  
> > the json (not wrapped in quotes). For example,
> 
> > $\> curl -XPUT '[http://localhost:9200/myindex/mytype/33'-d](http://localhost:9200/myindex/mytype/33'-d) '  
> > {  
> > "song\_id": 1000987,  
> > "band\_id": 140534,  
> > "song\_title": "Aftermath",  
> > "genre\_id": 1396,  
> > "genre\_name": "Down Tempo",  
> > "band\_name": "Band X"  
> > }'
> 
> > With the index thus created the above queries where not working,  
> > except for this one:
> 
> > {"query":{"bool":{"must":  
> > [  
> > {"query\_string" :  
> > { "query" :  
> > "genre\_id: or 1016 or 1034 or 1129" }  
> > }  
> > ]  
> > }},  
> > "size": "100"  
> > }
> 
> > This query ignores the genre\_id field specification and just  
> > matches every field (not what I wanted, but since most of the  
> > genre\_ids where unique it 'kinda' worked, but occasionally  
> > it did match other fields)
> 
> > I fixed it by re-indexing, this time wrapping the bare integer  
> > arguments in quotes, as follows:
> 
> > curl -XPUT '[http://localhost:9200/myindex/mytype/33'-d](http://localhost:9200/myindex/mytype/33'-d) '  
> > {  
> > "song\_id": "1000987",  
> > "band\_id": "140534",  
> > "song\_title": "Aftermath",  
> > "genre\_id": "1396",  
> > "genre\_name": "Down Tempo",  
> > "band\_name": "Band X"  
> > }'
> 
> > Now all of the queries work as expected.
> 
> > This seems to point to a discrepancy on how the parser/index  
> > generator is handling bare integers vs. quoted ones.
> 
> > On Dec 10, 7:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > > Those queries should work... . Can you gist a recreation of this (using curl)?
> 
> > > On Friday, December 10, 2010 at 10:40 PM, John Kirkley wrote:
> > > 
> > > > I think I have solved this problem: I needed to add quotes around  
> > > > numeric field arguments.
> 
> > > > Elastic search will accept the bare integer fields and add them,  
> > > > but then it does not handle searching them properly. Perhaps  
> > > > this is a type issue?
> 
> > > > On Dec 10, 1:07 pm, John Kirkley  
> > > > wrote:
> 
> > > > > Hi,
> 
> > > > > Thanks for the great search index API. Just having a difficulty  
> > > > > with a fairly straight forward boolean OR query.
> 
> > > > > I am trying to match a single field with my query, but I am  
> > > > > getting hits on other fields as well. I cannot figure out how to  
> > > > > limit the results to the single field.
> 
> > > > > Here is an example index entry:
> 
> > > > > {  
> > > > > "song\_id": 3233490,  
> > > > > "band\_id": 158406,  
> > > > > "song\_title": "J. S. Bach: Siciliano from BWV 1031",  
> > > > > "genre\_id": 1016,  
> > > > > "genre\_name": "Baroque",  
> > > > > "band\_name": "Virtually Baroque Players"
> 
> > > > > }
> 
> > > > > The following querys return 0 results:
> 
> > > > > {"query":  
> > > > > {"term" : { "genre\_id": "1016" }}  
> > > > > }
> 
> > > > > {"query":{"bool":  
> > > > > {"must":[  
> > > > > {"query\_string" : { "query" : "genre\_id:1016" }}  
> > > > > ]}  
> > > > > },  
> > > > > "size": "100"  
> > > > > }
> 
> > > > > I managed to get the following query to work,
> 
> > > > > {"query":{"bool":{"must":  
> > > > > [  
> > > > > {"query\_string" :  
> > > > > { "query" : "genre\_id: or 1016" }  
> > > > > }  
> > > > > ]  
> > > > > }},  
> > > > > "size": "100"  
> > > > > }
> 
> > > > > however, it is matching other fields as well.
> 
> > > > > This query does not return results either:
> 
> > > > > {"query":{"bool":{"must":  
> > > > > [  
> > > > > {"query\_string" :  
> > > > > { "query" : "genre\_id:1016 or genre\_id:1027" }  
> > > > > }  
> > > > > ]  
> > > > > }},  
> > > > > "size": "100"  
> > > > > }
> 
> > > [.fredrick.kirk...@gmail.com](mailto:.fredrick.kirk...@gmail.com)\>

---

<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:15am UTC](https://discuss.elastic.co/t/problems-trying-to-restrict-query-to-match-a-single-field/3650/7 "2017-07-06T04:15:26Z")

</div>


