有人能告诉我如何编写Python语句来聚合(和)关于我的文档的内容吗?
剧本
from datetime import datetime
from elasticsearch_dsl import DocType, String, Date, Integer
from elasticsearch_dsl.connections import connections
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
# Define a default Elasticsearch client
client = connections.create_connection(hosts=['http://blahblahblah:9200'])
s = Search(using=client, index="attendance")
s = s.execute()
for tag in s.aggregations.per_tag.buckets:
print (tag.key)输出
File "/Library/Python/2.7/site-packages/elasticsearch_dsl/utils.py", line 106, in __getattr__
'%r object has no attribute %r' % (self.__class__.__name__, attr_name))
AttributeError: 'Response' object has no attribute 'aggregations'是什么引起的?“聚合”关键字错了吗?还有其他的包裹需要我进口吗?如果“考勤”索引中的文档有一个名为emailAddress的字段,我将如何计算该字段的值?
发布于 2015-06-25 00:45:21
首先。现在我注意到,我在这里写的东西实际上没有定义聚合。关于如何使用它的文档对我来说不是很容易读。使用我上面所写的,我将展开。我正在更改索引名,以提供一个更好的示例。
from datetime import datetime
from elasticsearch_dsl import DocType, String, Date, Integer
from elasticsearch_dsl.connections import connections
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
# Define a default Elasticsearch client
client = connections.create_connection(hosts=['http://blahblahblah:9200'])
s = Search(using=client, index="airbnb", doc_type="sleep_overs")
s = s.execute()
# invalid! You haven't defined an aggregation.
#for tag in s.aggregations.per_tag.buckets:
# print (tag.key)
# Lets make an aggregation
# 'by_house' is a name you choose, 'terms' is a keyword for the type of aggregator
# 'field' is also a keyword, and 'house_number' is a field in our ES index
s.aggs.bucket('by_house', 'terms', field='house_number', size=0)在上面,我们为每个家庭创建了一个桶。因此,水桶的名字将是房子的编号。ElasticSearch (ES)将始终给出适合于该桶的文档数量。Size=0意味着使用所有结果,因为ES的默认设置是只返回10个结果(或者您的开发人员将其设置为要做的任何事情)。
# This runs the query.
s = s.execute()
# let's see what's in our results
print s.aggregations.by_house.doc_count
print s.hits.total
print s.aggregations.by_house.buckets
for item in s.aggregations.by_house.buckets:
print item.doc_count我以前的错误是认为一个弹性搜索查询默认有聚合。你自己定义它们,然后执行它们。然后,您的响应可以被拆分,b您提到的聚合器。
上面的卷曲应该如下所示:
注意:我在Google上使用了ElasticSearch插件/扩展/加载项。从某种意义上说,你可以用//注释事物。
POST /airbnb/sleep_overs/_search
{
// the size 0 here actually means to not return any hits, just the aggregation part of the result
"size": 0,
"aggs": {
"by_house": {
"terms": {
// the size 0 here means to return all results, not just the the default 10 results
"field": "house_number",
"size": 0
}
}
}
}工作-周旋。有人在DSL的GIT告诉我忘记翻译,而只是使用这种方法。这很简单,你可以用卷曲写一些很难的东西。所以我才叫它工作。
# Define a default Elasticsearch client
client = connections.create_connection(hosts=['http://blahblahblah:9200'])
s = Search(using=client, index="airbnb", doc_type="sleep_overs")
# how simple we just past CURL code here
body = {
"size": 0,
"aggs": {
"by_house": {
"terms": {
"field": "house_number",
"size": 0
}
}
}
}
s = Search.from_dict(body)
s = s.index("airbnb")
s = s.doc_type("sleepovers")
body = s.to_dict()
t = s.execute()
for item in t.aggregations.by_house.buckets:
# item.key will the house number
print item.key, item.doc_count希望这能有所帮助。我现在设计所有的内容,然后使用Python语句对结果进行剥离,以得到我想要的结果。这有助于多个级别的聚合(子聚合)。
发布于 2019-03-18 22:12:11
我还没有代表对此发表评论,但我想对马修对VISQL关于from_dict的回答的评论做一个小小的修正。如果要维护搜索属性,请使用update_from_dict而不是from_dict。
根据文档,from_dict创建了一个新的搜索对象,但是update_from_dict将在适当的位置进行修改,如果搜索已经具有索引、使用等属性,那么这就是您想要的。
因此,您需要在搜索之前声明查询主体,然后创建如下搜索:
query_body = {
"size": 0,
"aggs": {
"by_house": {
"terms": {
"field": "house_number",
"size": 0
}
}
}
}
s = Search(using=client, index="airbnb", doc_type="sleep_overs").update_from_dict(query_body)https://stackoverflow.com/questions/29380198
复制相似问题