使用python从elasticsearch获取数据table可视化

Use python to get data table Visualization from elastic search

如何通过python在弹性搜索中筛选和汇总数据。我通过Kibana界面手动创建了一个数据table可视化,并以.csv格式下载。现在我想使用 python.

做同样的事情

例如,如果索引中有10个变量:v1,v2,v3,.. v10那么如何得到一个数据table,可以在sql中描述为:

select v2, count(v2) 
from index 
where v1 = "some value" 
group by v2 

到目前为止,我可以做到这一点:

from elasticsearch5 import Elasticsearch
user = 'xxx'
password = 'xxx'
url = 'xxx'
command = "%s:%s@%s:9200" % (user,password,url)
x = Elasticsearch(command)
# Get the count of documents
num = x.count(index='my_index')['count']
# Get documents filtered by v1
my_docs = x.search(index="my_index",  body={"query": {"match": {'v1':'US'}}})

现在我想要的是 select 仅来自 my_docs 的变量 v2 并且还按 v2 分组以获得计数。抱歉,我不知道如何在不泄露用户凭据的情况下创建可重现的示例。

  • 第一:我不想下载完整的文件(每个文件在 实际数据包含 150 多个变量)。

如果您只想处理文档中的几个字段,您应该在查询前使用 _source filter - doc here。例如,仅从您的文档中检索 v1v2 字段:

body={
    "_source": ["v1", "v2"],"query": {"match": {'v1':'US'}}}
  • 其次:我还不熟悉json,虽然我正在研究它。

你试试这样的:

for result in mydocs['hits']['hits']:
    print result["_source"]['v1']
    print result["_source"]['v2']