Search two different queries on two different fields


(Chunki Calvin Lee) #1

Hi,

what I have tried are
{
"query" : {
"query_string" : {
"fields" : ["city", "menu"]
, "query" : "seoul sushi"
, "default_operator" : "AND"
}
}
}

AND
{
"query" : {
"query_string" : {
"fields" : ["menu"]
, "query" : "sushi"
, "default_operator" : "AND"
}
}
,
"filter" : {
"query" : {
"query_string" : {
"query" : "seoul"
}
}
}
}

These queries work well but what I really want to find is
city = seoul, menu = sushi
case only

How can I do this ?

Thanks,
Calvin.


(Clinton Gormley) #2

These queries work well but what I really want to find is
city = seoul, menu = sushi
case only

Given your current index state, this should work:

{
"query" : {
"bool" : {
"must" : [
{
"text" : {
"city" : "seoul"
}
},
{
"text" : {
"menu" : "sushi"
}
}
]
}
}
}

However, we can make this a good deal more efficient. It looks like
"menu" and "city" are not "full text" fields, but are exact string
values, ie you store "sushi", not "I love SUSHI!"

Assuming that's the case, then I'd do the following:

  1. Set your document mappings to make the "city" and "menu" fields
    not_analyzed:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' -d '
{
"mappings" : {
"restaurant" : {
"properties" : {
"city" : {
"index" : "not_analyzed",
"type" : "string"
},
"menu" : {
"index" : "not_analyzed",
"type" : "string"
},
"description" : {
"type" : "string"
}
}
}
}
}
'

  1. Index your docs with the exact string value for menu/city:

curl -XPOST 'http://127.0.0.1:9200/test/restaurant?pretty=1' -d '
{
"city" : "seoul",
"menu" : "sushi",
"description" : "I love sushi in Seoul!"
}
'

  1. Search for all sushi restaurants in seoul:

curl -XGET 'http://127.0.0.1:9200/test/restaurant/_search?pretty=1' -d
'
{
"query" : {
"constant_score" : {
"filter" : {
"and" : [
{
"term" : {
"city" : "seoul"
}
},
{
"term" : {
"menu" : "sushi"
}
}
]
}
}
}
}
'

  1. Search for keywords, filtering by menu=sushi and city=seoul:

curl -XGET 'http://127.0.0.1:9200/test/restaurant/_search?pretty=1' -d
'
{
"query" : {
"filtered" : {
"filter" : {
"and" : [
{
"term" : {
"city" : "seoul"
}
},
{
"term" : {
"menu" : "sushi"
}
}
]
},
"query" : {
"text" : {
"description" : "love"
}
}
}
}
}
'

clint

--


(system) #3