正在用的Neo4j是当前最新版:3.1.0,各种踩坑。说一下如何在Neo4j 3.1.0中使用中文索引。选用了IKAnalyzer做分词器。

1. 首先参考文章:

https://segmentfault.com/a/1190000005665612

里面大致讲了用IKAnalyzer做索引的方式。但并不清晰,实际上,这篇文章的背景是用嵌入式Neo4j,即Neo4j一定要嵌入在你的Java应用中(https://neo4j.com/docs/java-reference/current/#tutorials-java-embedded),切记。否则无法使用自定义的Analyzer。其次,文中的方法现在用起来已经有问题了,因为Neo4j 3.1.0用了lucene5.5,故官方的IKAnalyzer已经不适用了。

2. 修正

转用 IKAnalyzer2012FF_u1.jar,在Google可以下载到(https://code.google.com/archive/p/ik-analyzer/downloads)。这个版本的IKAnalyzer是有小伙伴修复了IKAnalyzer不适配lucene3.5以上而修改的一个版本。但是用了这个包仍有问题,报错提示:

Caused by: java.lang.AbstractMethodError: org.apache.lucene.analysis.Analyzer.createComponents(Ljava/lang/String;)Lorg/apache/lucene/analysis/Analyzer$TokenStreamComponents;

即IKAnalyzer的Analyzer类和当前版本的lucene仍有不适配的地方。

解决方案:再增加两个类


  1. package com.uc.wa.function;
  2. import org.apache.lucene.analysis.Analyzer;
  3. import org.apache.lucene.analysis.Tokenizer;
  4. public class IKAnalyzer5x extends Analyzer{
  5. private boolean useSmart;
  6. public boolean useSmart() {
  7. return useSmart;
  8. }
  9. public void setUseSmart(boolean useSmart) {
  10. this.useSmart = useSmart;
  11. }
  12. public IKAnalyzer5x(){
  13. this(false);
  14. }
  15. public IKAnalyzer5x(boolean useSmart){
  16. super();
  17. this.useSmart = useSmart;
  18. }
  19. /**
  20. protected TokenStreamComponents createComponents(String fieldName, final Reader in) {
  21. Tokenizer _IKTokenizer = new IKTokenizer(in , this.useSmart());
  22. return new TokenStreamComponents(_IKTokenizer);
  23. }
  24. **/
  25. /**
  26. * 重写最新版本的createComponents
  27. * 重载Analyzer接口,构造分词组件
  28. */
  29. @Override
  30. protected TokenStreamComponents createComponents(String fieldName) {
  31. Tokenizer _IKTokenizer = new IKTokenizer5x(this.useSmart());
  32. return new TokenStreamComponents(_IKTokenizer);
  33. }
  34. }


  1. package com.uc.wa.function;
  2. import java.io.IOException;
  3. import org.apache.lucene.analysis.Tokenizer;
  4. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  5. import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
  6. import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
  7. import org.wltea.analyzer.core.IKSegmenter;
  8. import org.wltea.analyzer.core.Lexeme;
  9. public class IKTokenizer5x extends Tokenizer{
  10. //IK�ִ���ʵ��
  11. private IKSegmenter _IKImplement;
  12. //��Ԫ�ı�����
  13. private final CharTermAttribute termAtt;
  14. //��Ԫλ������
  15. private final OffsetAttribute offsetAtt;
  16. //��Ԫ�������ԣ������Է���ο�org.wltea.analyzer.core.Lexeme�еķ��ೣ����
  17. private final TypeAttribute typeAtt;
  18. //��¼���һ����Ԫ�Ľ���λ��
  19. private int endPosition;
  20. /**
  21. public IKTokenizer(Reader in , boolean useSmart){
  22. super(in);
  23. offsetAtt = addAttribute(OffsetAttribute.class);
  24. termAtt = addAttribute(CharTermAttribute.class);
  25. typeAtt = addAttribute(TypeAttribute.class);
  26. _IKImplement = new IKSegmenter(input , useSmart);
  27. }**/
  28. /**
  29. * Lucene 5.x Tokenizer�������๹�캯��
  30. * ʵ�����µ�Tokenizer�ӿ�
  31. * @param useSmart
  32. */
  33. public IKTokenizer5x(boolean useSmart){
  34. super();
  35. offsetAtt = addAttribute(OffsetAttribute.class);
  36. termAtt = addAttribute(CharTermAttribute.class);
  37. typeAtt = addAttribute(TypeAttribute.class);
  38. _IKImplement = new IKSegmenter(input , useSmart);
  39. }
  40. /* (non-Javadoc)
  41. * @see org.apache.lucene.analysis.TokenStream#incrementToken()
  42. */
  43. @Override
  44. public boolean incrementToken() throws IOException {
  45. //������еĴ�Ԫ����
  46. clearAttributes();
  47. Lexeme nextLexeme = _IKImplement.next();
  48. if(nextLexeme != null){
  49. //��Lexemeת��Attributes
  50. //���ô�Ԫ�ı�
  51. termAtt.append(nextLexeme.getLexemeText());
  52. //���ô�Ԫ����
  53. termAtt.setLength(nextLexeme.getLength());
  54. //���ô�Ԫλ��
  55. offsetAtt.setOffset(nextLexeme.getBeginPosition(), nextLexeme.getEndPosition());
  56. //��¼�ִʵ����λ��
  57. endPosition = nextLexeme.getEndPosition();
  58. //��¼��Ԫ����
  59. typeAtt.setType(nextLexeme.getLexemeTypeString());
  60. //����true��֪�����¸���Ԫ
  61. return true;
  62. }
  63. //����false��֪��Ԫ������
  64. return false;
  65. }
  66. /*
  67. * (non-Javadoc)
  68. * @see org.apache.lucene.analysis.Tokenizer#reset(java.io.Reader)
  69. */
  70. @Override
  71. public void reset() throws IOException {
  72. super.reset();
  73. _IKImplement.reset(input);
  74. }
  75. @Override
  76. public final void end() {
  77. // set final offset
  78. int finalOffset = correctOffset(this.endPosition);
  79. offsetAtt.setOffset(finalOffset, finalOffset);
  80. }
  81. }

解决 IKAnalyzer2012FF_u1.jar和lucene5不适配的问题。使用时用IKAnalyzer5x替换IKAnalyzer即可。

3. 最后

Neo4j中文索引建立和搜索示例:


  1. /**
  2. * 为单个结点创建索引
  3. *
  4. * @param propKeys
  5. */
  6. public static void createFullTextIndex(long id, List<String> propKeys) {
  7. log.info("method[createFullTextIndex] begin.propKeys<"+propKeys+">");
  8. Index<Node> entityIndex = null;
  9. try (Transaction tx = Neo4j.graphDb.beginTx()) {
  10. entityIndex = Neo4j.graphDb.index().forNodes("NodeFullTextIndex",
  11. MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "analyzer", IKAnalyzer5x.class.getName()));
  12. Node node = Neo4j.graphDb.getNodeById(id);
  13. log.info("method[createFullTextIndex] get node id<"+node.getId()+"> name<"
  14. +node.getProperty("knowledge_name")+">");
  15. /**获取node详细信息*/
  16. Set<Map.Entry<String, Object>> properties = node.getProperties(propKeys.toArray(new String[0]))
  17. .entrySet();
  18. for (Map.Entry<String, Object> property : properties) {
  19. log.info("method[createFullTextIndex] index prop<"+property.getKey()+":"+property.getValue()+">");
  20. entityIndex.add(node, property.getKey(), property.getValue());
  21. }
  22. tx.success();
  23. }
  24. }


  1. /**
  2. * 使用索引查询
  3. *
  4. * @param query
  5. * @return
  6. * @throws IOException
  7. */
  8. public static List<Map<String, Object>> selectByFullTextIndex(String[] fields, String query) throws IOException {
  9. List<Map<String, Object>> ret = Lists.newArrayList();
  10. try (Transaction tx = Neo4j.graphDb.beginTx()) {
  11. IndexManager index = Neo4j.graphDb.index();
  12. /**查询*/
  13. Index<Node> addressNodeFullTextIndex = index.forNodes("NodeFullTextIndex",
  14. MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "analyzer", IKAnalyzer5x.class.getName()));
  15. Query q = IKQueryParser.parseMultiField(fields, query);
  16. IndexHits<Node> foundNodes = addressNodeFullTextIndex.query(q);
  17. for(Node n : foundNodes){
  18. Map<String, Object> m = n.getAllProperties();
  19. if(!Float.isNaN(foundNodes.currentScore())){
  20. m.put("score", foundNodes.currentScore());
  21. }
  22. log.info("method[selectByIndex] score<"+foundNodes.currentScore()+">");
  23. ret.add(m);
  24. }
  25. tx.success();
  26. } catch (IOException e) {
  27. log.error("method[selectByIndex] fields<"+Joiner.on(",").join(fields)+"> query<"+query+">", e);
  28. throw e;
  29. }
  30. return ret;
  31. }

