如何在elasticsearch中搜索文档

时间:2017-05-21 08:43:34

标签: search elasticsearch types document

我想使用elasticsearch来搜索my_type的属性..

例如在my_type中我有一个名为' folder_applications'的属性:

"folder_applications":  [
   *[
    "employer_folder_id" => 142
    "status_id" => 140
    "folder" =>  [
      "id" => 142
      "employer_id" => 11
    ]
    "created_at" => "2017-04-12"
    "is_applied" => 1
    "hire_stage_id" => 144
  ],
  **[
  "employer_folder_id" => 7922
  "status_id" => 141
  "folder" =>  [
    "id" => 7922
    "employer_id" => 11
    ]
    "created_at" => "2017-04-12"
    "is_applied" => 1
    "hire_stage_id" => 143
  ]
]

* -> first array
** -> second array

这里例如我想在my_type中搜索具有status_id =>的对象141;

当我搜索这个时,我的搜索结果返回了包含所有状态的孔文档..但我只想要status_id => 141 ...不是140或任何其他..例如我想得到folder.id => 7922&& status_id => 141 ..和搜索结果只显示我的第二个数组。

它也适用于其他搜索..我该如何处理?

1 个答案:

答案 0 :(得分:0)

如果您的folder_applications媒体资源为nested,则可以使用Inner Hits仅获取folder_applications数组中的对象。
更新:示例:

{
"query":{
  "nested":{
     "path":"folder_applications",
     "query":{
        "bool":{
           "must":[{"match":{
                    "folder_applications.status_id":141
                 }
              },
              {"nested":{
                    "path":"folder_applications.folder",
                    "query":{
                       "match":{
                          "folder_applications.folder.id":7922
                       }
                    }
                 }
              }
           ]
        }
     },
     "inner_hits":{}
  }
 }
}