Skip to content

Commit 7747de1

Browse files
authored
fix: add full text search for neo4j db (#1095) (#1099)
* feat: add full_text_search for neo4j * test: 改回去 --------- ## Description Please include a summary of the change, the problem it solves, the implementation approach, and relevant context. List any dependencies required for this change. Related Issue (Required): Fixes @issue_number ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Refactor (does not change functionality, e.g. code style improvements, linting) - [ ] Documentation update ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration - [ ] Unit Test - [ ] Test Script Or Test Steps (please provide) - [ ] Pipeline Automated API Test (please provide) ## Checklist - [ ] I have performed a self-review of my own code | 我已自行检查了自己的代码 - [ ] I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释 - [ ] I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常 - [ ] I have created related documentation issue/PR in [MemOS-Docs](https://github.com/MemTensor/MemOS-Docs) (if applicable) | 我已在 [MemOS-Docs](https://github.com/MemTensor/MemOS-Docs) 中创建了相关的文档 issue/PR(如果适用) - [ ] I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用) - [ ] I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人 ## Reviewer Checklist - [ ] closes #xxxx (Replace xxxx with the GitHub issue number) - [ ] Made sure Checks passed - [ ] Tests have been provided
2 parents 71a2440 + f196fdb commit 7747de1

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/memos/graph_dbs/neo4j.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,26 @@ def search_by_embedding(
928928

929929
return records
930930

931+
def search_by_fulltext(
932+
self,
933+
query_words: list[str],
934+
top_k: int = 10,
935+
scope: str | None = None,
936+
status: str | None = None,
937+
threshold: float | None = None,
938+
search_filter: dict | None = None,
939+
user_name: str | None = None,
940+
filter: dict | None = None,
941+
knowledgebase_ids: list[str] | None = None,
942+
tsquery_config: str | None = None,
943+
**kwargs,
944+
) -> list[dict]:
945+
"""
946+
TODO: 实现 Neo4j 的关键词检索, 以兼容 TreeTextMemory 的 keyword/fulltext 召回路径.
947+
目前先返回空列表, 避免切换到 Neo4j 后因缺失方法导致运行时报错.
948+
"""
949+
return []
950+
931951
def get_by_metadata(
932952
self,
933953
filters: list[dict[str, Any]],

src/memos/graph_dbs/neo4j_community.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,26 @@ def search_by_embedding(
382382

383383
return filtered_results
384384

385+
def search_by_fulltext(
386+
self,
387+
query_words: list[str],
388+
top_k: int = 10,
389+
scope: str | None = None,
390+
status: str | None = None,
391+
threshold: float | None = None,
392+
search_filter: dict | None = None,
393+
user_name: str | None = None,
394+
filter: dict | None = None,
395+
knowledgebase_ids: list[str] | None = None,
396+
tsquery_config: str | None = None,
397+
**kwargs,
398+
) -> list[dict]:
399+
"""
400+
TODO: 实现 Neo4j Community 的关键词检索, 以兼容 TreeTextMemory 的 keyword/fulltext 召回路径.
401+
目前先返回空列表, 避免切换到 Neo4j 后因缺失方法导致运行时报错.
402+
"""
403+
return []
404+
385405
def _normalize_date_string(self, date_str: str) -> str:
386406
"""
387407
Normalize date string to ISO 8601 format for Neo4j datetime() function.

src/memos/memories/textual/tree_text_memory/retrieve/searcher.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,22 @@ def _retrieve_from_keyword(
513513

514514
id_to_score: dict[str, float] = {}
515515
for scope in scopes:
516-
hits = self.graph_store.search_by_fulltext(
517-
query_words=tsquery_terms,
518-
top_k=top_k * 2,
519-
status="activated",
520-
scope=scope,
521-
search_filter=None,
522-
filter=search_filter,
523-
user_name=user_name,
524-
tsquery_config="jiebaqry",
525-
)
516+
try:
517+
hits = self.graph_store.search_by_fulltext(
518+
query_words=tsquery_terms,
519+
top_k=top_k * 2,
520+
status="activated",
521+
scope=scope,
522+
search_filter=None,
523+
filter=search_filter,
524+
user_name=user_name,
525+
tsquery_config="jiebaqry",
526+
)
527+
except Exception as e:
528+
logger.warning(
529+
f"[PATH-KEYWORD] search_by_fulltext failed, scope={scope}, user_name={user_name}"
530+
)
531+
hits = []
526532
for h in hits or []:
527533
hid = str(h.get("id") or "").strip().strip("'\"")
528534
if not hid:

0 commit comments

Comments
 (0)