Problems trying to restrict query to match a single field


(John Kirkley) #1

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"
}

(John Kirkley) #2

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
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"
    }

(Shay Banon) #3

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
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"
}


(John Kirkley) #4

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 '
{
"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 '
{
"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 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
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"
}


(Shay Banon) #5

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.

-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 '
{
"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 '
{
"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 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>


(John Kirkley) #6

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 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.

-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 '
{
"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 '
{
"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 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>


(system) #7