如何在elasticsearch中搜索文档

how to search in document in elasticsearch

我想使用 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;

的对象

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

其他搜索也会出现这种情况。我该如何处理?

如果您的 folder_applications 属性 是 nested you can use 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":{}
  }
 }
}