简体   繁体   English

使用python从弹性搜索获取数据表可视化

[英]Use python to get data table Visualization from elastic search

How can I filter and summarize data in elastic search through python. 如何通过python在弹性搜索中过滤和汇总数据。 I manually created a data table visualization through Kibana interface and downloaded it in .csv format. 我通过Kibana界面手动创建了数据表可视化,并以.csv格式下载了它。 Now I want to do the same using python. 现在我想使用python做同样的事情。

For example, if there are 10 variables in the index: v1,v2,v3,.. v10 then how to get a data table which can be described in sql as: 例如,如果索引中有10个变量: v1,v2,v3,.. v10则如何获取数据表,该数据表可以在sql中描述为:

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

Till now I am able to do this: 到现在为止,我可以执行以下操作:

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'}}})

Now what I want is to select only variable v2 from my_docs and also group by v2 to get a count. 现在,我要从my_docs中仅选择变量v2 ,并按v2分组以获得计数。 Apologies that I don't know how to create a reproducible example without revealing the user credentials. 在不透露用户凭据的情况下我不知道如何创建可复制示例的道歉。

  • First: I do not want to download complete documents (each document in the actual data contain 150+ variables). 第一:我不想下载完整的文档(实际数据中的每个文档都包含150多个变量)。

If you want to treat only few fields on your doc, you should use the _source filter before your query - doc here . 如果您只想处理文档中的少数几个字段,则应在此处查询doc之前使用_source filter For example to retrieve from your docs only the v1 and v2 fields : 例如,仅从文档中检索v1v2字段:

body={
    "_source": ["v1", "v2"],"query": {"match": {'v1':'US'}}}
  • Second: I am not familiar with json yet, though I am working on it. 第二:虽然我正在研究json,但我还不熟悉json。

You just try something like this: 您只需尝试如下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM