我有一个伪文件,像这样:

{
    "snapshots": [
        {
            "code":"[id]",
            "dateFrom": "[Date]",
            "dateTo": "[Date]",
            "attribute1": "[Some value]",
            "attribute2": "[Some value]"
        }, {
            "code":"[id]",
            "dateFrom": "[Date]",
            "dateTo": "[Date]",
            "attribute2": "[Some value]"
        }
    ],
    "attributeXYZ":[
        {
            "dateFrom": "[Date]",
            "dateTo": "[Date]",
            "attributeXYZ": "[Some value]"
        },
        {
            "dateFrom": "[Date]",
            "dateTo": "[Date]",
            "attributeXYZ": "[Some value]"
        }
    ],
    "attributeZYX":[
        {
            "dateFrom": "[Date]",
            "dateTo": "[Date]",
            "attributeZYX": "[Some value]"
        },
        {
            "dateFrom": "[Date]",
            "dateTo": "[Date]",
            "attributeZYX": "[Some value]"
        }
    ]
}

字段“快照”,“attributeXYZ”,“attributeZYX”是嵌套对象。是否可以从条件(例如snapshots.attribute1="Some value" AND attributeXYZ.attributeXYZ="Some value" AND snapshots.dateFrom<attributeXYZ.dateTo AND snapshots.dateTo>attributeXYZ.dateFrom)的快照(字段snapshots.dateFrom,snapshots.dateTo,snapshots.code)中选择内部命中对象?因此,我想跨嵌套对象进行查询,并且日期时间上下文必须重叠。我正在使用嵌套聚合,但是还没有成功。

谢谢。

最佳答案

要查询嵌套字段,您需要构造嵌套查询-假设您的示例中的过滤器是snapshots.attribute1 =“Some value” AND attributeXYZ.attributeXYZ =“Some value” AND snapshots.dateFromattributeXYZ.dateFrom?
因此,您需要将这三个条件放入一个 bool(boolean) 查询中,例如:

  {
    "query":{
      "bool":{
          "must":[
              {<term- query for snapshots.attribute1="Some value">},
              {<nested query having term match query inside for
                attributeXYZ.attributeXYZ="Some value" >},
              {<nested query having range queries with gte and lte  inside for
                snapshots.dateFrom<attributeXYZ.dateTo AND
                snapshots.dateTo>attributeXYZ.dateFrom>}
            ]
         }
     }
  }

相反,如果您需要OR条件,则可以将所有Or-ed候选对象放在“should”子句中,并将should子句的minimum_should_match属性设置为“1”。

来到聚合部分,您可以看一下这个问题-How to get nested aggregations buckets using java high level REST client Elasticsearch

08-28 13:37