我正在尝试从pyton查询elasticsearch。我想做的是首先搜索关键字,然后在匹配查询后计算每个关键字的数量。下面是我的代码:
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q, A
from elasticsearch_dsl.query import Match
import pandas as pd #for analyzing the data
#Created a client here
print("keyword?)
keyword= input()
q = (Q("match", myfield=keyword))
a = A("terms", field="myfield")
s = Search(using=client, index="myindex").query(q)
s.aggs(a)
response=s.execute()
bucket_results = response.aggregations.bucket
for key, value in bucket_results.items():
print(key, value)
匹配查询工作正常,但当我试图添加聚合部分时,我得到了这个错误:s.aggs(a)TypeError:“AggsProxy”对象不可调用**bucket_results = response.aggregations.bucket()而不是属性
我还尝试用方法bucket_results = response.aggregations.bucket()**代替attribute访问聚合,但似乎也不起作用。我该怎么办?为什么会出现这个错误?
p.s:我已连接到远程服务器。有没有可能我不被允许做AGGS?
1条答案
按热度按时间pokxtpni1#
查看https://www.lewuathe.com/elasticsearch-aggregation-with-python-dsl.html
from elasticsearch_dsl import搜索,Q,A