Python elasticsearch_dsl.MultiSearch() Examples

The following are 10 code examples of elasticsearch_dsl.MultiSearch(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module elasticsearch_dsl , or try the search function .
Example #1
Source File: es_query.py    From series-tiempo-ar-api with MIT License 6 votes vote down vote up
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])
        } 
Example #2
Source File: es_search.py    From seqr with GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example #3
Source File: __init__.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
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)
    ] 
Example #4
Source File: multi_search_conductor.py    From texta with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.field_counts = {}
        self.multi_search = MultiSearch() 
Example #5
Source File: data.py    From georef-ar-api with MIT License 5 votes vote down vote up
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 
Example #6
Source File: query.py    From series-tiempo-ar-api with MIT License 5 votes vote down vote up
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 
Example #7
Source File: query_tests.py    From series-tiempo-ar-api with MIT License 5 votes vote down vote up
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')) 
Example #8
Source File: query_tests.py    From series-tiempo-ar-api with MIT License 5 votes vote down vote up
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']) 
Example #9
Source File: query_tests.py    From series-tiempo-ar-api with MIT License 5 votes vote down vote up
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)) 
Example #10
Source File: data.py    From georef-ar-api with MIT License 4 votes vote down vote up
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))