Skip to content

Commit

Permalink
Fix corner cases when filter value is None
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Nov 29, 2023
1 parent b718f18 commit 49a2c01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from elasticsearch import Elasticsearch, helpers # type: ignore[import-not-found]
from haystack import default_from_dict, default_to_dict
from haystack.dataclasses import Document
from haystack.document_stores.decorator import document_store
from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError
from haystack.document_stores.protocols import DuplicatePolicy
from haystack.document_stores import document_store, DocumentStoreError, DuplicateDocumentError, DuplicatePolicy

from elasticsearch_haystack.filters import _normalize_filters

Expand Down
30 changes: 30 additions & 0 deletions integrations/elasticsearch/src/elasticsearch_haystack/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]:


def _equal(field: str, value: Any) -> Dict[str, Any]:
if value is None:
return {"bool": {"must_not": {"exists": {"field": field}}}}

if isinstance(value, list):
return {
"terms_set": {
Expand All @@ -58,6 +61,9 @@ def _equal(field: str, value: Any) -> Dict[str, Any]:


def _not_equal(field: str, value: Any) -> Dict[str, Any]:
if value is None:
return {"exists": {"field": field}}

if isinstance(value, list):
return {"bool": {"must_not": {"terms": {field: value}}}}
if field in ["text", "dataframe"]:
Expand All @@ -68,6 +74,12 @@ def _not_equal(field: str, value: Any) -> Dict[str, Any]:


def _greater_than(field: str, value: Any) -> Dict[str, Any]:
if value is None:
# When the value is None and '>' is used we create a filter that would return a Document
# if it has a field set and not set at the same time.
# This will cause the filter to match no Document.
# This way we keep the behavior consistent with other Document Stores.
return {"bool": {"must": [{"exists": {"field": field}}, {"bool": {"must_not": {"exists": {"field": field}}}}]}}
if isinstance(value, str):
try:
datetime.fromisoformat(value)
Expand All @@ -84,6 +96,12 @@ def _greater_than(field: str, value: Any) -> Dict[str, Any]:


def _greater_than_equal(field: str, value: Any) -> Dict[str, Any]:
if value is None:
# When the value is None and '>=' is used we create a filter that would return a Document
# if it has a field set and not set at the same time.
# This will cause the filter to match no Document.
# This way we keep the behavior consistent with other Document Stores.
return {"bool": {"must": [{"exists": {"field": field}}, {"bool": {"must_not": {"exists": {"field": field}}}}]}}
if isinstance(value, str):
try:
datetime.fromisoformat(value)
Expand All @@ -100,6 +118,12 @@ def _greater_than_equal(field: str, value: Any) -> Dict[str, Any]:


def _less_than(field: str, value: Any) -> Dict[str, Any]:
if value is None:
# When the value is None and '<' is used we create a filter that would return a Document
# if it has a field set and not set at the same time.
# This will cause the filter to match no Document.
# This way we keep the behavior consistent with other Document Stores.
return {"bool": {"must": [{"exists": {"field": field}}, {"bool": {"must_not": {"exists": {"field": field}}}}]}}
if isinstance(value, str):
try:
datetime.fromisoformat(value)
Expand All @@ -116,6 +140,12 @@ def _less_than(field: str, value: Any) -> Dict[str, Any]:


def _less_than_equal(field: str, value: Any) -> Dict[str, Any]:
if value is None:
# When the value is None and '<=' is used we create a filter that would return a Document
# if it has a field set and not set at the same time.
# This will cause the filter to match no Document.
# This way we keep the behavior consistent with other Document Stores.
return {"bool": {"must": [{"exists": {"field": field}}, {"bool": {"must_not": {"exists": {"field": field}}}}]}}
if isinstance(value, str):
try:
datetime.fromisoformat(value)
Expand Down

0 comments on commit 49a2c01

Please sign in to comment.