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,
@@ -280,8 +293,22 @@ def query(self, search, query):
280
293
)
281
294
282
295
queries .extend ([sections_nested_query , domains_nested_query ])
296
+ bool_query = Bool (should = queries )
297
+
298
+ if self .projects :
299
+ versions_query = [
300
+ Bool (
301
+ must = [
302
+ Term (project = {'value' : project }),
303
+ Term (version = {'value' : version }),
304
+ ]
305
+ )
306
+ for project , version in self .projects .items ()
307
+ ]
308
+ bool_query = Bool (must = [bool_query , Bool (should = versions_query )])
309
+
283
310
final_query = FunctionScore (
284
- query = Bool ( should = queries ) ,
311
+ query = bool_query ,
285
312
script_score = self ._get_script_score (),
286
313
)
287
314
search = search .query (final_query )
0 commit comments