注意到,在这里我用了IKQueryParser,即根据我们的查询词和要查询的字段,自动构造Query。这里是绕过了一个坑:用lucene查询语句直接查的话,是有问题的。比如:“address:南昌市” 查询语句,会搜到所有带市字的地址,这是非常不合理的。改用IKQueryParser即修正这个问题。IKQueryParser是IKAnalyzer自带的一个工具,但在 IKAnalyzer2012FF_u1.jar却被删减掉了。因此我这里重新引入了原版IKAnalyzer的jar包,项目最终是两个jar包共存的。

到这里坑就踩得差不多了。

       原文地址:https://blog.csdn.net/hereiskxm/article/details/54345261                         </div>

【Neo4j】踩坑大会-Neo4J用中文索引的更多相关文章

  1. PLSQL Developer 中文乱码踩坑记

    环境 操作系统版本: Windows 7 PL/SQL 版本: 12.0.1.1814 原因 由于 Oracle 服务器端和客户端字符集编码不一致引起的. 注意点 写在最前面,减少踩坑!!! 网上教程 ...

  2. Neo4j之坑

    10个月前,我开始用neo4j做cmdb. 初体验下去neo4j很美好. 但是一年中发现一些问题, 仅仅是个人的体验.经供参考 查询语言 如果接触过Neo4j,都会为Cypher的简单和易用感觉到惊叹 ...

  3. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  4. AI相关 TensorFlow -卷积神经网络 踩坑日记之一

    上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...

  5. 人工智能(AI)库TensorFlow 踩坑日记之一

    上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...

  6. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  7. C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式

    C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...

  8. Nebula Exchange 工具 Hive 数据导入的踩坑之旅

    摘要:本文由社区用户 xrfinbj 贡献,主要介绍 Exchange 工具从 Hive 数仓导入数据到 Nebula Graph 的流程及相关的注意事项. 1 背景 公司内部有使用图数据库的场景,内 ...

  9. Spark踩坑记——数据库(Hbase+Mysql)

    [TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...

随机推荐

  1. hive调用MapReduce之后遇到kill command之后卡住或者一直开在MapReduce之前

    https://blog.csdn.net/weixin_42158422/article/details/88876943

  2. 洛谷 P3803 【模板】多项式乘法(FFT)

    题目链接:P3803 [模板]多项式乘法(FFT) 题意 给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(F(x)\) 和 \(G(x)\) ...

  3. Ibatis sql语句

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-/ ...

  4. JAVA 实现数据导入Phoenix

    需要导入的jar 包有: 实现代码: package cn.test; import java.sql.Connection; import java.sql.DriverManager; impor ...

  5. js 点击获取验证码后的倒数60s

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script ...

  6. <爬虫>相关的知识

    1.概念.工具和HTTP 什么是爬虫 模拟客户端发送网络请求,获取响应,按照规则提取数据 爬虫的数据去哪了 展示到网页上(百度新闻,今日头条) 进行分析,从数据中寻找规律(指数网站:百度指数) 需要的 ...

  7. 44-Ubuntu-用户管理-09-chmod的数字表示法介绍

    chmod 修改文件和目录权限 chmod在设置权限时,可以简单地使用三个数字分别对应拥有者/组和其他用户的权限. 注意: chmod直接修改文件|目录的'读|写|执行'权限,但是不能精确到拥有者|组 ...

  8. springboot中引用配置文件中的参数

    首先可以看到这是做微信登陆时候的配置,一般不会写死都是通过配置文件获取,所以,记载配置文件中 那么怎么引用呢: 可以看到直接注入的方式就可以引用了,所以看下面: 进行页面跳转,并且带有参数的, 使用m ...

  9. css切角效果,折角效果

    html <div class="one">12345</div> <div class="two">abcde</d ...

  10. angularJs input框绑定ng-model后js获取不到问题

    搬运自:https://blog.csdn.net/fenglongmiao/article/details/81545993 与其他指令一样,ng-controller指令也会创建一个子级作用域,因 ...