我正在尝试为我的数据添加时间戳,使用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***
Run Code Online (Sandbox Code Playgroud)
这将被elasticsearch接受并将被导入Kibana,但_timestamp字段实际上不会被索引(当在"时间字段名称"下配置索引模式时,它确实显示在下拉列表中).
我也试过像这样格式化metaFields:
{'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'
}
}
}
Run Code Online (Sandbox Code Playgroud)
这也行不通.
最后,我尝试在索引中包含_timestamp字段并应用格式,但我在弹性搜索时出错.
{'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'
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Run Code Online (Sandbox Code Playgroud)elasticsearch.exceptions.TransportError: TransportError(500,u'IllegalArgumentException[Malformed action/metadata line [1], expected a simple value for field [_timestamp] …
我正在使用ElasticSearch批量Python API,它是否同时提供同步和异步api?
根据docs,这应该足够了:
"settings": {
"index.mapping.ignore_malformed": true
}
Run Code Online (Sandbox Code Playgroud)
但是如何在 python 包装器上实现这一点呢?我当前的代码如下所示:
from elasticsearch_dsl import Index
index = Index('my_index', my_conn)
index.settings(
number_of_shards=ES_NUMBER_OF_SHARDS,
number_of_replicas=ES_NUMBER_OF_REPLICAS
)
index.create()
Run Code Online (Sandbox Code Playgroud) python python-2.7 elasticsearch elasticsearch-dsl elasticsearch-py
我正在尝试使用从脚本调用的Python客户端https://github.com/elastic/elasticsearch-py(也在容器中运行)为容器化的Elasticsearch db编制索引。
通过查看现有的代码片段,看来这docker-compose是用于我的目的的有用工具。我的目录结构是
docker-compose.ymlindexer
Dockerfileindexer.pyrequirements.txtelasticsearch
Dockerfile我的docker-compose.yml读物
version: '3'
services:
elasticsearch:
build: elasticsearch/
ports:
- 9200:9200
networks:
- deploy_network
container_name: elasticsearch
indexer:
build: indexer/
depends_on:
- elasticsearch
networks:
- deploy_network
container_name: indexer
networks:
deploy_network:
driver: bridge
Run Code Online (Sandbox Code Playgroud)
indexer.py 读
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
es = Elasticsearch(hosts=[{"host":'elasticsearch'}]) # what should I put here?
actions = [
{
'_index' : 'test',
'_type' : 'content',
'_id' : str(item['id']),
'_source' : …Run Code Online (Sandbox Code Playgroud) 使用官方ElasticSearch Python 库(文档)
我创建一个索引:
doc = {
"something": "123a",
"somethingelse": "456b",
"timestamp": datetime.now(),
"history": []
}
es.index(index="someindex", doc_type="somedoctype", id="someid", body=doc)
Run Code Online (Sandbox Code Playgroud)
我想每次都将项目附加到历史记录中,而不是覆盖它们:
es.update(index="someindex", doc_type="somedoctype", id="someid",
body={"doc": {
"history": {
"123abc": "abc", "456def": "def", "timestamp": datetime.now()
}
}
})
Run Code Online (Sandbox Code Playgroud)
我必须在第二个代码片段中更改什么才能将其附加到历史记录数组/列表中,而不是每次都覆盖它?
我有一个跨多个索引的搜索查询。要根据文档启用此功能,我需要提供一个以逗号分隔的索引列表。
但是当我尝试这样做时: es.search(index='index1,index2',body=body)
我没有得到任何结果:
{u'hits': {u'hits': [], u'total': 0, u'max_score': None}, u'_shards': {u'successful': 10, u'failed': 0, u'skipped': 0, u'total': 10}, u'took': 1, u'timed_out': False}
但是, index='_all' 可以搜索所有索引。我在这里做错了什么还是这个功能有问题?谢谢。
首先,我想让大家知道我知道ElasticSearch Scroll API如何工作的基本工作逻辑。要使用Scroll API,首先,我们需要使用一些滚动值(如1m )调用search方法,然后它将返回一个_scroll_id,该_scroll_id将用于 Scroll 上的下一个连续调用,直到所有文档在循环中返回。但问题是我只想在多线程的基础上使用相同的进程,而不是串行。例如:
如果我有 300000 个文档,那么我想以这种方式处理/获取文档
所以我的问题是,我没有找到任何方法来设置滚动 API 上的from值,如何使用线程使滚动过程更快。不要以序列化的方式处理文档。
我的示例 python 代码
if index_name is not None and doc_type is not None and body is not None:
es = init_es()
page = es.search(index_name,doc_type, scroll = '30s',size = 10, body = body)
sid = page['_scroll_id']
scroll_size = page['hits']['total']
# Start scrolling
while (scroll_size > …Run Code Online (Sandbox Code Playgroud) 我正在使用 python-elasticsearch 模块,我在python-elasticsearch 文档中curl读到,您可以将所有底层 HTTP 请求记录为命令行命令:
elasticsearch.trace 可用于使用漂亮打印的 json 以 curl 命令的形式将请求记录到服务器,然后可以从命令行执行。因为它被设计为共享(例如演示一个问题),所以它也只使用 localhost:9200 作为地址而不是主机的实际地址。如果尚未配置跟踪记录器,则将其设置为传播=假,因此需要单独激活。
对于 python-elasticsearch 模块,您如何启用此curl日志记录?
我试过:
logging.basicConfig(level=logging.DEBUG)但没有输出 curlelasticsearch.trace记录器并将该记录器的级别logging.DEBUG设置为然后设置es_trace_logger.propagate = True但这些都不起作用python logging instrumentation elasticsearch elasticsearch-py
我正在尝试使用 elasticsearch-py 在我的应用程序代码中关闭与我的 elasticsearch 集群的连接。
目前,我正在使用:
es = Elasticsearch()
es.close()
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
Traceback (most recent call last):
File "tmp.py", line 45, in <module>
es.close()
AttributeError: 'Elasticsearch' object has no attribute 'close'
Run Code Online (Sandbox Code Playgroud)
我也没有在文档中看到任何关闭方法:
https://elasticsearch-py.readthedocs.io/en/master/api.html
任何帮助将不胜感激!提前致谢 :)
我使用 python 生成大量具有随机内容的 elasticsearch 文档,并使用elasticsearch-py对其进行索引。
简化的工作示例(只有一个字段的文档):
from elasticsearch import Elasticsearch
from random import getrandbits
es_client = Elasticsearch('https://elastic.host:9200')
for i in range(1,10000000):
document = {'my_field': getrandbits(64)}
es_client.index(index='my_index', document=document)
Run Code Online (Sandbox Code Playgroud)
由于这会对每个文档发出一个请求,因此我尝试通过使用 API 发送 1000 个文档块来加快速度_bulk。然而,到目前为止我的尝试还没有成功。
我对文档的理解是,您可以将 iterable 传递给bulk(),所以我尝试了:
from elasticsearch import Elasticsearch
from random import getrandbits
es_client = Elasticsearch('https://elastic.host:9200')
document_list = []
for i in range(1,10000000):
document = {'my_field': getrandbits(64)}
document_list.append(document)
if i % 1000 == 0:
es_client.bulk(operations=document_list, index='my_index')
document_list = []
Run Code Online (Sandbox Code Playgroud)
但这会导致
elasticsearch.BadRequestError: BadRequestError(400, 'illegal_argument_exception', '格式错误的操作/元数据行 …
我正在使用 2 个类似的 ES 方法来加载和删除文档:
result = es.search(index='users_favourite_documents',
doc_type='favourite_document',
body={"query": {"match": {'user': user}}})
Run Code Online (Sandbox Code Playgroud)
和:
result = es.delete_by_query(index='users_favourite_documents',
doc_type='favourite_document',
body={"query": {"match": {'user': user}}})
Run Code Online (Sandbox Code Playgroud)
第一个工作正常并返回预期记录。
第二个抛出异常:
"TransportError(404,'{
\"found\":false,
\"_index\":\"user_favourite_documents\",
\"_type\":\"favourite_document\",
\"_id\":\"_query\" ,\"_version\":1,
\"_shards\":{\"总计\":2,\"成功\":2,\"失败\":0}}')"
我究竟做错了什么?
我一直在尝试使用python的elasticsearch库连接到我的ElasticSearch主机。因此,代码如下所示:
client = Elasticsearch(
["https://my-machine.io:9200"],
http_auth=("user", "password")
)
Run Code Online (Sandbox Code Playgroud)
现在的问题是,该指令仅在我使用python 2.7解释器时有效,而在python 3.6时失败,从而产生以下错误:
File "/usr/local/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 171, in __init__
self.transport = transport_class(_normalize_hosts(hosts), **kwargs)
File "/usr/local/lib/python3.6/site-packages/elasticsearch/transport.py", line 108, in __init__
self.set_connections(hosts)
File "/usr/local/lib/python3.6/site-packages/elasticsearch/transport.py", line 163, in set_connections
connections = list(zip(connections, hosts))
File "/usr/local/lib/python3.6/site-packages/elasticsearch/transport.py", line 160, in _create_connection
return self.connection_class(**kwargs)
File "/usr/local/lib/python3.6/site-packages/elasticsearch/connection/http_urllib3.py", line 78, in __init__
raise ImproperlyConfigured("Root certificates are missing for certificate "
elasticsearch.exceptions.ImproperlyConfigured: Root certificates are missing for certificate validation. Either pass them in using the ca_certs parameter or install certifi to …Run Code Online (Sandbox Code Playgroud) elasticsearch-py ×12
elasticsearch ×10
python ×9
python-3.x ×2
bulkinsert ×1
docker ×1
indexing ×1
json ×1
kibana ×1
kibana-4 ×1
logging ×1
python-2.7 ×1