Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: reddit/reddit
base: 4208ea8
...
head fork: reddit/reddit
compare: 034aeef022b2d0ca9fad7675a7b9a5a784a810a1
  • 5 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
26 r2/r2/controllers/api.py
@@ -59,7 +59,7 @@
from r2.lib.filters import safemarkdown
from r2.lib.scraper import str_to_image
from r2.controllers.api_docs import api_doc, api_section
-from r2.lib.cloudsearch import basic_query
+from r2.lib.search import SearchQuery
import csv
from collections import defaultdict
@@ -2785,7 +2785,7 @@ def POST_disable_otp(self, form, jquery):
c.user._commit()
form.redirect("/prefs/otp")
- @json_validate(query=VPrintable("query", max_length=50))
+ @json_validate(query=VLength("query", max_length=50))
@api_doc(api_section.subreddits, extensions=["json"])
def GET_subreddits_by_topic(self, responder, query):
if not g.CLOUDSEARCH_SEARCH_API:
@@ -2796,25 +2796,17 @@ def GET_subreddits_by_topic(self, responder, query):
exclude = Subreddit.default_subreddits()
- q = basic_query(query,
- facets={"reddit":{"sort":"-sum(text_relevance)", "count":20}},
- record_stats=True)
- if not q["facets"]:
- return []
-
- sr_facets = [f["value"] for f in q["facets"]["reddit"]["constraints"]]
- srs = Subreddit._by_name(sr_facets)
+ faceting = {"reddit":{"sort":"-sum(text_relevance)", "count":20}}
+ results = SearchQuery(query, sort="relevance", faceting=faceting).run()
- results = []
- for sr_name in sr_facets:
- sr = srs.get(sr_name)
+ sr_results = []
+ for sr, count in results.subreddit_facets:
if (sr._id in exclude or (sr.over_18 and not c.over18)
- or not sr.can_view(c.user)
or sr.type == "archived"):
continue
- results.append({
- "name": sr_name,
+ sr_results.append({
+ "name": sr.name,
})
- return results
+ return sr_results
View
47 r2/r2/lib/cloudsearch.py
@@ -496,11 +496,18 @@ def subreddit_facets(self):
if not self._subreddits and 'reddit' in self._facets:
sr_facets = [(sr['value'], sr['count']) for sr in
self._facets['reddit']]
- srs_by_name = Subreddit._by_name([sr[0] for sr in sr_facets])
- self._subreddits = [sr for sr in sr_facets
- if sr[0] in srs_by_name and
- srs_by_name[sr[0]].can_view(c.user)]
-
+
+ # look up subreddits
+ srs_by_name = Subreddit._by_name([name for name, count
+ in sr_facets])
+
+ sr_facets = [(srs_by_name[name], count) for name, count
+ in sr_facets if name in srs_by_name]
+
+ # filter by can_view
+ self._subreddits = [(sr, count) for sr, count in sr_facets
+ if sr.can_view(c.user)]
+
return self._subreddits
@@ -509,12 +516,14 @@ def subreddit_facets(self):
'CS-IncorrectFieldTypeInMatchExpression',
'CS-InvalidMatchSetExpression',)
DEFAULT_FACETS = {"reddit": {"count":20}}
-def basic_query(query=None, bq=None, facets=DEFAULT_FACETS, size=1000,
+def basic_query(query=None, bq=None, faceting=None, size=1000,
start=0, rank="-relevance", return_fields=None, record_stats=False,
search_api=None):
if search_api is None:
search_api = g.CLOUDSEARCH_SEARCH_API
- path = _encode_query(query, bq, facets, size, start, rank, return_fields)
+ if faceting is None:
+ faceting = DEFAULT_FACETS
+ path = _encode_query(query, bq, faceting, size, start, rank, return_fields)
timer = None
if record_stats:
timer = g.stats.get_timer("cloudsearch_timer")
@@ -557,7 +566,7 @@ def basic_query(query=None, bq=None, facets=DEFAULT_FACETS, size=1000,
basic_subreddit = functools.partial(basic_query,
- facets=None,
+ faceting=None,
size=10, start=0,
rank="-activity",
return_fields=['title', 'reddit',
@@ -566,7 +575,7 @@ def basic_query(query=None, bq=None, facets=DEFAULT_FACETS, size=1000,
search_api=g.CLOUDSEARCH_SUBREDDIT_SEARCH_API)
-def _encode_query(query, bq, facets, size, start, rank, return_fields):
+def _encode_query(query, bq, faceting, size, start, rank, return_fields):
if not (query or bq):
raise ValueError("Need query or bq")
params = {}
@@ -578,9 +587,9 @@ def _encode_query(query, bq, facets, size, start, rank, return_fields):
params["size"] = size
params["start"] = start
params["rank"] = rank
- if facets:
- params["facet"] = ",".join(facets.iterkeys())
- for facet, options in facets.iteritems():
+ if faceting:
+ params["facet"] = ",".join(faceting.iterkeys())
+ for facet, options in faceting.iteritems():
params["facet-%s-top-n" % facet] = options.get("count", 20)
if "sort" in options:
params["facet-%s-sort" % facet] = options["sort"]
@@ -600,7 +609,8 @@ class CloudSearchQuery(object):
default_syntax = "plain"
lucene_parser = None
- def __init__(self, query, sr=None, sort=None, syntax=None, raw_sort=None):
+ def __init__(self, query, sr=None, sort=None, syntax=None, raw_sort=None,
+ faceting=None):
if syntax is None:
syntax = self.default_syntax
elif syntax not in self.known_syntaxes:
@@ -614,6 +624,7 @@ def __init__(self, query, sr=None, sort=None, syntax=None, raw_sort=None):
self.sort = raw_sort
else:
self.sort = self.sorts[sort]
+ self.faceting = faceting
self.bq = ''
self.results = None
@@ -644,8 +655,8 @@ def _run(self, start=0, num=1000, _update=False):
q = self.query
if g.sqlprinting:
g.log.info("%s", self)
- return self._run_cached(q, self.bq, self.sort, start=start, num=num,
- _update=_update)
+ return self._run_cached(q, self.bq, self.sort, self.faceting,
+ start=start, num=num, _update=_update)
def customize_query(self, bq):
return bq
@@ -663,8 +674,8 @@ def __repr__(self):
return ''.join(result)
@classmethod
- def _run_cached(cls, query, bq, sort="relevance", start=0, num=1000,
- _update=False):
+ def _run_cached(cls, query, bq, sort="relevance", faceting=None, start=0,
+ num=1000, _update=False):
'''Query the cloudsearch API. _update parameter allows for supposed
easy memoization at later date.
@@ -701,7 +712,7 @@ def _run_cached(cls, query, bq, sort="relevance", start=0, num=1000,
'''
response = basic_query(query=query, bq=bq, size=num, start=start,
rank=sort, search_api=cls.search_api,
- record_stats=True)
+ faceting=faceting, record_stats=True)
warnings = response['info'].get('messages', [])
for warning in warnings:
View
5 r2/r2/templates/searchbar.html
@@ -78,10 +78,9 @@ <h4 style="color:gray">${thing.header}</h4>
<div class="searchfacets">
<h4 class="title">${_("too many results? narrow it down to a subreddit!")}</h4>
<ol class="list">
- %for facet in thing.facets:
- <% subreddit, count = facet %>
+ %for subreddit, count in thing.facets:
<li class="searchfacet reddit">
- <a class="facet title word" href="${search_url(thing.prev_search, subreddit, restrict_sr='on', sort=thing.sort)}">/r/${subreddit}</a>&nbsp;
+ <a class="facet title word" href="${search_url(thing.prev_search, subreddit.name, restrict_sr='on', sort=thing.sort)}">/r/${subreddit.name}</a>&nbsp;
<span class="facet count number">(${count})</span>
</li>&nbsp;
%endfor

No commit comments for this range

Something went wrong with that request. Please try again.