【问题标题】:Elasticsearch find date use range query for intersects and withinElasticsearch查找相交和范围内的日期使用范围查询
【发布时间】:2020-06-23 01:16:20
【问题描述】:

我在 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"
  }]
}]

我厌倦了在列表中找到相交,我不知道在弹性搜索中逻辑 OR 与编程语言流不同

【问题讨论】:

  • 你能添加你的映射吗
  • 您能否也检查一下您共享的数据。不确定您使用的是 12 小时还是 24 小时表示。 index1 有 24 小时表示。而index2, index3 and index4hour_start 值高于hour_end,这让我想如果它是12 小时的表示,但它又不是上午或下午。
  • { "mappings": { "properties": { "time": { "properties": { "hour_end": { "type": "date", "format": "yyyy-MM -dd HH:mm:ss" }, "hour_start": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } } } } } @jaspreetchahal
  • @OpsterESNinja-Kamal 我认为我的日期是 24 小时,当我尝试过滤以查找日期内的日期时,我会在 24 小时的情况下得到我的预期结果。但我不知道从底部和顶部小时得到一个迭代小时

标签: elasticsearch kibana elastic-stack elasticsearch-5 elk


【解决方案1】:

字段类型必须从对象类型更改为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
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2020-06-14
    • 2016-09-22
    • 2016-10-08
    相关资源
    最近更新 更多