From 8f6d83bb6ca7503e97a614fe726bb215e8774523 Mon Sep 17 00:00:00 2001 From: Jeff Balogh Date: Mon, 27 Jun 2011 17:19:00 -0700 Subject: [PATCH] support multiple queries and range queries --- apps/amo/search.py | 4 +++- apps/amo/tests/test_search.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/apps/amo/search.py b/apps/amo/search.py index 527043463c..925bff0d99 100644 --- a/apps/amo/search.py +++ b/apps/amo/search.py @@ -119,7 +119,7 @@ class ES(object): qs['filter'] = filters[0] if len(queries) > 1: - raise NotImplementedError() + qs['query'] = {'bool': {'must': queries}} elif queries: qs['query'] = queries[0] @@ -163,6 +163,8 @@ class ES(object): rv.append({'term': {key: val}}) elif field_action == 'startswith': rv.append({'prefix': {key: val}}) + elif field_action in ('gt', 'gte', 'lt', 'lte'): + rv.append({'range': {key: {field_action: val}}}) return rv def _do_search(self): diff --git a/apps/amo/tests/test_search.py b/apps/amo/tests/test_search.py index b897b33032..8425190b89 100644 --- a/apps/amo/tests/test_search.py +++ b/apps/amo/tests/test_search.py @@ -45,6 +45,14 @@ class TestES(amo.tests.ESTestCase): eq_(qs._build_query(), {'fields': ['id'], 'query': {'term': {'type': 1}}}) + def test_query_multiple_and_range(self): + qs = Addon.search().query(type=1, status__gte=1) + eq_(qs._build_query(), {'fields': ['id'], + 'query': {'bool': {'must': [ + {'term': {'type': 1}}, + {'range': {'status': {'gte': 1}}}, + ]}}}) + def test_order_by_desc(self): qs = Addon.search().order_by('-rating') eq_(qs._build_query(), {'fields': ['id'],