Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions annotator/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 6 additions & 10 deletions annotator/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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}}
}


Expand Down