Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Lucene and Elasticsearch Boolean operations #71

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions luqum/elasticsearch/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ class EMustNot(AbstractEMustOperation):
operation = 'must_not'


class EBoolOperation(EOperation):

@property
def json(self):
must_items = []
should_items = []
must_not_items = []
for item in self.items:
if isinstance(item, EMust):
must_items.extend(item.items)
elif isinstance(item, EMustNot):
must_not_items.extend(item.items)
else:
should_items.append(item)
bool_query = {}
if must_items:
bool_query["must"] = [item.json for item in must_items]
if should_items:
bool_query["should"] = [item.json for item in should_items]
if must_not_items:
bool_query["must_not"] = [item.json for item in must_not_items]

query = dict(bool_query, **self.options)
return {'bool': query}


class ElasticSearchItemFactory:
"""
Factory to preconfigure EItems and EOperation
Expand Down
5 changes: 4 additions & 1 deletion luqum/elasticsearch/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from luqum.tree import Word # noqa: F401
from .tree import (
EMust, EMustNot, EShould, EWord, EPhrase, ERange,
ENested)
ENested, EBoolOperation)
from ..check import CheckNestedFields
from ..naming import get_name
from ..utils import (
Expand Down Expand Up @@ -342,6 +342,9 @@ def visit_prohibit(self, *args, **kwargs):
def visit_plus(self, *args, **kwargs):
yield from self._must_operation(*args, **kwargs)

def visit_bool_operation(self, *args, **kwargs):
yield from self._binary_operation(EBoolOperation, *args, **kwargs)

def visit_unknown_operation(self, *args, **kwargs):
if self.default_operator == self.SHOULD:
yield from self._should_operation(*args, **kwargs)
Expand Down
15 changes: 15 additions & 0 deletions luqum/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ def children(self, value):
self.operands = tuple(value)


class BoolOperation(BaseOperation):
"""Lucene Boolean Query.

This operation assumes that the query builder can utilize a boolean operator
with three possible sections, must, should and must_not. If the
UnknownOperationResolver is asked to resolve_to this operation, the query
builder can utilize this operator directly instead of nested AND/OR.
This also makes it possible to correctly support Lucene queries such as:
"apples +bananas -vegetables"
.. seealso::
the :py:class:`.utils.UnknownOperationResolver`
"""
op = ""


class UnknownOperation(BaseOperation):
"""Unknown Boolean operator.

Expand Down
8 changes: 4 additions & 4 deletions luqum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
from . import visitor
from .deprecated_utils import ( # noqa: F401
LuceneTreeTransformer, LuceneTreeVisitor, LuceneTreeVisitorV2)
from .tree import AndOperation, BaseOperation, OrOperation
from .tree import AndOperation, BaseOperation, OrOperation, BoolOperation


class UnknownOperationResolver(visitor.TreeTransformer):
"""Transform the UnknownOperation to OR or AND
"""

VALID_OPERATIONS = frozenset([None, AndOperation, OrOperation])
VALID_OPERATIONS = frozenset([None, AndOperation, OrOperation, BoolOperation])
DEFAULT_OPERATION = AndOperation

def __init__(self, resolve_to=None, add_head=" "):
"""Initialize a new resolver

:param resolve_to: must be either None, OrOperation or AndOperation.
:param resolve_to: must be either None, OrOperation, AndOperation, BoolOperation.

for the latter two the UnknownOperation is repalced by specified operation.
for the latter three the UnknownOperation is replaced by specified operation.

if it is None, we use the last operation encountered, as would Lucene do
"""
Expand Down
25 changes: 24 additions & 1 deletion tests/test_elasticsearch/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from luqum.tree import (
AndOperation, Word, Prohibit, OrOperation, Not, Phrase, SearchField,
UnknownOperation, Boost, Fuzzy, Proximity, Range, Group, FieldGroup,
Plus)
Plus, BoolOperation)
from luqum.elasticsearch.tree import ElasticSearchItemFactory
from luqum.elasticsearch.visitor import EWord, ElasticsearchQueryBuilder

Expand Down Expand Up @@ -64,6 +64,29 @@ def test_should_transform_or(self):
]}}
self.assertDictEqual(result, expected)

def test_bool_transform_bool(self):
tree = BoolOperation(
Word("a"),
Word("b"),
Group(BoolOperation(Plus(Word('f')), Plus(Word('g')))),
Prohibit(Group(BoolOperation(Word("c"), Word("d")))),
Plus(Word('e')))
result = self.transformer(tree)
expected = {'bool': {
'must': [
{'term': {'text': {'value': 'e'}}}],
'should': [
{"term": {"text": {"value": 'a'}}},
{"term": {"text": {"value": 'b'}}},
{'bool': {'must': [
{'term': {'text': {"value": 'f'}}},
{'term': {'text': {"value": 'g'}}}]}}],
'must_not': [{"bool": {"should": [
{"term": {"text": {"value": 'c'}}},
{"term": {"text": {"value": 'd'}}}]}}],
}}
self.assertDictEqual(result, expected)

def test_should_raise_when_or_and_and_on_same_level(self):
tree = OrOperation(
Word('spam'),
Expand Down
15 changes: 14 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import TestCase

from luqum.parser import parser
from luqum.tree import Group, Word, AndOperation, OrOperation, UnknownOperation
from luqum.tree import (Group, Word, AndOperation, OrOperation, BoolOperation,
UnknownOperation, Prohibit, Plus)
from luqum.utils import UnknownOperationResolver


Expand Down Expand Up @@ -49,6 +50,18 @@ def test_lucene_resolution_simple(self):
resolver = UnknownOperationResolver(resolve_to=None)
self.assertEqual(resolver(tree), expected)

def test_lucene_resolution_bool(self):
tree = parser.parse("a b (+f +g) -(c d) +e")
expected = (
BoolOperation(
Word("a"),
Word("b"),
Group(BoolOperation(Plus(Word("f")), Plus(Word("g")))),
Prohibit(Group(BoolOperation(Word("c"), Word("d")))),
Plus(Word('e'))))
resolver = UnknownOperationResolver(resolve_to=BoolOperation)
self.assertEqual(resolver(tree), expected)

def test_lucene_resolution_last_op(self):
tree = (
OrOperation(
Expand Down