Skip to content

Commit

Permalink
Merge pull request #53 from lhannest/knowledge_beacon
Browse files Browse the repository at this point in the history
Knowledge beacon
  • Loading branch information
cmungall committed Jun 30, 2017
2 parents bad1244 + 436a8de commit 360f2df
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions ontobio/golr/golr_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ def flip(d, x, y):

def solr_quotify(v):
if isinstance(v, list):
return '({})'.format(" OR ".join([solr_quotify(x) for x in v]))
if len(v) == 1:
return solr_quotify(v[0])
else:
return '({})'.format(" OR ".join([solr_quotify(x) for x in v]))
else:
# TODO - escape quotes
return '"{}"'.format(v)
Expand Down Expand Up @@ -434,6 +437,7 @@ def __init__(self,
object_category=None,
relation=None,
subject_or_object_ids=None,
subject_or_object_category=None,
subject=None,
subjects=None,
object=None,
Expand Down Expand Up @@ -481,6 +485,7 @@ def __init__(self,
self.object_category=object_category
self.relation=relation
self.subject_or_object_ids=subject_or_object_ids
self.subject_or_object_category=subject_or_object_category
self.subject=subject
self.subjects=subjects
self.object=object
Expand Down Expand Up @@ -756,17 +761,26 @@ def solr_params(self):
filter_queries = [ '{}:{}'.format(k,solr_quotify(v)) for (k,v) in fq.items()]

# We want to match all associations that have either a subject or object
# with an ID that is contained in subject_or_object_ids, and sort them by the number
# of hits to the ID's in subject_or_object_ids
boost_function = None
# with an ID that is contained in subject_or_object_ids.
if subject_or_object_ids is not None:
summands = ['termfreq(subject_closure, ' + c + ')' \
'termfreq(object_closure, ' + c + ')' for c in subject_or_object_ids]
boost_function = 'prod(sum('+ ','.join(summands) + '), 10)'
disjunction = " OR ".join(['"' + c + '"' for c in subject_or_object_ids])
disjunctive_query = 'subject_closure:(' + disjunction + ')' \
' OR object_closure:(' + disjunction + ')'
filter_queries.append(disjunctive_query.strip())
quotified_ids = solr_quotify(subject_or_object_ids)
subject_id_filter = '{}:{}'.format('subject', quotified_ids)
object_id_filter = '{}:{}'.format('object', quotified_ids)

# If subject_or_object_category is provided, we add it to the filter.
if self.subject_or_object_category is not None:
quotified_categories = solr_quotify(self.subject_or_object_category)
subject_category_filter = '{}:{}'.format('subject_category', quotified_categories)
object_category_filter = '{}:{}'.format('object_category', quotified_categories)

filter_queries.append(
'(' + subject_id_filter + ' AND ' + object_category_filter + ')' \
' OR ' \
'(' + object_id_filter + ' AND ' + subject_category_filter + ')'
)

else:
filter_queries.append(subject_id_filter + ' OR ' + object_id_filter)

# unless caller specifies a field list, use default
if select_fields is None:
Expand Down Expand Up @@ -829,10 +843,6 @@ def solr_params(self):
'rows': rows
}

# This enables us to sort the queries that are returned
if (boost_function != None):
params['bf'] = boost_function

if self.start is not None:
params['start'] = self.start

Expand Down

0 comments on commit 360f2df

Please sign in to comment.