首页 文章

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

提问于
浏览
1

我正在探索 elastic R包来向ElasticSearch写一个数据框 . 我正在使用 docs_bulk 功能 .

我的数据框中的一列是 @timestamp ,格式为 POSIXct . 但该字段在弹性搜索中以字符串形式保存 . 有关如何以时间格式保存列的任何想法 .

我还尝试通过手动创建具有适当数据类型定义的索引映射,但它不起作用 .

请建议 .

Version:

R:3.3.1

弹性搜索 - 2.4.1

操作系统 - Redhat

1 回答

  • 2

    elastic 不会尝试从您的输入data.frame或列表中捕获数据类型到 docs_bulk() - 我们可以考虑尝试这样做,但我想R数据类型不会如何做到这一点:

    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")
    

相关问题