将时间戳添加到使用Bulk-API的Elasticsearch-py的ElasticSearch中

问题描述 投票:4回答:2

我正在尝试将时间戳添加到我的数据中,使它具有elasticsearch-py批量索引,然后使用kibana显示数据。

我的数据在kibana中显示,但未使用我的时间戳。配置索引模式后转到“发现”选项卡时,得到0个结果(是的,我尝试调整搜索时间)。

这是我的批量索引json的样子:

{'index': 
         {'_timestamp': u'2015-08-11 14:18:26', 
          '_type': 'webapp_fingerprint', 
          '_id': u'webapp_id_redacted_2015_08_13_12_39_34',
          '_index': 'webapp_index'
         }
}

****JSON DATA HERE***

这将被elasticsearch接受并将导入到Kibana中,但是_timestamp字段实际上不会被索引(在“时间字段名称”下配置索引模式时,它确实会显示在下拉列表中。

我也尝试过这样格式化metaField:

{'index': {
           '_type': 'webapp_fingerprint', 
           '_id': u'webapp_id_redacted_2015_08_13_12_50_04', 
           '_index': 'webapp_index'
          }, 
           'source': {
                      '_timestamp': {
                                     'path': u'2015-08-11 14:18:26',
                                     'enabled': True, 
                                     'format': 'YYYY-MM-DD HH:mm:ss'
                                    }
                     }
}

这也不起作用。

最后,我尝试在索引中包含_timestamp字段并应用格式,但是elasticsearch出现错误。

{'index': {
           '_timestamp': {
                          'path': u'2015-08-11 14:18:26', 
                          'enabled': True, 
                          'format': 'YYYY-MM-DD HH:mm:ss'
                         }, 
           '_type': 'webapp_fingerprint', 
           '_id': u'webapp_id_redacted_2015_08_13_12_55_53', 
           '_index': 'webapp_index'
          }
}

错误是:

elasticsearch.exceptions.TransportError: 
TransportError(500,u'IllegalArgumentException[Malformed action/metadata 
line [1], expected a simple value for field [_timestamp] but found [START_OBJECT]]')

非常感谢任何人可以提供的帮助。如果我对这个问题的解释不够好,我深表歉意。让我知道是否需要进一步说明。谢谢。

python elasticsearch kibana kibana-4 elasticsearch-py
2个回答
5
投票

解决了我自己的问题。基本上,我在创建索引时需要为时间戳添加映射。

request_body = {
    "settings" : {
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "mappings" : {
        "_default_":{
            "_timestamp":{
                 "enabled":"true",
                 "store":"true",
                 "path":"plugins.time_stamp.string",
                 "format":"yyyy-MM-dd HH:m:ss"
             }
         }
    }
}
print("creating '%s' index..." % (index_name))
res = es.indices.create(index = index_name, body = request_body)
print(" response: '%s'" % (res))

0
投票

在最新版本的Elasticsearch中,仅使用PUT / POST API和ISOFORMAT字符串即可。

import datetime
import requests 

query = json.dumps(
{
 "createdAt": datetime.datetime.now().replace(microsecond=0).isoformat(),
}
)
response = requests.post("https://search-XYZ.com/your-index/log", data=query,
               headers={'Content-Type': 'application/json'})
print(response)
© www.soinside.com 2019 - 2024. All rights reserved.