elasticsearch python查询-按字段分组,然后计数

时间:2018-09-18 07:55:29

标签: python elasticsearch nosql querydsl kibana-4

我对使用python进行Elasticsearch查询感到困惑

我有数据,例如

{'_index': 'toto',
 '_type': 'tata',
 '_id': '9',
 '_version': 14,
 'found': True,
 '_source': {'Loss Event ID': 833,
  'Product': 'Sushi',
  'Company': 'SushiShop',
  'Profit': '10000000'}
}

{'_index': 'toto',
 '_type': 'tata',
 '_id': '11',
 '_version': 14,
 'found': True,
 '_source': {'Loss Event ID': 834,
  'Product': 'Burgers',
  'Company': 'McDonalds',
  'Profit': '4000000000'}
}

{'_index': 'toto',
 '_type': 'tata',
 '_id': '12',
 '_version': 14,
 'found': True,
 '_source': {'Loss Event ID': 836,
  'Product': 'Sushi',
  'Company': 'PlanetSushi',
  'Profit': '20000000'}
}

目标:我想使用python进行查询-与 Product 的group_by保持一致,并计数 Profit 以获得这种结果:

产品|利润

  

->寿司= 30000000

     

->汉堡= 4000000000

     

(...)

有帮助吗?我尝试了python DSL,但失败了

enter code here

from time import time
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk

import requests
res = requests.get('http://localhost:9200')
print(res.content)

#connect to our cluster
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

r = es.search(index='toto',
              doc_type='tata',
              body= {
    "query": { 
            "match" : { "Product": "Sushi" }
    },
    "aggs" : {
                "sum_income" : { "sum" : { "field" : "Profit" } }
    }
})

失败... Tks

1 个答案:

答案 0 :(得分:0)

使用以下汇总查询获取每种产品的总利润。

./term.c:                       tprintf(" %sopost %sisig %sicanon %secho ...}",

注意{ "size": 0, "aggs": { "product_name": { "terms": { "field": "Product" }, "aggs": { "total_profit": { "sum": { "field": "Profit" } } } } } } 字段必须是任何数字类型。