简体   繁体   English

Elasticsearch DSL:过滤,然后在python中聚合

[英]Elasticsearch DSL: filter, then aggregate in python

I need to filter documents in an Elasticsearch index and then aggregate them by field. 我需要过滤Elasticsearch索引中的文档,然后按字段汇总它们。 Here is the code of what I am trying to do: 这是我正在尝试执行的代码:

import elasticsearch
from elasticsearch_dsl import Search, Q, Index, MultiSearch
es_client = elasticsearch.Elasticsearch([url],
        timeout=30, retry_on_timeout=True)
project_ids=['CSI'] 
family_ids=['SF6140691_WES_CIDR'] 
sample_ids=['S1379354_CIDR'] 
gene_symbols=['GLTPD1', 'CCNL2', 'MRPL20'] 

genes_filter = Q('bool', must=[Q('terms', project_id=project_ids),
                                   Q('terms', family_id=family_ids),
                                   Q('terms', sample_id=sample_ids),
                                   Q('terms', gene_symbol=gene_symbols)])
search = Search(using=es_client, index="GENES_DATA")
search = search.filter(genes_filter).execute()
results = search.aggs.bucket('by_family', 'terms', field='family_id', size=0)

Currently I am getting the following error: 目前,我收到以下错误:

'{!r} object has no attribute {!r}'.format(self. class . name , name)) AttributeError: 'Terms' object has no attribute 'execute' '{!R}对象没有属性{R!}'格式(个体经营 的名称 ,名称。))AttributeError的: '条款'对象有没有属性'执行'

I tried to switch filtering and aggregation, tried doing execute() at the very end, but it does not help. 我试图切换过滤和聚合,最后尝试执行execute() ,但这没有帮助。 How could this simple transformation be achieved - filtering + aggregation ? 如何实现这种简单的转换- filtering + aggregation I found examples of doing aggregations separately or filtering separately but have trouble finding both in one query. 我发现了分别进行聚合或分别进行过滤的示例,但是在一个查询中都找不到这两个示例。

instead of 代替

search = search.filter(genes_filter)
results = search.aggs.bucket('by_family', 'terms', field='family_id', size=0)

you should have: 你应该有:

search = search.filter(genes_filter)
search.aggs.bucket('by_family', 'terms', field='family_id', size=0)
results = search.execute()

First you add a filter, then you define the aggregations and finally you execute your search. 首先添加一个过滤器,然后定义聚合,最后执行搜索。

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

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