Skip to content

Commit 19a6ebc

Browse files
Fix groupless search (#118)
* fix(search): 🐛 Search across null group_ids * chore: Version bump * chore: Set group_ids to none if it's an empty list * fix: Check for group ids being a list before setting it to None if empty * fix check * chore: Simplify group_ids check * chore: Simplify the check further
1 parent d7c20c1 commit 19a6ebc

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

graphiti_core/search/search.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ async def search(
6161
) -> SearchResults:
6262
start = time()
6363
query = query.replace('\n', ' ')
64-
64+
# if group_ids is empty, set it to None
65+
group_ids = group_ids if group_ids else None
6566
edges = (
6667
await edge_search(
6768
driver, embedder, query, group_ids, config.edge_config, center_node_uuid, config.limit

graphiti_core/search/search_utils.py

+36-14
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ async def edge_fulltext_search(
6565
group_ids: list[str | None] | None = None,
6666
limit=RELEVANT_SCHEMA_LIMIT,
6767
) -> list[EntityEdge]:
68-
group_ids = group_ids if group_ids is not None else [None]
69-
7068
# fulltext search over facts
7169
cypher_query = Query("""
7270
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
7371
YIELD relationship AS rel, score
7472
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
75-
WHERE r.group_id IN $group_ids
73+
WHERE CASE
74+
WHEN $group_ids IS NULL THEN n.group_id IS NULL
75+
ELSE n.group_id IN $group_ids
76+
END
7677
RETURN
7778
r.uuid AS uuid,
7879
r.group_id AS group_id,
@@ -94,7 +95,10 @@ async def edge_fulltext_search(
9495
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
9596
YIELD relationship AS rel, score
9697
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity)
97-
WHERE r.group_id IN $group_ids
98+
WHERE CASE
99+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
100+
ELSE r.group_id IN $group_ids
101+
END
98102
RETURN
99103
r.uuid AS uuid,
100104
r.group_id AS group_id,
@@ -115,7 +119,10 @@ async def edge_fulltext_search(
115119
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
116120
YIELD relationship AS rel, score
117121
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
118-
WHERE r.group_id IN $group_ids
122+
WHERE CASE
123+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
124+
ELSE r.group_id IN $group_ids
125+
END
119126
RETURN
120127
r.uuid AS uuid,
121128
r.group_id AS group_id,
@@ -136,7 +143,10 @@ async def edge_fulltext_search(
136143
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
137144
YIELD relationship AS rel, score
138145
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity)
139-
WHERE r.group_id IN $group_ids
146+
WHERE CASE
147+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
148+
ELSE r.group_id IN $group_ids
149+
END
140150
RETURN
141151
r.uuid AS uuid,
142152
r.group_id AS group_id,
@@ -177,13 +187,15 @@ async def edge_similarity_search(
177187
group_ids: list[str | None] | None = None,
178188
limit: int = RELEVANT_SCHEMA_LIMIT,
179189
) -> list[EntityEdge]:
180-
group_ids = group_ids if group_ids is not None else [None]
181190
# vector similarity search over embedded facts
182191
query = Query("""
183192
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
184193
YIELD relationship AS rel, score
185194
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
186-
WHERE r.group_id IN $group_ids
195+
WHERE CASE
196+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
197+
ELSE r.group_id IN $group_ids
198+
END
187199
RETURN
188200
r.uuid AS uuid,
189201
r.group_id AS group_id,
@@ -205,7 +217,10 @@ async def edge_similarity_search(
205217
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
206218
YIELD relationship AS rel, score
207219
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity)
208-
WHERE r.group_id IN $group_ids
220+
WHERE CASE
221+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
222+
ELSE r.group_id IN $group_ids
223+
END
209224
RETURN
210225
r.uuid AS uuid,
211226
r.group_id AS group_id,
@@ -226,7 +241,10 @@ async def edge_similarity_search(
226241
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
227242
YIELD relationship AS rel, score
228243
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
229-
WHERE r.group_id IN $group_ids
244+
WHERE CASE
245+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
246+
ELSE r.group_id IN $group_ids
247+
END
230248
RETURN
231249
r.uuid AS uuid,
232250
r.group_id AS group_id,
@@ -247,7 +265,10 @@ async def edge_similarity_search(
247265
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
248266
YIELD relationship AS rel, score
249267
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity)
250-
WHERE r.group_id IN $group_ids
268+
WHERE CASE
269+
WHEN $group_ids IS NULL THEN r.group_id IS NULL
270+
ELSE r.group_id IN $group_ids
271+
END
251272
RETURN
252273
r.uuid AS uuid,
253274
r.group_id AS group_id,
@@ -284,15 +305,16 @@ async def node_fulltext_search(
284305
group_ids: list[str | None] | None = None,
285306
limit=RELEVANT_SCHEMA_LIMIT,
286307
) -> list[EntityNode]:
287-
group_ids = group_ids if group_ids is not None else [None]
288-
289308
# BM25 search to get top nodes
290309
fuzzy_query = re.sub(r'[^\w\s]', '', query) + '~'
291310
records, _, _ = await driver.execute_query(
292311
"""
293312
CALL db.index.fulltext.queryNodes("name_and_summary", $query)
294313
YIELD node AS n, score
295-
MATCH (n WHERE n.group_id in $group_ids)
314+
WHERE CASE
315+
WHEN $group_ids IS NULL THEN n.group_id IS NULL
316+
ELSE n.group_id IN $group_ids
317+
END
296318
RETURN
297319
n.uuid AS uuid,
298320
n.group_id AS group_id,

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "graphiti-core"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
description = "A temporal graph building library"
55
authors = [
66
"Paul Paliychuk <[email protected]>",

0 commit comments

Comments
 (0)