Skip to content

Commit a2e6731

Browse files
committed
allows for filtered (partial) searches.
This is the same as coderanger#76 which has not been merged since Sep 5, 2017 :(
1 parent 216cbb2 commit a2e6731

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

chef/search.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,46 @@ class Search(collections.Sequence):
3636
The only required argument is the index name to search (eg. node, role, etc).
3737
The second, optional argument can be any Solr search query, with the same semantics
3838
as Chef.
39+
The third optional parameter can be a dictionary containing the parameters for a filtered
40+
search (partial search); see https://docs.chef.io/api_chef_server.html#id76
3941
4042
Example::
4143
4244
for row in Search('node', 'roles:app'):
4345
print row['roles']
4446
print row.object.name
47+
48+
# or
49+
50+
filters = {
51+
'name': ['name'],
52+
'ip': ['ipaddress'],
53+
'kernel_version': ['kernel', 'version'],
54+
}
55+
56+
for row in Search('node', 'roles:app', filters):
57+
print row['data']['name']
58+
print row['data']['kernel_version']
4559
4660
.. versionadded:: 0.1
4761
"""
4862

4963
url = '/search'
5064

51-
def __init__(self, index, q='*:*', rows=1000, start=0, api=None):
65+
def __init__(self, index, q='*:*', rows=1000, start=0, api=None, filter_result=None):
5266
self.name = index
5367
self.api = api or ChefAPI.get_global()
5468
self._args = dict(q=q, rows=rows, start=start)
69+
self.filter_result = filter_result
5570
self.url = self.__class__.url + '/' + self.name + '?' + six.moves.urllib.parse.urlencode(self._args)
5671

5772
@property
5873
def data(self):
5974
if not hasattr(self, '_data'):
60-
self._data = self.api[self.url]
75+
if self.filter_result is None:
76+
self._data = self.api.api_request('GET', self.url)
77+
else:
78+
self._data = self.api.api_request('POST', self.url, data=self.filter_result)
6179
return self._data
6280

6381
@property

0 commit comments

Comments
 (0)