您的位置:首页 > 其它

Lucene教程(一) 创建索引

2016-08-04 16:59 295 查看
简述:

由于Lucene不同的版本差距较大,,此系列教程打算把3.5版本,4.5版本,5.0版本都给出个例子,方便大家学习,也方便自己复习。

注:由于Lucene5.0版本是基于JDK1.7开发的,所以想学习的同学请配置1.7及以上的版本。故测试Lucene 6.1.0也适用Lucene 5.0中的代码。Lucene 6.1.0最低要求也是JDK1.7.

创建索引可分为主要的几步,我自己试验过,不同的版本间会有些不同,但是跟着如下的几大步骤一步一步写,问题不会太大。

创建Directory

创建IndexWriter

创建Document对象

为Document添加Field

通过IndexWriter添加文档到索引中

下面为例子代码:

3.5版本:

3.5版本比较简单,只需要Lucene核心包lucene-core即可,

例子代码如下:

package com.darren.lucene35;

import java.io.File;
import java.io.FileReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class HelloLucene {

/**
* 建立索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
// 1、创建Directory
// 建立在内存中
// Directory directory = new RAMDirectory();
// 建立在硬盘式
Directory directory = FSDirectory.open(new File("F:/test/lucene/index"));

// 2、创建IndexWriter
// 使用默认的标准分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);

indexWriter = new IndexWriter(directory, indexWriterConfig);

File dFile = new File("F:/test/lucene/document");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、创建Document对象
Document document = new Document();

// 4、为Document添加Field
document.add(new Field("content", new FileReader(file)));
document.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("filepath", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));

// 5、通过IndexWriter添加文档到索引中
indexWriter.addDocument(document);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null) {
indexWriter.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

}


4.5版本:

4.5版本需要Lucene核心包lucene-core和分词包lucene-analyzers-common,从4.0版本之后分词包从核心包分离。

代码如下:

package com.darren.lucene45;

import java.io.File;
import java.io.FileReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class HelloLucene {
/**
* 建立索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
// 1、创建Directory
Directory directory = FSDirectory.open(new File("F:/test/lucene/index"));

// 2、创建IndexWriter
/**
* 注意StandardAnalyzer与3.5版本的不同:
*
* StandardAnalyzer不在lucene-core包中而在lucene-analyzers-common包中 从4.0版本以后分离
*/
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer);
indexWriter = new IndexWriter(directory, indexWriterConfig);

File dFile = new File("F:/test/lucene/document");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、创建Document对象
Document document = new Document();

// 4、为Document添加Field
/**
* 注意Field与3.5版本的不同:两个参数的构造器已过时,使用如下构造器
*/
// 第三个参数是FieldType 但是定义在TextField中作为静态变量,看API也不好知道怎么写
document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED));
document.add(new Field("filename", file.getName(), TextField.TYPE_STORED));
document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED));

// 5、通过IndexWriter添加文档到索引中
indexWriter.addDocument(document);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null) {
indexWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
}


5.0版本:

5.0版本和4.5版本一样,例子代码如下:

package com.darren.lucene50;

import java.io.File;
import java.io.FileReader;
import java.nio.file.FileSystems;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class HelloLucene {
/**
* 建立索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
// 1、创建Directory
/**
* 注意open方法与3.5版本和4.5版本的不同:
*
* 这里不再接受一个File而是Path,使用的是JDK1.7的新特性,也就是说5.0版本是基于JDK1.7开发的
*
* 如何获取Path,请参照 Java7新特性--Path http://blog.csdn.net/zpf336/article/details/45074445 *
*/
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:/test/lucene/index"));
// 2、创建IndexWriter
/**
* 注意StandardAnalyzer与3.5版本的不同:
*
* StandardAnalyzer不在lucene-core包中而在lucene-analyzers-common包中 从4.0版本以后分离
*
* 并且不需要提供版本号
*/
Analyzer analyzer = new StandardAnalyzer();

/**
* 注意IndexWriterConfig与3.5版本和4.5版本的不同:
*
* 不需要提供版本号
*/
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(directory, indexWriterConfig);
File dFile = new File("F:/test/lucene/document");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、创建Document对象
Document document = new Document();
// 4、为Document添加Field
/**
* 注意Field与3.5版本的不同:两个参数的构造器已过时,使用如下构造器
*
* 但是和4.5版本是相同的
*/
// 第三个参数是FieldType 但是定义在TextField中作为静态变量,看API也不好知道怎么写
document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED));
document.add(new Field("filename", file.getName(), TextField.TYPE_STORED));
document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED));

// 5、通过IndexWriter添加文档到索引中
indexWriter.addDocument(document);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null) {
indexWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
}


测试代码:

由于测试代码都一样,仅贴出5.0版本的此时代码:

package com.darren.lucene50;

import org.junit.Test;

public class HelloLuceneTest {
@Test
public void testIndex() {
HelloLucene helloLucene = new HelloLucene();
helloLucene.index();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息