使用@timestamp 将数据框写入 Elastic Search

Write data frame to Elastic Search with @timestamp

我正在研究 elastic R 包以将数据框写入 ElasticSearch。 我正在使用 docs_bulk 函数。

我的数据框中的一列是 @timestamp,格式为 POSIXct。 但是该字段正在 Elastic Search 中作为字符串保存。 关于如何以时间格式保存列的任何想法。

我也尝试过使用正确的数据类型定义手动创建索引映射,但它没有用。

求推荐。

版本:

R: 3.3.1

弹性搜索 - 2.4.1

OS - 红帽

elastic 不会尝试从您的输入 data.frame 或列表中捕获数据类型 docs_bulk() - 我们可以考虑尝试这样做,但我想 R 数据类型不会精确映射到 Elasticsearch 类型 - 可能会尝试映射数据类型。以下是我的做法:

library('elastic')
connect()

假人data.frame

df <- data.frame(
  date = as.POSIXct(seq(from = as.Date("2016-10-01"), 
                        to = as.Date("2016-10-31"), by = 'day')),
  num = 1:31
)

创建映射,作为列表或 JSON 字符串

mapping <- list(
  world = list(properties = list(
    date = list(
      type = "date",
      format = "yyyy-mm-dd HH:mm:ss"
    ),
    num = list(type = "long")
)))

建立索引

index_create(index = "hello")

在索引中创建映射

mapping_create(index = "hello", type = "world", body = mapping)

获取映射

mapping_get("hello")
#> $hello
#> $hello$mappings
#> $hello$mappings$world
#> $hello$mappings$world$properties
#> $hello$mappings$world$properties$date
#> $hello$mappings$world$properties$date$type
#> [1] "date"
#> 
#> $hello$mappings$world$properties$date$format
#> [1] "yyyy-mm-dd HH:mm:ss"
#> 
#> 
#> $hello$mappings$world$properties$num
#> $hello$mappings$world$properties$num$type
#> [1] "long"

批量加载data.frame

docs_bulk(df, index = "hello", type = "world")

在索引上搜索

Search("hello")