3

我在我的项目中使用 Elastic 搜索 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_id嵌套在courses学院提供的学院下的学院。我想返回父对象的全部或所有字段,即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`。

4

0 回答 0