Ejemplo n.º 1
0
def search_for_types(search_types, search_term, limit, lang):
    """Get results for all given types.
    """
    if not search_types:
        return {}

    document_id = try_to_parse_document_id(search_term)

    if document_id is not None:
        # search by document id for every type
        results_for_type = [([document_id], None)] * len(search_types)
    else:
        # search in ElasticSearch
        results_for_type = do_multi_search_for_types(
            search_types, search_term, limit, lang)

    # load the documents using the document ids returned from the search
    results = {}
    for search_type, result_for_type in zip(search_types, results_for_type):
        (key, get_documents_config) = search_type
        (document_ids, total) = result_for_type

        def search_documents(_, __):
            return document_ids, total

        results[key] = get_documents(
            get_documents_config, {'lang': lang}, search_documents)

    return results
Ejemplo n.º 2
0
    def _collection_get(self, doc_type, documents_config):
        validated = self.request.validated
        meta_params = {
            'offset': validated.get('offset', 0),
            'limit': min(validated.get('limit', LIMIT_DEFAULT), LIMIT_MAX),
            'lang': validated.get('lang')
        }

        if meta_params['offset'] + meta_params['limit'] > ES_MAX_RESULT_WINDOW:
            # ES does not process requests where offset + limit is greater
            # than 10000, see:
            # https://www.elastic.co/guide/en/elasticsearch/reference/master/search-request-from-size.html
            raise HTTPBadRequest('offset + limit greater than 10000')

        if advanced_search.contains_search_params(self.request.GET):
            # search with ElasticSearch
            search_documents = advanced_search.get_search_documents(
                self.request.GET, meta_params, doc_type)
        else:
            # if no search parameters, directly load documents from the db
            search_documents = partial(
                self._search_documents_paginated, meta_params)

        return get_documents(
            documents_config, meta_params, search_documents)
Ejemplo n.º 3
0
def search_for_types(search_types, search_term, limit, lang):
    """Get results for all given types.
    """
    if not search_types:
        return {}

    document_id = try_to_parse_document_id(search_term)

    if document_id is not None:
        # search by document id for every type
        results_for_type = [([document_id], None)] * len(search_types)
    else:
        # search in ElasticSearch
        results_for_type = do_multi_search_for_types(search_types, search_term,
                                                     limit, lang)

    # load the documents using the document ids returned from the search
    results = {}
    for search_type, result_for_type in zip(search_types, results_for_type):
        (key, get_documents_config) = search_type
        (document_ids, total) = result_for_type

        def search_documents(_, __):
            return document_ids, total

        results[key] = get_documents(get_documents_config, {'lang': lang},
                                     search_documents)

    return results
Ejemplo n.º 4
0
    def _collection_get(self, doc_type, documents_config):
        validated = self.request.validated
        meta_params = {
            'offset': validated.get('offset', 0),
            'limit': min(validated.get('limit', LIMIT_DEFAULT), LIMIT_MAX),
            'lang': validated.get('lang')
        }

        if advanced_search.contains_search_params(self.request.GET):
            # search with ElasticSearch
            search_documents = advanced_search.get_search_documents(
                self.request.GET, meta_params, doc_type)
        else:
            # if no search parameters, directly load documents from the db
            search_documents = partial(
                self._search_documents_paginated, meta_params)

        return get_documents(
            documents_config, meta_params, search_documents)