Needs to return only the matched nested objects with full parent body in Elasticsearch

依然范特西╮ 提交于 2019-12-22 00:24:18

问题


I am using Elastic search version 1.7 in my project. I have a an index named colleges and under this index there is a nested index name courses like this.

{ 
"name": "College Name"    
"university": "University Name",
"city": 429,
"city_name": "London",
"state": 328,
"state_name": "London",
"courses": [
     {
         "id": 26,
         "degree_name": "Master Of Technology",
         "annual_fee": 100000,
         "stream": "Engineering",
         "degree_id": 9419
     },  
     {
         "id": 28,
         "degree_name": "Master Of Philosophy",
         "annual_fee": 100000,
         "stream": "Philosophy",
         "degree_id": 9420
     }
]
}

What I am doing is that I am trying to filter the the colleges based on state and degree_id which is nested under courses provided by the College. I want to return the full body or all the fields of the parent object i.e colleges and only those courses which matches the query.

The query I return to accomplish the task is

{
   "_source": false,
   "query": {
      "bool": {
          "must": [
                {
                   "term": {
                      "state": "328"
                }
            },
            {
               "nested": {
                    "path": "courses",
                    "query": {
                         "term": {
                             "courses.degree_id": 9419
                          }
                     }
               }
           }
        ]
     }
  }
}

This query is working fine and returning me only those nested objects which matches the query but the wrong with this query is that i declared "_source":false in the parent object. if I declare"_source": truethen it returns me all the nested objects whether they meets the query or not. And the second way the query is working fine is to declare field names in"_source": ["field1", "field2", .... "field100"]. But I have about 50 fields in the parent or colleges index. So i don't want to declare all the field names in_source. Is there any other way to accomplish this without declaring all the field names in_source`.

来源:https://stackoverflow.com/questions/44759718/needs-to-return-only-the-matched-nested-objects-with-full-parent-body-in-elastic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!