Skip to content

Commit

Permalink
add internal support for passing options in elasticsearch operations …
Browse files Browse the repository at this point in the history
…(related to #45)
  • Loading branch information
alexgarel committed Sep 22, 2020
1 parent ba617c8 commit e25e1f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 7 additions & 4 deletions luqum/elasticsearch/tree.py
Expand Up @@ -210,17 +210,20 @@ 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)
return "%s(%s)" % (self.__class__.__name__, items)

@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):
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions 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,
}},
)

0 comments on commit e25e1f0

Please sign in to comment.