neo4j 如何有效地使用全文索引在同一查询中基于先前的MATCH结果进行搜索

bmp9r5qi  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(135)

简介

这是我的一个现有查询的样子:

MATCH
(p:Person { id: $p_id })-[k1:`KNOWS`]->(person:Person)
WHERE (// some criteria)
MATCH
(person)-[work:`WORKED_AT`]->(company:Company)
WHERE (work.title contains “Product Manager" and work.start_date is not null)
WITH person, work
RETURN
DISTINCT person.full_name, work.title

上述查询的问题在于它不区分大小写。因此,如果数据库中的实际标题是product manager,则上述查询将失败。
所以我们尝试使用full text search index。我们在:work关系上创建了索引,并验证了它的有效性,例如搜索:

call db.index.fulltext.queryRelationships(“<indexName>”, “CEO”) 
yield relationship return reltionship.title limit 10

将返回类似于以下内容的内容:

CEO
Ceo
..etc

提问

如何将上述索引搜索应用于节点搜索?例如:

MATCH
(p:Person { id: $p_id })-[k1:`KNOWS`]->(person:Person)
WHERE (// some criteria)
MATCH
(person)-[work:`WORKED_AT`]->(company:Company)
WHERE //apply the db.index.fulltext.queryRelationships here on :work somehow
WITH person, work
RETURN
DISTINCT person.full_name, work.title
dgtucam1

dgtucam11#

我猜您首先必须使用索引获取关系,然后应用MATCH。试试这个:

MATCH (p:Person { id: $p_id })-[k1:`KNOWS`]->(person:Person)
WHERE (// some criteria)
CALL db.index.fulltext.queryRelationships("indexName", "searchString") 
YIELD relationship AS work
MATCH (person)-[work]->(company:Company)
WITH person, work
RETURN DISTINCT person.full_name, work.title

或者,您可以使用正则表达式执行不区分大小写的搜索。像这样:

MATCH (p:Person { id: $p_id })-[k1:`KNOWS`]->(person:Person)
WHERE (// some criteria)
MATCH (person)-[work:`WORKED_AT`]->(company:Company)
WHERE (work.title =~ '(?i).*Product Manager.*' and work.start_date is not null)
WITH person, work
RETURN DISTINCT person.full_name, work.title

相关问题