python - 使用 Python 在 ElasticSearch 中添加 @timestamp 字段

标签 python elasticsearch kibana

我正在使用 Python 在本地 ElasticSearch (localhost:9200) 中添加条目

目前,我使用的是这种方法:

def insertintoes(data):
"""
Insert data into ElasicSearch
:param data: dict
:return:
"""
timestamp = data.get('@timestamp')
logstashIndex = 'logstash-' + timestamp.strftime("%Y.%m.%d")
es = Elasticsearch()
if not es.indices.exists(logstashIndex):
    # Setting mappings for index
    mapping = '''
        {
            "mappings": {
                  "_default_": {
                    "_all": {
                      "enabled": true,
                      "norms": false
                    },
                    "dynamic_templates": [
                      {
                        "message_field": {
                          "path_match": "message",
                          "match_mapping_type": "string",
                          "mapping": {
                            "norms": false,
                            "type": "text"
                          }
                        }
                      },
                      {
                        "string_fields": {
                          "match": "*",
                          "match_mapping_type": "string",
                          "mapping": {
                            "fields": {
                              "keyword": {
                                "type": "keyword"
                              }
                            },
                            "norms": false,
                            "type": "text"
                          }
                        }
                      }
                    ],
                    "properties": {
                      "@timestamp": {
                        "type": "date",
                        "include_in_all": true
                      },
                      "@version": {
                        "type": "keyword",
                        "include_in_all": true
                      }
                    }
                  }
            }
        }
    '''
    es.indices.create(logstashIndex, ignore=400, body=mapping)

es.index(index=logstashIndex, doc_type='system', timestamp=timestamp, body=data)

data 是一个 dict 结构,具有有效的 @timestamp 定义如下 data['@timestamp'] = datetime。日期时间.now()

问题是,即使我的数据中有时间戳值,Kibana 也不会在 «discovery» 字段中显示该条目。 :(

这是 ElasicSearch 中完整条目的示例:

{ “_index”:“logstash-2017.06.25”, “_type”:“系统”, "_id": "AVzf3QX3iazKBndbIkg4", “_分数”:1, “_来源”: { “优先级”:6, “uid”:0, “吉德”:0, "systemd_slice": "system.slice", "cap_effective": "1fffffffff", "exe": "/usr/bin/bash", “主机名”:“ns3003395”, “syslog_facility”:9, “通信”:“crond”, "systemd_cgroup": "/system.slice/cronie.service", "systemd_unit": "cronie.service", "syslog_identifier": "CROND", “消息”:“(根)CMD(/usr/local/rtm/bin/rtm 14 >/dev/null 2>/dev/null)”, “systemd_invocation_id”:“9228b6c72e6a4624a1806e4c59af8d04”, “syslog_pid”:26652, “pid”:26652, “@timestamp”:“2017-06-25T17:27:01.734453” } }

如您所见,有一个 @timestamp 字段,但它似乎不是 Kibana 所期望的。

并且不知道如何使我的条目在 Kibana 中可见。

有什么想法吗?

最佳答案

Elasticsearch 无法将@timestamp 识别为日期,而是识别为字符串。如果你的data['@timestamp']是一个datetime对象,你可以尝试将其转换为自动识别的ISO字符串,试试:

timestamp = data.get('@timestamp').isoformat()

时间戳现在应该是一个字符串,但是是 ISO 格式

关于python - 使用 Python 在 ElasticSearch 中添加 @timestamp 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44747836/

相关文章:

ruby-on-rails - 如何防止与 searchkick 和 word_start 的部分查询匹配

elasticsearch - 在Elasticsearch查询中转义特殊字符

python - 具有固定值的 Itertools 二进制列表

python - 使用另一个域的用户凭据连接到 SQL Server

python - Scrapy 部署停止工作

elasticsearch - Logstash异常:读取csv文件时出现Errno::EACCES

symfony - 在没有 ES 服务器的情况下使用 FOSElasticaBundle 对 Symfony 应用程序进行单元测试?

elasticsearch - Elasticsearch-如何在所有索引和文档中搜索词的一部分

elasticsearch - 如何将Kibana查询转换为 `elasticsearch_dsl`查询

python - 将 SQL 转换为 pymongo 查询