# Search two different queries on two different fields

**URL:** <https://discuss.elastic.co/t/search-two-different-queries-on-two-different-fields/9041>\
**Category:** Elasticsearch\
**Created:** [September 17, 2012, 12:06pm UTC](https://discuss.elastic.co/t/search-two-different-queries-on-two-different-fields/9041 "2012-09-17T12:06:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chunki\_Calvin\_Lee](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chunki\_Calvin\_Lee](https://discuss.elastic.co/u/Chunki_Calvin_Lee)\
**Post date:** [September 17, 2012, 12:06pm UTC](https://discuss.elastic.co/t/search-two-different-queries-on-two-different-fields/9041/1 "2012-09-17T12:06:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [September 18, 2012, 9:39am UTC](https://discuss.elastic.co/t/search-two-different-queries-on-two-different-fields/9041/2 "2012-09-18T09:39:16Z")

</div>

> 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](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](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](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](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

--

---

<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:12am UTC](https://discuss.elastic.co/t/search-two-different-queries-on-two-different-fields/9041/3 "2017-07-06T03:12:30Z")

</div>

Closed on July 6, 2017, 3:12am UTC


