1.場景
查詢與content相似的實體
解決方案:
1.直接從neo4j中查詢所有實體并使用杰卡德相似度算法計算相似度,返回top n,該方案由于要匹配圖中所有實體,性能較差。
2.模糊查詢neo4j中的實體,并對查詢結(jié)果與content做相似度計算,相似度算法為hutool中的TextSimilarity.similar()接口。算法原理請自行學(xué)習(xí)。
2.實現(xiàn)
springboot整合neo4j,使用@Query方式進(jìn)行模糊查詢文章來源:http://www.zghlxwxcb.cn/news/detail-643187.html
/**
* 模糊查詢包含content內(nèi)容的實例
* @param content 匹配內(nèi)容
* @param limit 返回限制
* @return 實例的名稱
*/
@Query("match (n) where n.name=~('.*' + $content + '.*') return n.name limit {limit}")
List<String> getLikeEntity(@Param("content") String content, @Param("limit") int limit);
/**
* 模糊查詢包含content內(nèi)容的實例
* @param content 匹配內(nèi)容
* @param limit 返回限制
* @return 實例的名稱
*/
@Query("match (n) where n.name Contains {content} return n.name limit {limit}")
List<String> getLikeEntity2(@Param("content") String content, @Param("limit") int limit);
/**
* 查詢實體并計算杰卡德相似度返回top limit與content相似的實體
* 如果數(shù)據(jù)量大該方法性能較差
*/
@Query("match(n) where exists (n.name) with n ,apoc.text.bytes(n.name) as e, apoc.text.bytes({content}) as s where algo.similarity.jaccard(s, e) > 0 return n.name as name ORDER BY algo.similarity.jaccard(s, e) desc limit {limit}")
List<String> querySimilarEntry(@Param("content") String content, @Param("limit") int limit);
2.參考
1.neo4j模糊查詢
2.spring data neo4j的@Query等查詢方式模糊查詢文章來源地址http://www.zghlxwxcb.cn/news/detail-643187.html
到了這里,關(guān)于springboot整合neo4j模糊查詢的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!