# Nested Boolean Query Question

**URL:** <https://discuss.elastic.co/t/nested-boolean-query-question/11892>\
**Category:** Elasticsearch\
**Created:** [May 9, 2013, 5:41am UTC](https://discuss.elastic.co/t/nested-boolean-query-question/11892 "2013-05-09T05:41:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cweilemann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cweilemann/32/2342_2.png) [@cweilemann](https://discuss.elastic.co/u/cweilemann)\
**Post date:** [May 9, 2013, 5:41am UTC](https://discuss.elastic.co/t/nested-boolean-query-question/11892/1 "2013-05-09T05:41:37Z")

</div>

Hey all,

I've done a bunch of searching and I haven't been able to get an answer to this question - hopefully this isn't a repeat (apologies if it is)...

Preface: I'm using Rails & Tire to perform ElasticSearch.

I have an object, Place, with attributes "name", "city", "state", and "zip". They are indexed as follows:

indexes :name, :type =\> 'multi\_field', :fields =\> {  
:name =\> { :type =\> 'string', :analyzer =\> 'snowball' },  
:"name.exact" =\> { :type =\> 'string', :index =\> :not\_analyzed }  
}  
indexes :city  
indexes :state  
indexes :zip

There are three conditions for searching: 1. Name only, 2. (City, State OR Zip), 3. Name AND (City, State OR Zip).

My code for the "query" block is:

if (City, State).present?  
boolean do  
must { string "name:#{name}_" } if name.present?  
must { string "city:#{city\_state}_" }  
must { string "state:#{city\_state}_" }  
end  
elsif (Zip).present?  
boolean do  
must { string "name:#{name}_" } if name.present?  
must { string "zip:#{query\_parameters["zip"]}_" }  
end  
else  
string "name:#{name}_" }  
end

The aforementioned search conditions #1 and #2 work as expected against multiple tests. However, condition 3 does not - it seems to only pay attention to the "name" field. I'm assuming it has something to do with using the "city\_state" variable to search on both "city" and "state"... But I'm doing this because a user can enter either "Chicago" or "Illinois" in the City, State / Zip text box and the search should still work, using either the geographic center of Chicago or the geographic center of Illinois, respectively.

Anything obvious I'm doing wrong?

Thanks!

--  
-Chris

---

<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, 2:37am UTC](https://discuss.elastic.co/t/nested-boolean-query-question/11892/2 "2017-07-06T02:37:27Z")

</div>

Closed on July 6, 2017, 2:37am UTC


