[b]5 如何建索引[/b]

5.1 最简单的能完成索引的代码片断

IndexWriter writer = new IndexWriter(“/data/index/”, new StandardAnalyzer(), true);
                Document doc = new Document();
                doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.TOKENIZED));
                doc.add(new Field("content", "lucene works well", Field.Store.YES, Field.Index.TOKENIZED));
                writer.addDocument(doc);
                writer.optimize();
                writer.close();

下面我们分析一下这段代码。

首先我们创建了一个writer,并指定存放索引的目录为“/data/index”,使用的分析器为StandardAnalyzer,第三个参数说明如果已经有索引文件在索引目录下,我们将覆盖它们(如果是false不覆盖,而是追加索引)。

然后我们新建一个document。

我们向document添加一个field,名字是“title”,内容是“lucene introduction”,对它进行存储并索引。

再添加一个名字是“content”的field,内容是“lucene works well”,也是存储并索引。

然后我们将这个文档添加到索引中,如果有多个文档,可以重复上面的操作,创建document并添加。

添加完所有document,我们对索引进行优化,优化主要是将多个segment合并到一个,有利于提高索引速度。

随后将writer关闭,这点很重要。

创建索引就这么简单!当然你可能修改上面的代码获得更具个性化的服务。

5.2 将索引直接写在内存

你需要首先创建一个RAMDirectory,并将其传给writer,代码如下:

Directory dir = new RAMDirectory();
            IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
            Document doc = new Document();
            doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.TOKENIZED));
            doc.add(new Field("content", "lucene works well", Field.Store.YES, Field.Index.TOKENIZED));
            writer.addDocument(doc);
            writer.optimize();
            writer.close();

5.3 索引文本文件

如果你想把纯文本文件索引起来,而不想自己将它们读入字符串创建field,你可以用下面的代码创建field:

Field field = new Field("content", new FileReader(file));

这里的file就是该文本文件。该构造函数实际上是读去文件内容,并对其进行索引,但不存储。

[b]6 如何维护索引[/b]

索引的维护操作都是由IndexReader类提供。

6.1 如何删除索引

lucene提供了两种从索引中删除document的方法,一种是

void deleteDocument(int docNum);

这种方法是根据document在索引中的编号来删除,每个document加进索引后都会有个唯一编号,所以根据编号删除是一种精确删除,但是这个编号是索引的内部结构,一般我们不会知道某个文件的编号到底是几,所以用处不大。另一种是:

void deleteDocuments(Term term);

这种方法实际上是首先根据参数term执行一个搜索操作,然后把搜索到的结果批量删除了。我们可以通过这个方法提供一个严格的查询条件,达到删除指定document的目的。

下面给出一个例子:

Directory dir = FSDirectory.getDirectory(PATH, false);
                IndexReader reader = IndexReader.open(dir);
                Term term = new Term(field, key);
                reader.deleteDocuments(term);
                reader.close();

6.2 如何更新索引

lucene并没有提供专门的索引更新方法,我们需要先将相应的document删除,然后再将新的document加入索引。例如:

Directory dir = FSDirectory.getDirectory(PATH, false);
                IndexReader reader = IndexReader.open(dir);
                Term term = new Term(“title”, “lucene introduction”);
                reader.deleteDocuments(term);
                reader.close();

                IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
                Document doc = new Document();
                doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.TOKENIZED));
                doc.add(new Field("content", "lucene is funny", Field.Store.YES, Field.Index.TOKENIZED));
                writer.addDocument(doc);
                writer.optimize();
                writer.close();