简单的建立索引和查询索引并不难,关键在于他的二次开发,让他适合你自己的需求

既然要二次开发就必须查看源码

首先看看索引过程中的核心类吧:

IndexWriter

这个是核心组件, 建立和打开索引,以及向文档中添加、删除或更新被索引文档的信息。

Directory

描述了Lucene索引的存放位置,他是一个抽象类,一般都用FSDirectory.open(),

Analyzer

IndexWriter 必须指定一个分词器(分析器),

Document

代表了一些域的集合,他表示了每个所要保存的单个文本

Field (4.0 以后就不是Field 了, LongField, TextField ,StringField ,pathField )

Field pathField = new StringField("path", file.getPath(), Field.Store.YES);

doc.add(pathField);

doc.add(new LongField("modified", file.lastModified(), Field.Store.NO));

doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8"))));

建立索引的例子

(注意使用Filed 的时候,StringField是全部匹配的,如下面的“我的Lucene学习” 如果你想查出来,Term必须是“我的Lucene学习” ,如果你想根据“我” 或者“Lucene” 查出结果,必须将StrinField 该为TextField,

如果你想自己的Filed不是完全匹配的话,建议使用TextFiled):

    public void creatIndex() {// 这是索引存放的位置try { String indexPath = "index"; Directory dir; dir = FSDirectory.open(new File(indexPath)); Analyzer analyzer = new MyAnalyzer(Version.LUCENE_41); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_41, analyzer); iwc.setOpenMode(OpenMode.CREATE); IndexWriter writer = new IndexWriter(dir, iwc); Document doc = new Document(); doc.add(new StringField ("id" ,"1" ,Store.YES)); doc.add(new StringField("title", "我的Lucene学习",Store.YES)); doc.add(new StringField("content", "Lucene是一个不错的搜索工具,我很喜欢",Store.YES)); writer.addDocument(doc); writer.close(); } catch (IOException e) { e.printStackTrace(); } }

在导入4.0的源码的时候如果你只导入了lucene-4.1.0-src\lucene-4.1.0\core\src\java 这个文件下的源码建立索引的话,会出现一个异常:

Exception in thread "main" java.lang.ExceptionInInitializerError
 at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:118)
 at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:145)
 at com.test.TestIndex.creatIndex(TestIndex.java:33)
 at com.test.TestIndex.main(TestIndex.java:22)
Caused by: java.lang.IllegalArgumentException: A SPI class of type org.apache.lucene.codecs.Codec with name 'Lucene41' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath.The current classpath supports the following names: []
 at org.apache.lucene.util.NamedSPILoader.lookup(NamedSPILoader.java:106)
 at org.apache.lucene.codecs.Codec.forName(Codec.java:95)
 at org.apache.lucene.codecs.Codec.<clinit>(Codec.java:122)
 ... 4 more

一开始我以为自己复制错了,查看了下源码,有这个类,于是我看了下源码,才放现原因在这:

package org.apache.lucene.util;

public final class SPIClassIterator<S> implements Iterator<Class<? extends S>> {
  private static final String META_INF_SERVICES = "META-INF/services/";

只要把lucene-core-4.1.0.jar包里的META-INF/services 文件夹考到工程里即可

添加后执行TestIndex, index下面就有了索引,和以前的有点区别吧

既然源码都跑通了,就开始研究它内部的代码吧。

既然是写索引,就从org.apache.lucene.index 这个文件夹下研究呗。

(1) IndexWriter

构造方法:

public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException

传递的参数是索引的目录和 IndexWriter配置 (配置包括了Lucene 的版本和分词器)

添加Document的方法

public void addDocument(Iterable<? extends IndexableField> doc)

A SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene40' does not exist.相关推荐

  1. An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene50' does not exist.报错

    记录一次使用ElasticSearch报错 使用junit本地调试时,启动spring项目时,es bean 注入报错: An SPI class of type org.apache.lucene. ...

  2. 【手把手教你全文检索】Apache Lucene初探

    讲解之前,先来分享一些资料 首先呢,学习任何一门新的亦或是旧的开源技术,百度其中一二是最简单的办法,先了解其中的大概,思想等等.这里就贡献一个讲解很到位的ppt.已经被我转成了PDF,便于搜藏. 其次 ...

