基于jar lucene3.6.2
Field.Store.YES / NO --- 存储选项
设置为YES表示把这个域中的内容完全存储到文件中,方便进行文本的还原
设置为NO表示把这个域的内容不存储到文件中,但是可以被索引,此时内容无法完全还原
Field.Index. --- 索引选项
ANALYZED 进行分词和索引,适用于标题,内容等
NOT_ANALYZED 进行索引,但不进行分词,适用于精确搜索
ANALYZED_NOT_NORMS 进行分词但是不存储norms信息,这个norms中包括了创建索引的时间和全职等信息
NOT_ANALYZED_NOT_NORMS 既不进行分词也不存储norms信息
NO 不进行索引
下面直接通过例子说明问题
- IndexUtil.java
- import java.io.File;
- import java.io.IOException;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.index.CorruptIndexException;
- import org.apache.lucene.index.IndexReader;
- 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.store.LockObtainFailedException;
- import org.apache.lucene.util.Version;
- public class IndexUtil {
- private String[] ids = {"1","2","3","4","5","6"};
- private String[] emails = {"soukenan@qq.com","li@soukenan.com","804632564@qq.com","admin@qq.com","soukenan@kenan.org","123@df.com"};
- private String[] content ={
- "Welcome to my home",
- "Hello Kenan",
- "Good morning",
- "Are you OK?",
- "Yeah hahahahahahaha",
- "I like foot ball"
- };
- private int[] attachs = {1,4,6,2,3,8};
- private String[] names = {"zhangsan","kenan","soukenan","Micheal","Liangchengpeng","Jah"};
- //词典
- private Directory directory = null;
- //写入笔
- private IndexWriter writer = null;
- //文档对象
- private Document doc = null;
- //读取对象
- private IndexReader reader = null;
- public IndexUtil(){
- try {
- this.directory = FSDirectory.open(new File("d:/lucene/index02"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 构建索引
- */
- public void buildIndex(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36)));
- for(int i=0;i<6;i++){
- doc = new Document();
- doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
- doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED));
- doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- writer.addDocument(doc);
- }
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (LockObtainFailedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(writer != null ){
- try {
- writer.close();
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public void query(){
- try {
- this.reader = IndexReader.open(directory);
- System.out.println("maxDoc:"+reader.maxDoc());
- System.out.println("numDocs:"+reader.numDocs());
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- 测试类 TestCase.java
- import static org.junit.Assert.*;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class TestCase {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test
- public void testBuildIndex() {
- new IndexUtil().buildIndex();
- }
- @Test
- public void testQuery(){
- new IndexUtil().query();
- }
- }
转载于:https://blog.51cto.com/soukenan/1119923