弹性 - 在 inner_hits 中查询嵌套对象的多个级别

elastic - query multiple levels on nested object in inner_hits

我有一个巨大的嵌套对象,它有很多层次 我想创建一个查询,它将 return 只有叶子/中间的一些对象, 并且查询应该查询树中的多个级别。

例如: 我的数据库正在保存整个公司结构。 公司 -> 病房 -> 员工 -> 工作时间

我想查询 return 只有本月下午 3 点以后开始的病房 2 员工的工作时间

我尝试使用 inner_hits - 但没有用。

根据要求,示例文档和预期结果:

company:[{
  properties:{companyId: 112}
  ward:[{
    properties: {wardId: 223}
    employee:{
      properties: {employeeId: 334},
      workingHours: [
        { date: "1.1.2021", numOfHours: 4},
        { date: "1.2.2021", numOfHours: 7}
      ]
    }]
  }]
}]

查询: 我需要 return 日期“1.2.21”,员工 334,病房 223 的工作时间。而且只有工作时间,而不是整个树。

预期结果: 4 或 { date: "1.1.2021", numOfHours: 4} ,更简单的

希望现在晴朗

您需要将 inner_hits 添加到所有嵌套查询

您可以解析整个结果以获得匹配的工作时间(来自内部匹配)o 可以使用 response filtering 删除额外数据

映射

PUT index123
{
  "mappings": {
    "properties": {
      "company": {
        "type": "nested",
        "properties": {
          "ward": {
            "type": "nested",
            "properties": {
              "employee": {
                "type": "nested",
                "properties": {
                  "workingHours": {
                    "type": "nested",
                    "properties": {
                      "date": {
                        "type": "date"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

数据


        "_index" : "index123",
        "_type" : "_doc",
        "_id" : "9gGYI3oBt-MOenya6BcN",
        "_score" : 1.0,
        "_source" : {
          "company" : [
            {
              "companyId" : 112,
              "ward" : [
                {
                  "wardId" : 223,
                  "employee" : {
                    "employeeId" : 334,
                    "workingHours" : [
                      {
                        "date" : "2021-01-01",
                        "numOfHours" : 4
                      },
                      {
                        "date" : "2021-01-02",
                        "numOfHours" : 7
                      }
                    ]
                  }
                }
              ]
            }
          ]
        }
      }

查询

GET index123/_search?filter_path=hits.hits.inner_hits.ward.hits.hits.inner_hits.employee.hits.hits.inner_hits.workingHours.hits.hits._source
{
  "query": {
    "nested": {
      "inner_hits": {
        "name":"ward"
      }, 
      "path": "company.ward",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "company.ward.wardId": {
                  "value": 223
                }
              }
            },
            {
              "nested": {
                "inner_hits": {
                  "name":"employee"
                }, 
                "path": "company.ward.employee",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "company.ward.employee.employeeId": {
                            "value":334
                          }
                        }
                      },
                      {
                        "nested": {
                          "inner_hits": {
                            "name":"workingHours"
                          }, 
                          "path": "company.ward.employee.workingHours",
                          "query": {
                            "range": {
                              "company.ward.employee.workingHours.date": {
                                "gte": "2021-01-01",
                                "lte": "2021-01-01"
                              }
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

结果

{
  "hits" : {
    "hits" : [
      {
        "inner_hits" : {
          "ward" : {
            "hits" : {
              "hits" : [
                {
                  "inner_hits" : {
                    "employee" : {
                      "hits" : {
                        "hits" : [
                          {
                            "inner_hits" : {
                              "workingHours" : {
                                "hits" : {
                                  "hits" : [
                                    {
                                      "_source" : {
                                        "date" : "2021-01-01",
                                        "numOfHours" : 4
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

更新:

使用公司 ID 查询

GET index123/_search?filter_path=hits.hits.inner_hits.company.hits.hits.inner_hits.ward.hits.hits.inner_hits.employee.hits.hits.inner_hits.workingHours.hits.hits._source
{
  "query": {
    "nested": {
      "path": "company",
      "inner_hits": {
        "name": "company"
      },
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "company.companyId": {
                  "value": 112
                }
              }
            },
            {
              "nested": {
                "inner_hits": {
                  "name": "ward"
                },
                "path": "company.ward",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "company.ward.wardId": {
                            "value": 223
                          }
                        }
                      },
                      {
                        "nested": {
                          "inner_hits": {
                            "name": "employee"
                          },
                          "path": "company.ward.employee",
                          "query": {
                            "bool": {
                              "must": [
                                {
                                  "term": {
                                    "company.ward.employee.employeeId": {
                                      "value": 334
                                    }
                                  }
                                },
                                {
                                  "nested": {
                                    "inner_hits": {
                                      "name": "workingHours"
                                    },
                                    "path": "company.ward.employee.workingHours",
                                    "query": {
                                      "range": {
                                        "company.ward.employee.workingHours.date": {
                                          "gte": "2021-01-01",
                                          "lte": "2021-01-01"
                                        }
                                      }
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}