Elasticsearch查找日期用于相交和内部的范围查询

问题描述 投票:0回答:1

我在Elasticsearch中的列表

[{
  "index1": [{
    "hour_start": "2020-03-17 14:00:00",
    "hour_end": "2020-03-17 15:00:00"
  }]
}, {
  "index2": [{
    "hour_start": "2020-03-17 10:00:00",
    "hour_end": "2020-03-17 07:00:00"
  }, {
    "hour_start": "2020-03-18 10:00:00",
    "hour_end": "2020-03-18 07:00:00"
  }]
}, {
  "index3": [{
    "hour_start": "2020-03-17 13:00:00",
    "hour_end": "2020-03-17 10:00:00"
  }]
}, {
  "index4": [{
    "hour_start": "2020-03-17 09:00:00",
    "hour_end": "2020-03-17 04:00:00"
  }]
}]

如何在“ 2020-03-17 06:00:00”至“ 2020-03-17 12:00:00”范围内以及与之相交的位置查找获取列表?

期望输出:

[{
  "index2": [{
    "hour_start": "2020-03-17 10:00:00",
    "hour_end": "2020-03-17 07:00:00"
  }, {
    "hour_start": "2020-03-18 10:00:00",
    "hour_end": "2020-03-18 07:00:00"
  }]
}, {
  "index3": [{
    "hour_start": "2020-03-17 13:00:00",
    "hour_end": "2020-03-17 10:00:00"
  }]
}, {
  "index4": [{
    "hour_start": "2020-03-17 09:00:00",
    "hour_end": "2020-03-17 04:00:00"
  }]
}]

我很累在列表中找不到相交点,在弹性搜索中我不知道逻辑或不同于编程语言流程

elasticsearch kibana elastic-stack elasticsearch-5 elk
1个回答
0
投票

字段类型必须从对象类型更改为nested type。这将允许将数组中的属性视为单独的索引

我已经使用must [AND]子句来涵盖以下情况

       <-----row 1 interval------->

映射:

{
  "mappings": {
    "properties": {
      "time": {
        "type": "nested", ---> note type
        "properties": {
          "hour_end": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "hour_start": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          }
        }
      }
    }
  }
}

查询:

{
  "query": {
    "nested": {
      "path": "time",
      "query": {
        "bool": {
          "must": [ 
            {
              "range": {
                "time.hour_start": {
                  "lte": "2020-03-17 10:00:00"
                }
              }
            },
            {
              "range": {
                "time.hour_end": {
                  "gte": "2020-03-17 04:00:00"
                }
              }
            }
          ]
        }
      },
      "inner_hits": {} --> to get objects in array which match
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.