通过 API 添加文档到 elasticsearch,通过 kibana 发现它们。如何?
Adding docs to elasticsearch via API, discovering them via kibana. How?
我通常通过 logstash 插件将数据(日志)插入到 elasticsearch 中。然后我
可以从 kibana 搜索它们。
但是,如果我尝试以编程方式在 elasticsearch 中插入数据(为了
跳过 filebeat 和 logstash),我在 kibana 中找不到数据。
这是我测试的:
from elasticsearch import Elasticsearch
es = Elasticsearch(["XXX"], ...)
doc = {
"@version": 1,
"@timestamp": datetime.now(),
"timestamp": datetime.now(), # Just in case this is needed too
"message": "test message"
}
res = es.index(
index="foobar-2019.05.13", doc_type='whatever', id=3, body=doc,
refresh=True
)
# Doc is indexed by above code, as proved by
# es.search(
# index="foobar-*", body={"query": {"match_all": {}}}
#)
我在“Index Pattern -> Create”中将索引模式 `foobar-*`` 添加到 kibana
索引模式”。然后,我可以使用 "discover" 页面在该页面中搜索文档
指数。但是 kibana 没有找到任何文件,即使这些文件存在于
弹性搜索。
我错过了什么?是否有任何应为索引配置的映射?
(注意:使用 6.x 版本)
更新:文档索引示例和索引映射
# Example of doc indexed
{'_index': 'foobar-2019.05.13', '_type': 'doc', '_id': '41', '_score': 1.0,
'_source': {'author': 'foobar', 'message': 'karsa big and crazy. icarium crazy. mappo big.',
'timestamp': '2019-05-13T15:52:19.857898',
'@version': 1, '@timestamp': '2019-05-13T15:52:19.857900'}}
# mapping of foobar-2019.05.13'
{
"mapping": {
"doc": {
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "long"
},
"author": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"timestamp": {
"type": "date"
}
}
}
}
}
我发现了问题...主机和主机之间存在 2 小时的时区差异
python 代码是 运行 和 elasticsearch/kibana 主机。
因此,由于我使用的是 datetime.now()
,因此我插入了带有时间戳 "hours in the future" 的文档,并且我正在搜索它们 "anywhere in the past"。
如果我以后寻找它们(或者,如果我等了 2 个小时没有更新
他们), 他们被发现了。
我这边的尴尬错误。
对我来说修复是使用 datetime.now(timezone.utc)
我通常通过 logstash 插件将数据(日志)插入到 elasticsearch 中。然后我 可以从 kibana 搜索它们。
但是,如果我尝试以编程方式在 elasticsearch 中插入数据(为了 跳过 filebeat 和 logstash),我在 kibana 中找不到数据。
这是我测试的:
from elasticsearch import Elasticsearch
es = Elasticsearch(["XXX"], ...)
doc = {
"@version": 1,
"@timestamp": datetime.now(),
"timestamp": datetime.now(), # Just in case this is needed too
"message": "test message"
}
res = es.index(
index="foobar-2019.05.13", doc_type='whatever', id=3, body=doc,
refresh=True
)
# Doc is indexed by above code, as proved by
# es.search(
# index="foobar-*", body={"query": {"match_all": {}}}
#)
我在“Index Pattern -> Create”中将索引模式 `foobar-*`` 添加到 kibana 索引模式”。然后,我可以使用 "discover" 页面在该页面中搜索文档 指数。但是 kibana 没有找到任何文件,即使这些文件存在于 弹性搜索。
我错过了什么?是否有任何应为索引配置的映射?
(注意:使用 6.x 版本)
更新:文档索引示例和索引映射
# Example of doc indexed
{'_index': 'foobar-2019.05.13', '_type': 'doc', '_id': '41', '_score': 1.0,
'_source': {'author': 'foobar', 'message': 'karsa big and crazy. icarium crazy. mappo big.',
'timestamp': '2019-05-13T15:52:19.857898',
'@version': 1, '@timestamp': '2019-05-13T15:52:19.857900'}}
# mapping of foobar-2019.05.13'
{
"mapping": {
"doc": {
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "long"
},
"author": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"timestamp": {
"type": "date"
}
}
}
}
}
我发现了问题...主机和主机之间存在 2 小时的时区差异 python 代码是 运行 和 elasticsearch/kibana 主机。
因此,由于我使用的是 datetime.now()
,因此我插入了带有时间戳 "hours in the future" 的文档,并且我正在搜索它们 "anywhere in the past"。
如果我以后寻找它们(或者,如果我等了 2 个小时没有更新 他们), 他们被发现了。
我这边的尴尬错误。
对我来说修复是使用 datetime.now(timezone.utc)