【问题标题】:How to specific day of month search in Elasticsearch如何在 Elasticsearch 中搜索特定月份的日期
【发布时间】:2021-01-07 14:21:10
【问题描述】:

我现在正在做的是获取所有数据并过滤“月中的某天”,例如 2020-01-05、2020-02-05 在熊猫中,

如何在 Elasticsearch 中搜索特定的日期?

我的文档如下所示

{
    "took": 80,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1508,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_data",
                "_type": "test",
                "_id": "SlR42nYBlaUrIoJsVOv6",
                "_score": null,
                "_source": {
                    "timestamp": 1609983082524,
                    "timestampstring": "2021-01-07 09:31:22.524",
                    "oneNetDevieId": "624232444",
                    "type": "data"
                },
                "sort": [
                    2609983082524
                ]
            },
            {
                "_index": "test_data",
                "_type": "test",
                "_id": "6FRL2nYBlaUrIoJsAuba",
                "_score": null,
                "_source": {

...

我尝试过类似的东西

{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script": "doc.timestampstring.date.dayOfMonth == 5"
        }
      }
    }
  }
}

或

{
      "query": {
        "filtered": {
          "filter": {
            "script": {
              "script": "doc._source.timestampstring.date.dayOfMonth == 5"
            }
          }
        }
      }
    }

它显示错误 “类型”:“解析异常”, “原因”:“未知查询[过滤]”, “线”:5, "col": 17

或者我需要映射吗?谢谢

编辑更新:

i got the new error:
  "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "doc['timestamp'].value.getDayOfMonth() == 5",
                        "                      ^---- HERE"
                    ],
                    "script": "doc['timestamp'].value.getDayOfMonth() == 5",
                    "lang": "painless",
                    "position": {
                        "offset": 22,
                        "start": 0,
                        "end": 43
                    },
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "dynamic method [java.lang.Long, getDayOfMonth/0] not found"
                    }
                }
            }
        ]

【问题讨论】:

标签: elasticsearch


【解决方案1】:

正如@Val 所指出的,filtered 被bool query 取代。您需要修改您的查询,如下所示

添加一个包含索引映射、数据、搜索查询和搜索结果的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "timestampstring": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss.SSS"
      }
    }
  }
}

索引数据:

{
   "timestampstring": "2020-11-06 15:22:58.537"
}
{
   "timestampstring": "2020-11-05 21:21:51.655"
}

搜索查询:

{
  "query": {
    "bool" : {
      "filter" : {
       "script" : {
          "script" : {
            "source": "doc['timestampstring'].value.getDayOfMonth() == 5",
            "lang": "painless"
          }
        }
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "65607193",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "timestampstring": "2020-11-05 21:21:51.655"
        }
      }
    ]

【讨论】:

  • 我应该使用时间戳还是时间戳字符串?因为 ES 中似乎不支持时间戳字符串
  • @ManManYu 您需要为该字段定义映射。请仔细阅读我更新的答案,如果这能解决您的问题,请告诉我?
  • 谢谢,但似乎字段“timestamp”:1609983082524,匹配ES中的日期格式“epoch_millis”,所以我想如果我可以用它来做查询,所以我不需要做映射,但我仍然在编辑更新中发布错误
  • @ManManYu 你用的是哪个版本的elasticsearch?
  • 6.8版@某人今天不工作..奇怪
猜你喜欢
  • 2023-03-05
  • 2023-03-03
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 2023-04-08
  • 2011-08-05
  • 1970-01-01
相关资源
最近更新 更多