neo4j 如何返回Cypher中where子句内部生成的值?

20jt8wwn  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(137)

我有以下Cypher查询:

MATCH (n)-[r]->(k) 
WHERE ANY(x in keys(n) 
    WHERE round(apoc.text.levenshteinSimilarity(
       TRIM(
          REDUCE(mergedString = "", item in n[x] 
               | mergedString + item + " ")), "syn"), 4) 
                   > 0.8) 
RETURN n, r, k

如何返回相似性函数在WHERE子句中生成的分数。
我尝试用WITH来做这件事,没有运气:

MATCH (n)-[r]->(k) 
WITH *,  [x in keys(n) | [x, round(apoc.text. levenshteinSimilarity(TRIM(REDUCE(mergedString = '', item in n[x] | mergedString + item + ' ')), 'syn'), 4)]] as scores
WHERE [s in scores WHERE s[1] >= 0.8]
RETURN n,r,k,[s in scores WHERE s[1] >= 0.8] AS attr_scores
pftdvrlh

pftdvrlh1#

要想只返回分数大于0.8的相关属性,请将列表理解更新为:

MATCH (n)-[r]->(k) 
WITH *,  [x in keys(n) | [x, round(apoc.text. levenshteinSimilarity(TRIM(REDUCE(mergedString = '', item in n[x] | mergedString + item + ' ')), 'syn'), 4)]] as scores
RETURN n,r,k,[s in scores WHERE s[1] >= 0.8 | s] AS attr_scores

最后,我们与Charchit Kapoor一起找到了最佳解决方案:

MATCH (n)-[r]->(k)  
UNWIND keys(n) as key  
WITH n, r, k, key, round(apoc.text. levenshteinSimilarity(TRIM(REDUCE(mergedString = "", item in n[key] | mergedString + item + " ")), "syn"), 4) as score  
WITH n, r, k, collect({key:key, value:n[key], score:score}) as keyScores  
WITH n, r, k, [s in keyScores 
WHERE s.score >= 0.8 | s] AS attr_scores WHERE size(attr_scores) > 0 
RETURN *

相关问题