【问题标题】:Elasticsearch fix date field value. (change field value from int to string)Elasticsearch 修复日期字段值。 (将字段值从 int 更改为 string)
【发布时间】:2016-09-12 10:08:05
【问题描述】:

我使用 python 将数据从 mongodb 发送到 elasticsearch。

有一个时间戳字段名为update_time,映射如下:

    "update_time" : {
        "type": "date",
        "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    },

epoch_millis 用于时间戳。

一切完成后,我使用range 查询日期范围内的文档。但没有任何回报。经过一番研究,我认为问题是:python时间戳int(time.time())是10位,但在elasticsearch中是13位,official example

输入 my_index/my_type/2?timestamp=1420070400000。

所以我尝试将 update_time 字段更新多个 1000:

{
  "script": {
    "inline": "ctx._source.update_time=ctx._source.update_time*1000"
  }
}

不幸的是,我发现所有update_time 都变成了负数。然后我想出Java int 类型是pow(2,32) -1,远低于1420070400000。所以我猜时间戳字段不应该是 int ?

然后我想将字段值从int更新为字符串(我不想更改字段映射类型,我知道更改映射类型需要重新索引,但这里只需要更改值)

但我不知道我可以使用什么脚本,官方脚本文档没有提到这个

【问题讨论】:

  • 您需要更改映射并将类型设置为long 而不是int。你不能用脚本来改变它。
  • @Val 映射是日期类型,不是int类型!

标签: python elasticsearch elasticsearch-query


【解决方案1】:

我不知道 groovy 是一种语言,并认为它只用于数学方法,因为 ES doc 只写过。

sulition 很简单,把 int 转成 long 就可以了。

curl -XPOST http://localhost:9200/my_index/_update_by_query -d '
{
  "script": {
    "inline": "ctx._source.update_time=(long)ctx._source.update_time*1000"
  }
}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多