Skip to content

Commit dc23b28

Browse files
committed
Documentation tweaks
1 parent 2618132 commit dc23b28

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ With the low-level client you would write something like this:
5454
for hit in response['hits']['hits']:
5555
print(hit['_score'], hit['_source']['title'])
5656
57-
Which would be very hard to modify (imagine adding another filter to that
57+
Which could be very hard to modify (imagine adding another filter to that
5858
query) and is definitely no fun to write. With the python DSL you can write the
5959
same query as:
6060

@@ -85,7 +85,7 @@ Or, if you want to have absolute control over your queries:
8585
8686
The library will take care of:
8787

88-
* composing queries/filters into ``bool`` queries/filters
88+
* composing queries/filters into compound queries/filters
8989

9090
* creating filtered queries when ``.filter()`` has been used
9191

@@ -106,10 +106,10 @@ with it and, at the end, serialize it back to dict to send over the wire:
106106
107107
body = {...} # insert complicated query here
108108
# convert to search
109-
s = Search.from_dict()
109+
s = Search.from_dict(body)
110110
# add some filters, aggregations, queries, ...
111111
s.filter("term", tags="python")
112-
# convert back to dict to plug back into existing code
112+
# optionally convert back to dict to plug back into existing code
113113
body = s.to_dict()
114114
115115
Since the DSL is built on top of the low-level client there should be nothing

elasticsearch_dsl/search.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ def __getitem__(self, n):
9292
9393
Slicing equates to the from/size parameters. E.g.::
9494
95-
Search().query(...)[0:25]
95+
s = Search().query(...)[0:25]
9696
9797
is equivalent to::
9898
99-
{"query": ...
100-
"from": 0, "size": 25}
99+
s = Search().query(...).extra(from_=0, size=25)
101100
102101
"""
103102
s = self._clone()
@@ -130,6 +129,10 @@ def from_dict(cls, d):
130129
return s
131130

132131
def _clone(self):
132+
"""
133+
Return a clone of the current search request. Performs a shallow copy
134+
of all the underlying objects. Used internally by most state modifying APIs.
135+
"""
133136
s = Search(using=self._using, index=self._index, doc_type=self._doc_type)
134137
s._sort = self._sort[:]
135138
s._extra = self._extra.copy()
@@ -143,6 +146,10 @@ def _clone(self):
143146
return s
144147

145148
def update_from_dict(self, d):
149+
"""
150+
Apply options from a serialized body to the current instance. Modifies
151+
the object in-place.
152+
"""
146153
d = d.copy()
147154
self.query._proxied = Q(d.pop('query'))
148155
if 'post_filter' in d:
@@ -163,6 +170,10 @@ def update_from_dict(self, d):
163170
self._extra = d
164171

165172
def params(self, **kwargs):
173+
"""
174+
Specify query params to be used when executing the search. All the
175+
keyword arguments will override the current values.
176+
"""
166177
s = self._clone()
167178
s._params.update(kwargs)
168179
return s
@@ -181,13 +192,13 @@ def sort(self, *keys):
181192
"""
182193
Add sorting information to the search request. If called without
183194
arguments it will remove all sort requirements. Otherwise it will
184-
replace them. Acceptable arguments are:
195+
replace them. Acceptable arguments are::
185196
186197
'some.field'
187198
'-some.other.fiels'
188199
{'different.field': {'any': 'dict'}}
189200
190-
so for example:
201+
so for example::
191202
192203
s = Search().sort(
193204
'category',
@@ -225,6 +236,11 @@ def index(self, *index):
225236
return s
226237

227238
def doc_type(self, *doc_type):
239+
"""
240+
Set the type to search through. You can supply a single value or a
241+
list. If no index is supplied (or an empty value) any information
242+
stored on the instance will be erased.
243+
"""
228244
# .doc_type() resets
229245
s = self._clone()
230246
if not doc_type:
@@ -233,7 +249,13 @@ def doc_type(self, *doc_type):
233249
s._doc_type = (self._doc_type or []) + list(doc_type)
234250
return s
235251

236-
def to_dict(self, count=False, **kwargs):
252+
def to_dict(self, count=False):
253+
"""
254+
Serialize the search into the dictionary that will be sent over as the request's body.
255+
256+
:arg count: a flag to specify we are interested in a body for count -
257+
no aggregations, no pagination bounds etc.
258+
"""
237259
if self.filter:
238260
d = {
239261
"query": {
@@ -257,15 +279,24 @@ def to_dict(self, count=False, **kwargs):
257279

258280
if not count:
259281
d.update(self._extra)
260-
d.update(kwargs)
261282
return d
262283

263284
def using(self, client):
285+
"""
286+
Associate the search request with an elasticsearch client. A fresh copy
287+
will be returned with current instance remaining unchanged.
288+
289+
:arg client: and instance of ``elasticsearch.Elasticsearch`` to use
290+
"""
264291
s = self._clone()
265292
s._using = client
266293
return s
267294

268295
def count(self):
296+
"""
297+
Return the number of hits matching the query and filters. Note that
298+
only the actual number is returned.
299+
"""
269300
if not self._using:
270301
raise #XXX
271302

@@ -278,6 +309,10 @@ def count(self):
278309
)['count']
279310

280311
def execute(self):
312+
"""
313+
Execute the search and return an instance of ``Response`` wrapping all
314+
the data.
315+
"""
281316
if not self._using:
282317
raise #XXX
283318

0 commit comments

Comments
 (0)