Skip to content

Commit 35b3713

Browse files
authored
Merge pull request #3 from macbre/allow-fields-list-to-be-provided
wikia_common_kibana | allow to provide a list of fields to be returned by elastic
2 parents 62dcbee + 0369d27 commit 35b3713

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ Returns data matching the given query.
3434
.. code-block:: python
3535
3636
source.query_by_string(query='@message:"^PHP Fatal"', limit=2000)
37+
source.query_by_string(query='@message:"^PHP Fatal"', fields=['@message', '@source_host'], limit=2000)
3738
3839
Returns data matching the given query string.
3940

4041
`query`: query string to be run against Kibana log messages (ex. @message:"^PHP Fatal").
42+
`fields`: optional list of fields to fetch
4143
`limit`: the number of results (defaults to 10).
4244

4345
.. code-block:: python

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup
22

3-
VERSION = '2.2.4'
3+
VERSION = '2.2.5'
44

55
# @see https://github.com/pypa/sampleproject/blob/master/setup.py
66
setup(
@@ -21,6 +21,6 @@
2121
},
2222
install_requires=[
2323
"elasticsearch>=6.0.0,<7.0.0",
24-
"python-dateutil==2.2",
24+
"python-dateutil==2.7.2",
2525
]
2626
)

wikia_common_kibana.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ def _get_timestamp_filer(self):
120120
}
121121
}
122122

123-
def _search(self, query, limit=50000, sampling=None):
123+
def _search(self, query, fields=None, limit=50000, sampling=None):
124124
"""
125125
Perform the search and return raw rows
126126
127127
:type query object
128+
:type fields list[str] or None
128129
:type limit int
129130
:type sampling int or None
130131
@@ -142,6 +143,12 @@ def _search(self, query, limit=50000, sampling=None):
142143
}
143144
}
144145

146+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
147+
if fields:
148+
body['_source'] = {
149+
"includes": fields
150+
}
151+
145152
# add @timestamp range
146153
# @see http://stackoverflow.com/questions/40996266/elasticsearch-5-1-unknown-key-for-a-start-object-in-filters
147154
# @see https://discuss.elastic.co/t/elasticsearch-watcher-error-for-range-query/70347/2
@@ -184,11 +191,12 @@ def _search(self, query, limit=50000, sampling=None):
184191
self._logger.info("{:d} rows returned".format(len(rows)))
185192
return rows
186193

187-
def get_rows(self, match, limit=10, sampling=None):
194+
def get_rows(self, match, fields=None, limit=10, sampling=None):
188195
"""
189196
Returns raw rows that matches given query
190197
191198
:arg match: query to be run against Kibana log messages (ex. {"@message": "Foo Bar DB queries"})
199+
:type fields list[str] or None
192200
:arg limit: the number of results (defaults to 10)
193201
:type sampling int or None
194202
:arg sampling: Percentage of results to be returned (0,100)
@@ -197,13 +205,14 @@ def get_rows(self, match, limit=10, sampling=None):
197205
"match": match,
198206
}
199207

200-
return self._search(query, limit, sampling)
208+
return self._search(query, fields, limit, sampling)
201209

202-
def query_by_string(self, query, limit=10, sampling=None):
210+
def query_by_string(self, query, fields=None, limit=10, sampling=None):
203211
"""
204212
Returns raw rows that matches the given query string
205213
206214
:arg query: query string to be run against Kibana log messages (ex. @message:"^PHP Fatal").
215+
:type fields list[str] or None
207216
:arg limit: the number of results (defaults to 10)
208217
:type sampling int or None
209218
:arg sampling: Percentage of results to be returned (0,100)
@@ -214,7 +223,7 @@ def query_by_string(self, query, limit=10, sampling=None):
214223
}
215224
}
216225

217-
return self._search(query, limit, sampling)
226+
return self._search(query, fields, limit, sampling)
218227

219228
def get_to_timestamp(self):
220229
""" Return the upper time boundary to returned data """

0 commit comments

Comments
 (0)