diff --git a/luqum/elasticsearch/tree.py b/luqum/elasticsearch/tree.py index 9392c55..ce0465f 100644 --- a/luqum/elasticsearch/tree.py +++ b/luqum/elasticsearch/tree.py @@ -210,9 +210,10 @@ class EOperation(AbstractEOperation): Abstract operation taking care of the json build """ - def __init__(self, items): + def __init__(self, items, **options): self.items = items self._method = None + self.options = options def __repr__(self): items = ", ".join(i.__repr__() for i in self.items) @@ -220,7 +221,9 @@ def __repr__(self): @property def json(self): - return {'bool': {self.operation: [item.json for item in self.items]}} + bool_query = {self.operation: [item.json for item in self.items]} + query = dict(bool_query, **self.options) + return {'bool': query} class ENested(AbstractEOperation): @@ -313,8 +316,8 @@ class EShould(EOperation): class AbstractEMustOperation(EOperation): - def __init__(self, items): - op = super().__init__(items) + def __init__(self, items, **options): + op = super().__init__(items, **options) for item in self.items: item.zero_terms_query = self.zero_terms_query return op diff --git a/tests/test_elasticsearch/test_estree.py b/tests/test_elasticsearch/test_estree.py new file mode 100644 index 0000000..1b6d79b --- /dev/null +++ b/tests/test_elasticsearch/test_estree.py @@ -0,0 +1,20 @@ +from unittest import TestCase + +from luqum.elasticsearch.tree import EShould, EWord + + +class TestItems(TestCase): + + def test_should_operation_options(self): + op = EShould(items=[EWord(q="a"), EWord(q="b"), EWord(q="c")], minimum_should_match=2) + self.assertEqual( + op.json, + {'bool': { + 'should': [ + {'term': {'': {'value': 'a'}}}, + {'term': {'': {'value': 'b'}}}, + {'term': {'': {'value': 'c'}}}, + ], + 'minimum_should_match': 2, + }}, + )