# Searching multiple nested fields not working (ES + ROR)

**URL:** <https://discuss.elastic.co/t/searching-multiple-nested-fields-not-working-es-ror/45808>\
**Category:** Elasticsearch\
**Created:** [March 30, 2016, 2:38pm UTC](https://discuss.elastic.co/t/searching-multiple-nested-fields-not-working-es-ror/45808 "2016-03-30T14:38:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin10](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@martin10](https://discuss.elastic.co/u/martin10)\
**Post date:** [March 30, 2016, 2:38pm UTC](https://discuss.elastic.co/t/searching-multiple-nested-fields-not-working-es-ror/45808/1 "2016-03-30T14:38:44Z")

</div>

Hi,

I am trying to do some aggregations which I will use as filters but I am having a problem with searching the fields I am aggregating. This is my mapping:

```
  settings :index => { :number_of_shards => 1 } do
    mapping do
      indexes :id, index: :not_analyzed
      indexes :name
      indexes :summary
      indexes :description

      indexes :occasions, type: 'nested' do
        indexes :id, type: 'integer'
        indexes :occasion_name, type: 'string', index: :not_analyzed
      end

      indexes :courses, type: 'nested' do
        indexes :id, type: 'integer'
        indexes :course_name, type: 'string', index: :not_analyzed
      end

```

And this is where I define the query:

```
 def multi_match_query(query)
      {

        bool: {

            should: [
                {
                    nested:{
                        path: 'occasions',
                        query: {
                            multi_match:
                            {
                                      query: query,
                                      type: "best_fields",
                                      fields: ["occasions.occasion_name"]

                                  }
                        }
                    }
                },

                {
                    nested:{
                        path:'courses',
                        query: {
                            multi_match:
                                {
                                    query: query,
                                    type: "best_fields",
                                    fields: ["course_name"]

                                }
                        }
                    }
                },

                {
                    multi_match: {
                        query: query,
                        fields:["name^9", "summary^8", "cuisine_name^7", "description^6"],

                    }
                }
            ]
        }

      }
    end

```

So far I am able to correctly display my aggregations and search on all fields except on "course\_name". The other aggregated field is searchable. The search query sent from RoR to ES looks like this:

```
{:query=>
        {:bool=>
          {:should=>
            [{:nested=>{:path=>"occasions", :query=>{:multi_match=>{:query=>"salad", :type=>"best_fields", :fields=>["occasions.occasion_name"]}}}},
             {:nested=>{:path=>"courses", :query=>{:multi_match=>{:query=>"salad", :type=>"best_fields", :fields=>["course_name"]}}}},
             {:multi_match=>{:query=>"salad", :fields=>["name^9", "summary^8", "cuisine_name^7", "description^6"]}}]}},
       :aggs=>
        {:occasion_aggregation=>
          {:nested=>{:path=>"occasions"}, :aggs=>{:id_and_name=>{:terms=>{:script=>"doc['occasions.id'].value + '|' + doc['occasions.occasion_name'].join(' ')", :size=>35}}}},
         :course_aggregation=>{:nested=>{:path=>"courses"}, :aggs=>{:id_and_name=>{:terms=>{:script=>"doc['courses.id'].value + '|' + doc['courses.course_name'].join(' ')", :size=>35}}}}}}}

```

Any help will be much appreciated!

---

<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 5, 2017, 11:04pm UTC](https://discuss.elastic.co/t/searching-multiple-nested-fields-not-working-es-ror/45808/2 "2017-07-05T23:04:02Z")

</div>


