Needs to return only the matched nested objects wi

2019-04-15 02:32发布

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`.

0条回答
登录 后发表回答