本文整理匯總了Python中elasticsearch_dsl.MultiSearch方法的典型用法代碼示例。如果您正苦於以下問題:Python elasticsearch_dsl.MultiSearch方法的具體用法?Python elasticsearch_dsl.MultiSearch怎麽用?Python elasticsearch_dsl.MultiSearch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類elasticsearch_dsl
的用法示例。
在下文中一共展示了elasticsearch_dsl.MultiSearch方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: execute_searches
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def execute_searches(self):
"""Ejecuta la query de todas las series agregadas, e inicializa
los atributos data y count a partir de las respuestas.
"""
multi_search = MultiSearch(index=self.index,
doc_type=settings.TS_DOC_TYPE)
for serie in self.series:
multi_search = multi_search.add(serie.search)
responses = multi_search.execute()
formatter = ResponseFormatter(self.series, responses,
self.args[constants.PARAM_SORT],
self.args[constants.PARAM_PERIODICITY])
return {
'data': (formatter.format_response()),
'count': max([response.hits.total for response in responses])
}
示例2: _execute_multi_search
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def _execute_multi_search(self, **kwargs):
indices = sorted(self._index_searches.keys(), reverse = True) or self._indices
if self.CACHED_COUNTS_KEY and not self.previous_search_results.get(self.CACHED_COUNTS_KEY):
self.previous_search_results[self.CACHED_COUNTS_KEY] = {}
ms = MultiSearch()
for index_name in indices:
start_index = 0
if self.CACHED_COUNTS_KEY:
if self.previous_search_results[self.CACHED_COUNTS_KEY].get(index_name):
index_total = self.previous_search_results[self.CACHED_COUNTS_KEY][index_name]['total']
start_index = self.previous_search_results[self.CACHED_COUNTS_KEY][index_name]['loaded']
if start_index >= index_total:
continue
else:
self.previous_search_results[self.CACHED_COUNTS_KEY][index_name] = {'loaded': 0, 'total': 0}
searches = self._get_paginated_searches(index_name, start_index=start_index, **kwargs)
ms = ms.index(index_name.split(','))
for search in searches:
ms = ms.add(search)
responses = self._execute_search(ms)
parsed_responses = [self._parse_response(response) for response in responses]
return self._process_multi_search_responses(parsed_responses, **kwargs)
示例3: multisearch
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def multisearch(*models, **params):
ms = MultiSearch(using=es.client, index=es.index_name)
queries = []
for model in models:
s = search_for(model, **params)
ms = ms.add(s._s)
queries.append(s)
responses = ms.execute()
return [
# _d_ is the only way to access the raw data
# allowing to rewrap response in a FacetedSearch
# because default multisearch loose facets
SearchResult(query, response._d_)
for query, response in zip(queries, responses)
]
示例4: __init__
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def __init__(self):
self.field_counts = {}
self.multi_search = MultiSearch()
示例5: _run_multisearch
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def _run_multisearch(es, searches):
"""Ejecuta una lista de búsquedas Elasticsearch utilizando la función
MultiSearch. La cantidad de búsquedas que se envían a la vez es
configurable vía la variable ES_MULTISEARCH_MAX_LEN.
Args:
es (Elasticsearch): Conexión a Elasticsearch.
searches (list): Lista de elasticsearch_dsl.Search.
Raises:
DataConnectionException: Si ocurrió un error al ejecutar las búsquedas.
Returns:
list: Lista de respuestas a cada búsqueda.
"""
step_size = constants.ES_MULTISEARCH_MAX_LEN
responses = []
# Partir las búsquedas en varios baches si es necesario.
for i in range(0, len(searches), step_size):
end = min(i + step_size, len(searches))
ms = MultiSearch(using=es)
for j in range(i, end):
ms = ms.add(searches[j])
try:
responses.extend(ms.execute(raise_on_error=True))
except elasticsearch.ElasticsearchException as e:
raise DataConnectionException() from e
return responses
示例6: get_multi_search
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def get_multi_search(self):
multi_search = MultiSearch()
search = self.get_search()
multi_search = multi_search.add(search)
if self.args.get(constants.PARAM_AGGREGATIONS) is not None:
multi_search = self.add_terms_aggregations(multi_search)
return multi_search
示例7: test_no_querystring_is_valid
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def test_no_querystring_is_valid(self):
query = FieldSearchQuery(args={})
with mock.patch.object(MultiSearch, 'execute', return_value=get_mock_search()):
result = query.execute()
self.assertFalse(result.get('errors'))
示例8: test_query_response_size
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def test_query_response_size(self):
query = FieldSearchQuery(args={'q': 'aceite'})
with mock.patch.object(MultiSearch, 'execute', return_value=get_mock_search()):
result = query.execute()
self.assertEqual(len(result['data']), result['count'])
示例9: test_response_params
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def test_response_params(self):
limit = '10'
offset = '15'
query = FieldSearchQuery(args={'q': 'aceite',
'limit': limit,
'start': offset})
with mock.patch.object(MultiSearch, 'execute', return_value=get_mock_search()):
result = query.execute()
self.assertEqual(result['limit'], int(limit))
self.assertEqual(result['start'], int(offset))
示例10: run_searches
# 需要導入模塊: import elasticsearch_dsl [as 別名]
# 或者: from elasticsearch_dsl import MultiSearch [as 別名]
def run_searches(es, searches):
"""Ejecuta una lista de búsquedas ElasticsearchSearch.
Para ejecutar las búsquedas, se obtiene un iterador de búsquedas
elasticsearch_dsl.Search por cada elemento de 'searches'. Utilizando
los iteradores, se construyen listas de elasticsearch_dsl.Search, que
son luego ejecutadas utilizando '_run_multisearch'. Después, los
resultados son devueltos a cada iterador, que pueden o no generar una
nueva búsqueda elasticsearch_dsl.Search. El proceso se repite hasta que
todos los iteradores hayan finalizado. Con todo este proceso se logra:
1) Ejecutar cualquier tipo de búsquedas bajo una mismas interfaz.
2) Ejecutar búsquedas que requieren distintas cantides de pasos
bajo una misma interfaz.
3) Utilizar la funcionalidad de MultiSearch para hacer la menor
cantidad de consultas posible a Elasticsearch.
Los resultados de cada búsqueda pueden ser accedidos vía el campo
'.result' de cada una.
Args:
es (Elasticsearch): Conexión a Elasticsearch.
searches (list): Lista de búsquedas ElasticsearchSearch o
derivados. La lista puede ser de cualquier largo ya que sus
contenidos son fraccionados por '_run_multisearch' para evitar
consultas demasiado extensas a Elasticsearch.
"""
iterators = [search.search_steps() for search in searches]
iteration_data = []
for iterator in iterators:
search = utils.step_iterator(iterator)
if search:
iteration_data.append((iterator, search))
while iteration_data:
responses = _run_multisearch(es, [
search for _, search in iteration_data
])
iterators = (iterator for iterator, _ in iteration_data)
iteration_data = []
for iterator, response in zip(iterators, responses):
search = utils.step_iterator(iterator, response)
if search:
iteration_data.append((iterator, search))