Skip to content

Instantly share code, notes, and snippets.

@acreeger
Created June 15, 2011 19:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save acreeger/1027916 to your computer and use it in GitHub Desktop.
Save acreeger/1027916 to your computer and use it in GitHub Desktop.
ElasticSearch: query_string vs text queries
curl -XDELETE http://localhost:9200/ac-test
curl -XPUT http://localhost:9200/ac-test
curl -XPUT http://localhost:9200/ac-test/people/1 -d '
{
"firstNames" : "James Earl",
"lastName" : "Jones",
"location" : "Hollywood, CA"
}'
curl -XPUT http://localhost:9200/ac-test/people/2 -d '
{
"firstNames" : "Earl",
"lastName" : "Grey",
"location" : "London, UK"
}'
curl -XPUT http://localhost:9200/ac-test/people/3 -d '
{
"firstNames" : "James Maxwell",
"lastName" : "Earl",
"location" : "Portland, OR"
}'
curl -XPOST 'http://localhost:9200/ac-test/_refresh'
echo 'We expect 2 hits from this'
curl -XPOST http://localhost:9200/ac-test/people/_search -d '
{
"query" : {
"query_string" : {
"fields": ["firstNames","lastName","location"],
"query":"James Earl",
"default_operator" : "AND"
}
}
}
'
echo 'We expect 1 hit from this'
curl -XPOST http://localhost:9200/ac-test/people/_search -d '
{
"query" : {
"query_string" : {
"fields": ["firstNames","lastName","location"],
"query":"\"James Earl\"",
"default_operator" : "AND"
}
}
}
'
echo 'We expect 1 hit from this'
curl -XPOST http://localhost:9200/ac-test/people/_search -d '
{
"query" : {
"query_string" : {
"fields": ["firstNames","lastName","location"],
"query":"James Portland",
"default_operator" : "AND"
}
}
}
'
echo And this errors...
curl -XPOST http://localhost:9200/ac-test/people/_search -d '
{
"query" : {
"query_string" : {
"fields": ["firstNames","lastName","location"],
"query":"\"James Maxwell\" Portland, OR",
"default_operator" : "AND"
}
}
}
'
# In order to fix the error, we could use a text query. But what are the equivalent queries using a text query?
# If the purpose of the text query is to meet the needs of a search box used by the general public, wouldn't this be a better interface?
# curl -XPOST http://localhost:9200/ac-test/people/_search -d '
# {
# "query" : {
# "text" : {
# "fields": ["firstNames","lastName","location"],
# "query":"\"James Maxwell\" Portland, OR",
# "default_operator" : "AND"
# }
# }
# }
# '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment