Lucene索引核心类研究

来源:互联网 发布:苹果mac重新安装系统 编辑:程序博客网 时间:2024/05/12 15:14

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

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

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

 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)

原创粉丝点击