  3. lucene使用3.0.3_使用Apache Lucene 4.3轻松进行搜索

    lucene使用3.0.3 Lucene是用Java编写的全文搜索引擎,可以为任何应用程序提供强大的搜索功能. Lucene的核心是基于文件的全文本索引. Lucene提供API创建该索引,然后向该索 ...

  4. 使用Apache Lucene 4.3轻松进行搜索

    Lucene是用Java编写的全文搜索引擎,可以为任何应用程序提供强大的搜索功能. Lucene的核心是基于文件的全文本索引. Lucene提供API创建该索引,然后向该索引添加和删除内容. 此外,它 ...

  5. 使用idea解决包依赖冲突的问题SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory

    今天启动项目的时候出现jar包依赖冲突的问题,spring-boot项目默认使用logback日志库,然而又引入了log4j2导致出现下面的错误 SLF4J: Class path contains ...

  6. solr和lucene_使用Apache Lucene和Solr 4进行下一代搜索和分析

    solr和lucene 六年前,我开始为developerWorks撰写有关Solr和Lucene的文章(请参阅参考资料 ). 多年来,Lucene和Solr确立了自己的坚如磐石的技术(Lucene作 ...

  7. 《深入理解Elasticsearch(原书第2版)》——第2章 查询DSL进阶 2.1 Apache Lucene默认评分公式解释...

    本节书摘来自华章计算机<深入理解Elasticsearch(原书第2版)>一书中的第2章,第2.1节,作者 [美]拉斐尔·酷奇(Rafal Ku)马雷克·罗戈任斯基(Marek Rogoz ...

  8. 关于org.apache.lucene.queryParser.ParseException: Encountered 解决方法

    现象: org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, col ...

  9. java hibernate错误org/dom4j/DocumentException、org/jboss/logging/BasicLogger、org/apache/lucene/index...

    很久没有看java的,今天运行一个以前的hibernate程序: 先报错,org/dom4j/DocumentException: 根据资料,原因是没有导入 dom4j-1.6.1.jar:下载此ja ...

最新文章

  1. 【django】项目准备
  2. 人心散了、项目必然要败
  3. xml转svg_C# Excel 转PDF/图片/HTML/TXT/XML/XPS/CSV/ODS/SVG/EMF
  4. 华为新系统 鸿蒙,旗舰CPU+鸿蒙OS!华为Mate家族重磅新品来袭
  5. 以云为跳板,疑似中国黑客组织黑进全球知名IT服务公司
  6. python工作区是什么_在IPython中保存工作区
  7. ue4移动到一定距离_UE4移动组件详解(一)——移动框架与实现原理
  8. 【白皮书下载】开源生态白皮书(2020年).pdf(附下载链接)
  9. git clone server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile
  10. 性能测试中的jvm监控
  11. ASP.NET Razor - html中使用if else
  12. python读取pdf内容转word_Python 实现加密过的PDF文件转WORD格式
  13. 计算机绘图cad期末考试试题,机械CAD/CAM期末考试复习资料[机械CAD/CAM]
  14. java加载不了驱动程序,请教java连mysql数据库时一直加载不起驱动
  15. python excel怎么将字母后的数字取出来_利用通配符将Excel中英文字母、中文、数字提取出来...
  16. 各个小组对于“我爱淘”的评价
  17. (C语言)不变初心数 (15 分)---pta
  18. 微信小程序 php解密,微信小程序des加密、PHP des解密
  19. 长隆大马戏机器人_长隆娱乐登陆
  20. 智能家居UI--灯光控制

热门文章

  1. 初识WordPress数据库
  2. Android属性动画源代码解析(超详细)
  3. 理解与使用Javascript中的回调函数
  4. ASP中Randomize随机函数的使用
  5. 收图一张: android 架构图
  6. oracle将213变成123,oracle 转换函数
  7. python中导入包出现unresolved reference问题
  8. 函数 —— fork()将运行着的程序分成2个(几乎)完全一样的进程
  9. 小技巧 —— linux中怎么简单的复制5000个数字
  10. [AT2369] [agc013_c] Ants on a Circle