neo4j截取字符串
我想在 neo4j 中将文章的前50个字符作为摘要保存到关系中,如果前50个字符中有句号或者问号,就截取到句号或者问号的位置。下面是在 spring-data-neo4j 中的实现。
@Query("UNWIND {linkList} AS link " +
"MATCH (a:BaseNode:Article {nodeId: link.articleId}) " +
"MATCH (n:BaseNode:Document {nodeId: link.documentId}) " +
"MERGE (n)-[r:reference]->(a) " +
"WITH n, r, a, substring(a.content, 0, 50) AS initialSnippet, " +
"replace(replace(substring(a.content, 0, 50), '。', '|'), '?', '|') AS markedSnippet " +
"WITH n, r, a, initialSnippet, markedSnippet, split(markedSnippet, '|')[0] AS summaryEnd " +
"SET r.summary = CASE WHEN summaryEnd IS NULL THEN initialSnippet ELSE substring(initialSnippet, 0, size(summaryEnd) + 1) END")
void linkArticleToDocument(List<Map<String, Integer>> linkList);
实现思路是先将文章的前50个字符保存到initialSnippet
中,然后将其中的句号和问号替换为|
,然后使用split
函数按|
分割并取第一个元素。