Skip to content

Commit

Permalink
Improve values handling in index and deindex methods (fixes #794)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienVH-c2c committed Aug 17, 2023
1 parent f63bb1d commit 7b59e06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions addok/helpers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ def index(pipe, key, doc, tokens, **kwargs):
if callable(boost):
boost = boost(doc)
boost = boost + importance
if not isinstance(values, (list, tuple)):
if isinstance(values, str):
values = [values]
if not all(isinstance(item, str) for item in values):
raise ValueError("{} must not be a string or an array of strings".format(name))
for value in values:
extract_tokens(tokens, str(value), boost=boost)
extract_tokens(tokens, value, boost=boost)
index_tokens(pipe, tokens, key, **kwargs)

@staticmethod
Expand All @@ -139,8 +141,10 @@ def deindex(db, key, doc, tokens, **kwargs):
continue
values = doc.get(name)
if values:
if not isinstance(values, (list, tuple)):
if isinstance(values, str):
values = [values]
if not all(isinstance(item, str) for item in values):
raise ValueError("{} must not be a string or an array of strings".format(name))
for value in values:
tokens.extend(deindex_field(key, value))

Expand Down
18 changes: 18 additions & 0 deletions tests/test_index_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ def test_doc_with_null_value_should_not_be_index_if_not_allowed(config):
index_document(doc)
assert not DB.exists("w|cergy")


def test_doc_with_array_of_array_value_should_not_be_index_if_not_allowed(config):
config.FIELDS = [
{"key": "name", "null": False},
{"key": "city"},
]
doc = {
"id": "xxxx",
"_id": "yyyy",
"lat": "49.32545",
"lon": "4.2565",
"name": [["Lilas"]],
"city": "Cergy",
}
index_document(doc)
assert not DB.exists("w|cergy")


def test_create_edge_ngrams(config):
config.MIN_EDGE_NGRAMS = 2
config.INDEX_EDGE_NGRAMS = False
Expand Down

0 comments on commit 7b59e06

Please sign in to comment.