11
11
MultiMatch ,
12
12
Nested ,
13
13
SimpleQueryString ,
14
+ Term ,
14
15
Wildcard ,
15
16
)
16
17
@@ -35,12 +36,23 @@ class RTDFacetedSearch(FacetedSearch):
35
36
'post_tags' : ['</span>' ],
36
37
}
37
38
38
- def __init__ (self , query = None , filters = None , user = None , use_advanced_query = True , ** kwargs ):
39
+ def __init__ (
40
+ self ,
41
+ query = None ,
42
+ filters = None ,
43
+ projects = None ,
44
+ user = None ,
45
+ use_advanced_query = True ,
46
+ ** kwargs ,
47
+ ):
39
48
"""
40
49
Pass in a user in order to filter search results by privacy.
41
50
42
- If `use_advanced_query` is `True`,
43
- force to always use `SimpleQueryString` for the text query object.
51
+ :param projects: A dictionary of project slugs mapped to a `VersionData` object.
52
+ Results are filter with these values.
53
+
54
+ :param use_advanced_query: If `True` forces to always use
55
+ `SimpleQueryString` for the text query object.
44
56
45
57
.. warning::
46
58
@@ -50,6 +62,7 @@ def __init__(self, query=None, filters=None, user=None, use_advanced_query=True,
50
62
self .user = user
51
63
self .filter_by_user = kwargs .pop ('filter_by_user' , True )
52
64
self .use_advanced_query = use_advanced_query
65
+ self .projects = projects or {}
53
66
54
67
# Hack a fix to our broken connection pooling
55
68
# This creates a new connection on every request,
@@ -259,7 +272,12 @@ def total_count(self):
259
272
return s .hits .total
260
273
261
274
def query (self , search , query ):
262
- """Manipulates the query to support nested queries and a custom rank for pages."""
275
+ """
276
+ Manipulates the query to support nested queries and a custom rank for pages.
277
+
278
+ If `self.projects` was given, we use it to filter the documents that
279
+ match the same project and version.
280
+ """
263
281
search = search .highlight_options (** self ._highlight_options )
264
282
265
283
queries = self ._get_queries (
@@ -280,8 +298,22 @@ def query(self, search, query):
280
298
)
281
299
282
300
queries .extend ([sections_nested_query , domains_nested_query ])
301
+ bool_query = Bool (should = queries )
302
+
303
+ if self .projects :
304
+ versions_query = [
305
+ Bool (
306
+ must = [
307
+ Term (project = {'value' : project }),
308
+ Term (version = {'value' : version }),
309
+ ]
310
+ )
311
+ for project , version in self .projects .items ()
312
+ ]
313
+ bool_query = Bool (must = [bool_query , Bool (should = versions_query )])
314
+
283
315
final_query = FunctionScore (
284
- query = Bool ( should = queries ) ,
316
+ query = bool_query ,
285
317
script_score = self ._get_script_score (),
286
318
)
287
319
search = search .query (final_query )
0 commit comments