A SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene40' does not exist.
簡單的建立索引和查詢索引并不難,關(guān)鍵在于他的二次開發(fā),讓他適合你自己的需求
既然要二次開發(fā)就必須查看源碼
?首先看看索引過程中的核心類吧:
?IndexWriter
??? ? 這個(gè)是核心組件, 建立和打開索引,以及向文檔中添加、刪除或更新被索引文檔的信息。
?Directory
???? 描述了Lucene索引的存放位置,他是一個(gè)抽象類,一般都用FSDirectory.open(),
?Analyzer
???? IndexWriter 必須指定一個(gè)分詞器(分析器),
?Document
???? 代表了一些域的集合,他表示了每個(gè)所要保存的單個(gè)文本
?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 的時(shí)候,StringField是全部匹配的,如下面的“我的Lucene學(xué)習(xí)” 如果你想查出來,Term必須是“我的Lucene學(xué)習(xí)” ,如果你想根據(jù)“我” 或者“Lucene” 查出結(jié)果,必須將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學(xué)習(xí)",Store.YES)); doc.add(new StringField("content", "Lucene是一個(gè)不錯(cuò)的搜索工具,我很喜歡",Store.YES)); writer.addDocument(doc); writer.close(); } catch (IOException e) { e.printStackTrace(); } }在導(dǎo)入4.0的源碼的時(shí)候如果你只導(dǎo)入了lucene-4.1.0-src\lucene-4.1.0\core\src\java 這個(gè)文件下的源碼建立索引的話,會(huì)出現(xiàn)一個(gè)異常:
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
? 一開始我以為自己復(fù)制錯(cuò)了,查看了下源碼,有這個(gè)類,于是我看了下源碼,才放現(xiàn)原因在這:
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 文件夾考到工程里即可
添加后執(zhí)行TestIndex, index下面就有了索引,和以前的有點(diǎn)區(qū)別吧
?既然源碼都跑通了,就開始研究它內(nèi)部的代碼吧。
?
?既然是寫索引,就從org.apache.lucene.index 這個(gè)文件夾下研究唄。
(1) IndexWriter
??????? 構(gòu)造方法:
????????? public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException
????? ?? 傳遞的參數(shù)是索引的目錄和 IndexWriter配置 (配置包括了Lucene 的版本和分詞器)
????? ?? 添加Document的方法
??????? ?public void addDocument(Iterable<? extends IndexableField> doc)
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的A SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene40' does not exist.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在Cocos2D游戏中实现A*寻路算
- 下一篇: IBM WebSphere MQ 系列(