elasticsearch 尝试访问ES聚合时,Python上出现“AggsProxy”对象不可调用错误

apeeds0o  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(107)

我正在尝试从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?

pokxtpni

pokxtpni1#

查看https://www.lewuathe.com/elasticsearch-aggregation-with-python-dsl.html
from elasticsearch_dsl import搜索,Q,A

search_query = Search(using=client, index=index_name)
count = A("terms", field="myfield")
search_query.aggs.bucket("count", count)
response = search_query.execute()

相关问题