Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Allow for multiple terms in facet_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Aug 21, 2013
1 parent 1eff92a commit 076f9a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 5 additions & 8 deletions monolith/client/__init__.py
Expand Up @@ -116,16 +116,13 @@ def __call__(self, field, start, end, interval=DAY, strict_range=False,
}
}

if len(terms) > 0:
term = {}

for key, value in terms.items():
term[key] = value
if terms:

range_ = query['facets']['histo1']['facet_filter']['range']
filter_ = {'and': [{'term': term},
{'range': range_}]}
query['facets']['histo1']['facet_filter'] = filter_

query['facets']['histo1']['facet_filter'] = {
'and': ([{'term': {k: v}} for k, v in terms.items()] +
[{'range': range_}])}

with self.statsd.timer('elasticsearch-query'):
res = self.session.post(self.es, data=json.dumps(query))
Expand Down
8 changes: 8 additions & 0 deletions monolith/client/tests/test_client.py
Expand Up @@ -28,6 +28,7 @@ def setUp(self):
'date': '2012-%.2d-01' % j,
'downloads_count': j,
'add_on': str(j % 2),
'is_something': True,
})
self.es_client.refresh()
self.server.wait()
Expand Down Expand Up @@ -76,6 +77,13 @@ def test_monthly(self):
interval='month', add_on='2'))
self.assertNotEqual(hits, hits2)

def test_multiple_terms(self):
client = self._make_one()
hits = list(client('downloads_count', start, '2012-05-01',
interval='month', add_on=1, is_something=True))
self.assertEqual(len(hits), 5)
self.assertEqual(len([h for h in hits if h['count']]), 2)

def test_global_daily_strict(self):
client = self._make_one()
hits = list(client('downloads_count', start, end, strict_range=True))
Expand Down

0 comments on commit 076f9a5

Please sign in to comment.