Skip to content

Commit 4f0f973

Browse files
authored
[core][fix] History search with sort and fulltext terms (#2146)
1 parent ea2c97f commit 4f0f973

File tree

6 files changed

+14
-19
lines changed

6 files changed

+14
-19
lines changed

fixcore/fixcore/db/arango_query.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def history_query(db: Any, query_model: QueryModel) -> Tuple[str, Json]:
130130
ctx = ArangoQueryContext()
131131
query = rewrite_query(query_model)
132132
start = f"`{db.name}_node_history`"
133-
cursor, query_str = query_string(db, query, query_model, start, False, ctx, id_column="id")
133+
cursor, query_str = query_string(
134+
db, query, query_model, start, False, ctx, id_column="id", use_fulltext_index=False
135+
)
134136
last_limit = f" LIMIT {ll.offset}, {ll.length}" if (ll := query.current_part.limit) else ""
135137
return f"""{query_str} FOR result in {cursor}{last_limit} RETURN UNSET(result, {unset_props})""", ctx.bind_vars
136138

@@ -302,7 +304,7 @@ def view_term(term: Term) -> Tuple[Optional[str], Term]:
302304
else:
303305
ctx.bind_vars.clear()
304306

305-
cursor, query_str = query_string(db, query, query_model, start_cursor, with_edges, ctx)
307+
cursor, query_str = query_string(db, query, query_model, start_cursor, with_edges, ctx, use_fulltext_index=False)
306308
return cursor, qs + query_str
307309

308310

@@ -340,6 +342,7 @@ def query_string(
340342
*,
341343
outer_merge: Optional[str] = None,
342344
id_column: str = "_key",
345+
use_fulltext_index: bool = True,
343346
) -> Tuple[str, str]:
344347
# Note: the parts are maintained in reverse order
345348
query_parts = query.parts[::-1]
@@ -646,6 +649,7 @@ def add_merge_query(mq: MergeQuery, part_result: str) -> None:
646649
ctx,
647650
outer_merge=merge_crsr,
648651
id_column=id_column,
652+
use_fulltext_index=use_fulltext_index,
649653
)
650654
if mq.only_first:
651655
merge_result += (
@@ -960,7 +964,7 @@ def ft_term(cursor: str, ab_term: Term) -> str:
960964
return q, crs
961965

962966
parts = []
963-
ft, remaining = fulltext_term_combine(query_parts[0].term)
967+
ft, remaining = fulltext_term_combine(query_parts[0].term) if use_fulltext_index else (None, query_parts[0].term)
964968
fulltext_part, crsr = fulltext(ft, remaining) if ft else ("", start_cursor)
965969
for idx, p in enumerate(query_parts):
966970
part_tuple = part(p, crsr, idx)

fixcore/fixcore/db/arango_query_rewrite.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
CombinedTerm,
1515
IsTerm,
1616
NotTerm,
17-
IdTerm,
1817
)
1918

2019

@@ -76,12 +75,6 @@ def change_term(term: Term) -> Term:
7675
def rewrite_query(
7776
query_model: QueryModel,
7877
) -> Query:
79-
q = query_model.query
80-
81-
# remove sort if id is used
82-
if isinstance(q.current_part.term, IdTerm):
83-
q = q.change_current_part(lambda p: evolve(p, sort=[]))
84-
query_model = evolve(query_model, query=q)
8578

8679
# try to add an IsTerm if not already provided
8780
q = add_is_term(query_model)

fixcore/fixcore/db/graphdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ async def search_history(
744744
more_than_one = len(query.query.parts) > 1
745745
has_invalid_terms = any(query.query.find_terms(lambda t: isinstance(t, (FulltextTerm, MergeTerm))))
746746
has_navigation = any(p.navigation for p in query.query.parts)
747-
if more_than_one and has_invalid_terms or has_navigation:
747+
if more_than_one or has_invalid_terms or has_navigation:
748748
raise AttributeError("Fulltext, merge terms and navigation is not supported in history queries!")
749749
# adjust query
750750
term = query.query.current_part.term

fixcore/fixcore/model/model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,9 @@ def for_complex_kind(
13741374
Property("exported_at", "datetime", False, None, "datetime when the account was exported."),
13751375
Property("exported_age", "trafo.duration_to_datetime", False, SyntheticProperty(["exported_at"])),
13761376
Property("provider_link", "string", False, None, "Link to the provider of this resource."),
1377+
# History entries
1378+
Property("change", "string", False, None, "Kind of change."),
1379+
Property("changed_at", "datetime", False, None, "Time of change."),
13771380
],
13781381
metadata={"dynamic": True},
13791382
),

fixcore/tests/fixcore/db/arango_query_rewrite_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
from fixcore.query.query_parser import parse_query
55

66

7-
def test_id_rewrite(person_model: Model) -> None:
8-
q = rewrite_query(QueryModel(parse_query("id(test) sort foo, bla, bar"), person_model))
9-
assert str(q) == 'id("test")'
10-
11-
127
def test_is_rewrite(person_model: Model) -> None:
138
q = rewrite_query(QueryModel(parse_query("reported.city = Gotham"), person_model))
149
assert str(q) == '(is("Address") and reported.city == "Gotham")'

fixcore/tests/fixcore/model/model_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def test_filter_model(person_model: Model) -> None:
594594

595595
def test_complete_path(person_model: Model) -> None:
596596
count, all_props = person_model.complete_path("", "", fuzzy=False, skip=0, limit=4)
597-
assert count == 22 # total
597+
assert count == 24 # total
598598
assert all_props == {"address": "Address", "addresses[*]": "Address", "age": "duration", "any": "any"}
599599
# filter by prefix
600600
count, all_props = person_model.complete_path("", "ad", fuzzy=False, skip=0, limit=4)
@@ -604,10 +604,10 @@ def test_complete_path(person_model: Model) -> None:
604604
assert ap == {"address": "Address", "addresses[*]": "Address", "other_addresses": "dictionary[string, Address]"}
605605
# filter by prefix fuzzy
606606
count, ap = person_model.complete_path("", "t", fuzzy=True, skip=0, limit=4)
607-
assert ap == {"tags": "dictionary[string, string]", "test": "string", "city": "string", "ctime": "datetime"}
607+
assert ap == {"tags": "dictionary[string, string]", "test": "string", "city": "string", "changed_at": "datetime"}
608608
# skip the first 4
609609
count, all_props = person_model.complete_path("", "", fuzzy=False, skip=4, limit=4)
610-
assert all_props == {"city": "string", "ctime": "datetime", "expires": "datetime", "exported_age": "duration"}
610+
assert all_props == {"change": "string", "changed_at": "datetime", "city": "string", "ctime": "datetime"}
611611
# ask for a nested kind
612612
count, all_props = person_model.complete_path("address", "", fuzzy=False, skip=4, limit=4)
613613
assert all_props == {"mtime": "datetime", "tags": "dictionary[string, string]", "zip": "string"}

0 commit comments

Comments
 (0)