elastic 不会尝试从您的输入 data.frame 或列表中捕获数据类型到docs_bulk() - 我们可以考虑尝试这样做,但我想 R 数据类型不会完全映射到 Elasticsearch 类型- 可能会尝试映射数据类型。这是我的做法:
library('elastic')
connect()
虚拟数据帧
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")