Skip to content
This repository has been archived by the owner on May 22, 2022. It is now read-only.

Commit

Permalink
Search: Expand subreddit search query for /r/subreddit
Browse files Browse the repository at this point in the history
When searching subreddits, detect a search for /r/subreddit and expand
the search query to include the subreddit name as a search term.

For example, searching for "/r/aww" expands the results to include
"/r/aww" and "aww". This helps ensure that searching for "/r/aww"
actually includes /r/aww.
  • Loading branch information
Florence Yeun committed May 14, 2015
1 parent a56fc06 commit 26b6ae0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions r2/r2/lib/providers/search/cloudsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,16 @@ def run(self, _update=False):
return self.results

def _parse(self):
query = self.preprocess_query(self.query)

if self.syntax == "cloudsearch":
self.bq = self.customize_query(self.query)
self.bq = self.customize_query(query)
elif self.syntax == "lucene":
bq = l2cs.convert(self.query, self.lucene_parser)
self.converted_data = {"syntax": "cloudsearch",
"converted": bq}
bq = l2cs.convert(query, self.lucene_parser)
self.converted_data = {"syntax": "cloudsearch", "converted": bq}
self.bq = self.customize_query(bq)
elif self.syntax == "plain":
self.q = self.query.encode('utf-8')
self.q = query.encode('utf-8')
self.bq = self.customize_query()

if not self.q and not self.bq:
Expand All @@ -620,6 +621,9 @@ def _run(self, _update=False):
self.rank_expressions, self.faceting,
start=self.start, num=self.num, _update=_update)

def preprocess_query(self, query):
return query

def customize_query(self, bq=u''):
return bq

Expand Down Expand Up @@ -799,6 +803,13 @@ class CloudSearchSubredditSearchQuery(CloudSearchQuery):
known_syntaxes = ("plain",)
default_syntax = "plain"

def preprocess_query(self, query):
# Expand search for /r/subreddit to include subreddit name.
sr = query.strip('/').split('/')
if len(sr) == 2 and sr[0] == 'r' and Subreddit.is_valid_name(sr[1]):
query = '"%s" | %s' % (query, sr[1])
return query

def customize_query(self, bq=u''):
queries = []
if bq:
Expand Down

0 comments on commit 26b6ae0

Please sign in to comment.