Skip to content

Commit 29609c1

Browse files
committed
docstrings for facetedsearch
1 parent 547c14a commit 29609c1

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

elasticsearch_dsl/faceted_search.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@
1111
__all__ = ['FacetedSearch', 'HistogramFacet', 'TermsFacet', 'DateHistogramFacet']
1212

1313
class Facet(object):
14+
"""
15+
A facet on faceted search. Wraps and aggregation and provides functionality
16+
to create a filter for selected values and return a list of facet values
17+
from the result of the aggregation.
18+
"""
1419
agg_type = None
20+
1521
def __init__(self, **kwargs):
1622
self.filter_values = ()
1723
self._params = kwargs
1824

1925
def get_aggregation(self):
26+
"""
27+
Return the aggregation object.
28+
"""
2029
return A(self.agg_type, **self._params)
2130

2231
def add_filter(self, filter_values):
32+
"""
33+
Construct a filter and remember the values for use in get_values.
34+
"""
2335
self.filter_values = filter_values
2436

2537
if not filter_values:
@@ -31,22 +43,36 @@ def add_filter(self, filter_values):
3143
return f
3244

3345
def get_value_filter(self, filter_value):
46+
"""
47+
Construct a filter for an individual value
48+
"""
3449
pass
3550

36-
def is_filtered(self, key, bucket):
51+
def is_filtered(self, key):
52+
"""
53+
Is a filter active on the given key.
54+
"""
3755
return key in self.filter_values
3856

3957
def get_value(self, bucket):
58+
"""
59+
return a value representing a bucket. Its key as default.
60+
"""
4061
return bucket['key']
4162

4263
def get_values(self, data):
64+
"""
65+
Turn the raw bucket data into a list of tuples containing the key,
66+
number of documents and a flag indicating whether this value has been
67+
selected or not.
68+
"""
4369
out = []
4470
for bucket in data:
4571
key = self.get_value(bucket)
4672
out.append((
4773
key,
4874
bucket['doc_count'],
49-
self.is_filtered(key, self.filter_values)
75+
self.is_filtered(key)
5076
))
5177
return out
5278

@@ -55,6 +81,8 @@ class TermsFacet(Facet):
5581
agg_type = 'terms'
5682

5783
def add_filter(self, filter_values):
84+
""" Create a terms filter instead of bool containing term filters. """
85+
5886
self.filter_values = filter_values
5987

6088
if filter_values:
@@ -126,19 +154,26 @@ def __init__(self, query=None, filters={}):
126154
self.add_filter(name, value)
127155

128156
def add_filter(self, name, filter_values):
157+
"""
158+
Add a filter for a facet.
159+
"""
160+
# normalize the value into a list
129161
if not isinstance(filter_values, (tuple, list)):
130162
if filter_values in (None, ''):
131163
return
132164
filter_values = [filter_values, ]
133165

134-
166+
# get the filter from the facet
135167
f = self.facets[name].add_filter(filter_values)
136168
if f is None:
137169
return
138170

139171
self._filters[name] = f
140172

141173
def search(self):
174+
"""
175+
Construct the Search object.
176+
"""
142177
return Search(doc_type=self.doc_types, index=self.index)
143178

144179
def query(self, search, query):

0 commit comments

Comments
 (0)