Skip to content

Commit

Permalink
Always conjunct CINERGI query items
Browse files Browse the repository at this point in the history
By default, the CINERGI query items would be ORd, thus expanding
the search as more terms were specified instead of narrowing it
down. By always ANDing the terms, we ensure that the more
information a user provides, the narrower the search becomes.
  • Loading branch information
rajadain committed Sep 18, 2017
1 parent 9edc2d8 commit df3aa2f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/mmw/apps/bigcz/clients/cinergi/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ def prepare_time(from_date, to_date):
return value


def prepare_query(query):
"""
Prepare query to always AND all search terms
Spaces will be replaced with AND. Existing ANDs and ORs will be preserved.
"""
def intersperse_and(phrase):
# For a given phrase, intersperse AND between all words,
# except "and" which is removed
return ' AND '.join([word for word in phrase.split()
if word != 'and'])

# If the query has any ORs, split it into phrases to be ANDed
phrases = query.split(' or ')

# AND words in each phrase, and OR all phrases together
return ' OR '.join(map(intersperse_and, phrases))


def search(**kwargs):
query = kwargs.get('query')
to_date = kwargs.get('to_date')
Expand All @@ -134,7 +153,7 @@ def search(**kwargs):

if query:
params.update({
'q': query
'q': prepare_query(query.lower())
})
if from_date:
params.update({
Expand Down

0 comments on commit df3aa2f

Please sign in to comment.