diff --git a/annotator/annotation.py b/annotator/annotation.py index 31e09af..33f6da7 100644 --- a/annotator/annotation.py +++ b/annotator/annotation.py @@ -113,18 +113,20 @@ def _build_query(cls, query=None, offset=None, limit=None, # attempt to expand query to include uris for other representations # using information we may have on hand about the Document if 'uri' in query: - term_filter = q['query']['filtered']['filter'] + clauses = q['query']['bool'] doc = document.Document.get_by_uri(query['uri']) if doc: - new_terms = [] - for term in term_filter['and']: - if 'uri' in term['term']: - term = {'or': []} + for clause in clauses['must']: + # Rewrite the 'uri' clause to match any of the document URIs + if 'match' in clause and 'uri' in clause['match']: + uri_matchers = [] for uri in doc.uris(): - term['or'].append({'term': {'uri': uri}}) - new_terms.append(term) - - term_filter['and'] = new_terms + uri_matchers.append({'match': {'uri': uri}}) + del clause['match'] + clause['bool'] = { + 'should': uri_matchers, + 'minimum_should_match': 1 + } if es.authorization_enabled: # Apply a filter to the results. diff --git a/annotator/elasticsearch.py b/annotator/elasticsearch.py index b284fdd..d6b8a5c 100644 --- a/annotator/elasticsearch.py +++ b/annotator/elasticsearch.py @@ -231,16 +231,12 @@ def _csv_split(s, delimiter=','): def _build_query(query, offset, limit): - # Base query is a filtered match_all - q = {'match_all': {}} + # Create a match query for each keyword + match_clauses = [{'match': {k: v}} for k, v in iteritems(query)] - if query: - f = {'and': []} - q = {'filtered': {'query': q, 'filter': f}} - - # Add a term query for each keyword - for k, v in iteritems(query): - q['filtered']['filter']['and'].append({'term': {k: v}}) + if len(match_clauses) == 0: + # Elasticsearch considers an empty conjunction to be false.. + match_clauses.append({'match_all': {}}) return { 'sort': [{'updated': { @@ -255,7 +251,7 @@ def _build_query(query, offset, limit): }}], 'from': max(0, offset), 'size': min(RESULTS_MAX_SIZE, max(0, limit)), - 'query': q + 'query': {'bool': {'must': match_clauses}} }