A SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene40' does not exist.
简单的建立索引和查询索引并不难,关键在于他的二次开发,让他适合你自己的需求
既然要二次开发就必须查看源码
首先看看索引过程中的核心类吧:
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.的更多相关文章
- An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene50' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classp
背景介绍: 当ES中guava库与hive等组件的库冲突时,对Elasticsearch库进行shade,relocate解决库冲突问题. 当使用"org.apache.maven.plug ...
- Apache Lucene学习笔记
Hadoop概述 Apache lucene: 全球第一个开源的全文检索引擎工具包 完整的查询引擎和搜索引擎 部分文本分析引擎 开发人员在此基础建立完整的全文检索引擎 以下为转载:http://www ...
- Apache Lucene(全文检索引擎)—分词器
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
- MyEclipse配置tomcat报错 - java.lang.UnsupportedClassVersionError: org/apache/lucene/store/Directory : Unsupported major.minor version 51.0
1 开发Servlet程序时,MyEclipse配置好tomcat与JDK之后,启动时控制台报下列错误: 1 java.lang.UnsupportedClassVersionError: org/a ...
- 【手把手教你全文检索】Apache Lucene初探 (zhuan)
http://www.cnblogs.com/xing901022/p/3933675.html *************************************************** ...
- query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max
3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...
- 使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析
使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析 使用搜索引擎计数构建快速.高效和可扩展的数据驱动应用程序 Apache Lucene™ 和 Solr™ 是强大的开源搜索技术, ...
- Apache Lucene(全文检索引擎)—搜索
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
- Apache Lucene(全文检索引擎)—创建索引
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
随机推荐
- 判断手机电脑微信 js
if ((navigator.userAgent.match(/(MicroMessenger)/i))) { //微信浏览器 //location.href=""; } else ...
- 推荐两个好用的Xcode插件(提供下载链接)
这里推荐两款好用的Xcode插件,并提供下载链接. 一.插件和使用如下: 1.两款插件 对项目中图片提供自动提示功能的插件:KSImageNamed-Xcode-master 提供快速创建自动注释:V ...
- 如何将编译出来的images拷贝到windows下面刷机
由于SPRD的刷机工具ResearchDownload运行在window环境下:这样,我们平时在开发环境下编译出来的镜像文件就不能直接用于刷机了. 这里涉及到一个双系统中文件共享的方法.由于企业信息安 ...
- javascript焦点图自动缓冲滚动
html中调用的js库,之前的随笔中有写,就不细说了,不明白的可以留言给我 <!DOCTYPE html> <html> <head> <meta chars ...
- 基于 twemproxy 搭建 redis 集群
概述 由于单台redis服务器的内存管理能力有限,使用过大内存redis服务器的性能急剧下降,且服务器发生故障将直接影响大面积业务.为了获取更好的缓存性能及扩展型,我们将需要搭建redis集群来满足需 ...
- SQLite : 解决“找不到请求的 .Net Framework 数据提供程序。可能没有安装”的问题
在使用subsonic 3时,如果不完整安装SQLite的SDK包,直接在项目中使用System.Data.SQLite将引发上述错误. 原因是:是 DBproviderfacotories 没有导 ...
- WIN7 64位 IE10打开WEB管理提示证书不安全
用IE10只要在CMD里运行 certutil -setreg chain\minRSAPubKeyBitLength 512 就好了
- VS中的快捷键快速格式化代码,使好看,整齐
在VC2005中,快捷键是Ctrl + K, Ctrl + F, 这是一个组合键,即先按Ctrl + K, 这时候编辑器会等待下一个按键动作,此时再按Ctrl + F, 即可以格式化代码了,当然,也可 ...
- selenium高级用法
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp# WebDriver: Advanced Usage Explicit and Im ...
- Entity Framework技巧系列之十一 - Tip 42 - 45
提示42. 怎样使用Code-Only创建一个动态模型 背景: 当我们给出使用Code-Only的例子,总是由创建一个继承自ObjectContext的强类型的Context开始.这个类用于引导模型. ...