def test_query_filter(app, user_factory): """Test post filter.""" urlargs = MultiDict() defs = dict( type=terms_filter('type'), subtype=terms_filter('subtype'), ) with app.test_request_context("?type=test"): q = Query("value") body = q.body['query'] query, args = _query_filter(q, urlargs, defs) assert 'post_filter' not in query.body assert query.body['query']['filtered']['query'] == body assert query.body['query']['filtered']['filter'] == \ dict( bool=dict( filter=[dict(terms=dict(type=['test']))] ), ) assert args['type'] == 'test' with app.test_request_context("?anotertype=test"): q = Query("value") body = q.body['query'] query, args = _query_filter(q, urlargs, defs) assert query.body['query'] == body
def cap_facets_factory(search, index): """Add a cap facets to query. :param search: Basic search object. :param index: Index name. :returns: A tuple containing the new search object and a dictionary with all fields and values used. """ urlkwargs = MultiDict() facets = current_app.config['RECORDS_REST_FACETS'].get(index) if facets is not None: # Aggregations. search = _aggregations(search, facets.get("aggs", {}), urlkwargs, facets.get("post_filters", {})) # Query filter search, urlkwargs = _query_filter(search, urlkwargs, facets.get("filters", {})) # Post filter search, urlkwargs = _post_filter(search, urlkwargs, facets.get("post_filters", {})) return (search, urlkwargs)
def cernopendata_facets_factory(search, index): """Add a cernopendata facets to query. :param search: Basic search object. :param index: Index name. :returns: A tuple containing the new search object and a dictionary with all fields and values used. """ urlkwargs = MultiDict() facets = current_app.config['RECORDS_REST_FACETS'].get(index) if facets is not None: # Aggregations. search = _aggregations(search, facets.get("aggs", {}), urlkwargs, facets.get("post_filters", {})) # Query filter search, urlkwargs = _query_filter( search, urlkwargs, facets.get("filters", {})) # Post filter search, urlkwargs = _post_filter( search, urlkwargs, facets.get("post_filters", {})) return (search, urlkwargs)
def inspire_filter_factory(search, index): urlkwargs = MultiDict() facets = get_facet_configuration(index) if facets is not None: search, urlkwargs = _query_filter(search, urlkwargs, facets.get("filters", {})) search, urlkwargs = _post_filter( search, urlkwargs, facets.get("post_filters", {}) ) return (search, urlkwargs)
def default_inspire_facets_factory(search, index): urlkwargs = MultiDict() facets = get_facet_configuration(index) if facets: search = _aggregations(search, facets.get("aggs", {})) search, urlkwargs = _query_filter(search, urlkwargs, facets.get("filters", {})) search, urlkwargs = _post_filter(search, urlkwargs, facets.get("post_filters", {})) return search, urlkwargs
def default_inspire_facets_factory(search, index): urlkwargs = MultiDict() facets = get_facet_configuration(index) if facets: search = _aggregations(search, facets.get("aggs", {})) search, urlkwargs = _query_filter( search, urlkwargs, facets.get("filters", {})) search, urlkwargs = _post_filter( search, urlkwargs, facets.get("post_filters", {})) return search, urlkwargs
def test_query_filter(app): """Test post filter.""" urlargs = MultiDict() defs = dict( type=terms_filter('type'), subtype=terms_filter('subtype'), ) with app.test_request_context('?type=test'): search = Search().query(Q('multi_match', query='value')) body = search.to_dict() search, args = _query_filter(search, urlargs, defs) assert 'post_filter' not in search.to_dict() assert search.to_dict()['query']['bool']['must'][0] == body['query'] assert search.to_dict()['query']['bool']['filter'] == [ dict(terms=dict(type=['test'])) ] assert args['type'] == 'test' with app.test_request_context('?anotertype=test'): search = Search().query(Q(query='value')) body = search.to_dict() query, args = _query_filter(search, urlargs, defs) assert query.to_dict() == body
def deposit_facets_factory(search, index): """Replace default search factory to use custom facet generator.""" urlkwargs = MultiDict() facets = current_app.config['RECORDS_REST_FACETS'].get(index) if facets is not None: # Aggregations. search = _aggregations(search, facets.get("aggs", {})) # Query filter search, urlkwargs = _query_filter(search, urlkwargs, facets.get("filters", {})) # Post filter search, urlkwargs = _post_filter(search, urlkwargs, facets.get("post_filters", {})) return (search, urlkwargs)
def inspire_filter_factory(search, urlkwargs, search_index): """Copies behaviour of default facets factory but without the aggregations, As facets factory is also responsible for filtering the year and author (invenio mess) Args: search: Elastic search DSL search instance. urlkwargs: search_index: index name Returns: tuple with search and urlarguments """ facets = get_facet_configuration(search_index) # Query filter search, urlkwargs = _query_filter(search, urlkwargs, facets.get("filters", {})) # Post filter search, urlkwargs = _post_filter(search, urlkwargs, facets.get("post_filters", {})) return search, urlkwargs
def inspire_filter_factory(search, urlkwargs, search_index): """Copies behaviour of default facets factory but without the aggregations, As facets factory is also responsible for filtering the year and author (invenio mess) Args: search: Elastic search DSL search instance. urlkwargs: search_index: index name Returns: tuple with search and urlarguments """ facets = get_facet_configuration(search_index) # Query filter search, urlkwargs = _query_filter( search, urlkwargs, facets.get("filters", {})) # Post filter search, urlkwargs = _post_filter( search, urlkwargs, facets.get("post_filters", {})) return search, urlkwargs