需要在Elasticsearch中仅返回具有完整父主体的匹配嵌套对象

时间:2017-06-26 12:26:44

标签: elasticsearch elasticsearch-dsl elasticsearch-query

我在我的项目中使用Elastic search 1.7版。我有一个名为colleges的索引,在此索引下有一个嵌套的索引名courses

{ 
"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
     }
]
}

我正在做的是,我正在尝试根据学院提供的state嵌套的degree_idcourses过滤学院。我想返回父对象的完整正文或所有字段,即colleges,只返回与查询匹配的courses

我返回完成任务的查询是

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

此查询工作正常,只返回与查询匹配的嵌套对象,但此查询的错误是我声明"_source":false in the parent object. if I declare“_ source”:true then 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 个答案:

没有答案