Lucene学习笔记(2)-- 索引的增删改

1、创建一个maven project

2、在pom.xml文件中添加一来的jar

pom.xml

<dependencies>
  	<dependency>
		<groupId>org.apache.lucene</groupId>
		<artifactId>lucene-core</artifactId>
		<version>5.3.1</version>
	</dependency>
	
	<dependency>
	<groupId>org.apache.lucene</groupId>
		<artifactId>lucene-queryparser</artifactId>
		<version>5.3.1</version>
	</dependency>
	
	<dependency>
		<groupId>org.apache.lucene</groupId>
		<artifactId>lucene-analyzers-common</artifactId>
		<version>5.3.1</version>
	</dependency>
	
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
	</dependency>
	
  </dependencies>

2、创建索引的测试类,这里用junit测试


import java.nio.file.Paths;

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.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Before;
import org.junit.Test;

public class IndexingTest {

	/**
	 * 准备要检索的内容
	 */
	private String ids[]={"1","2","3"};
	private String citys[]={"qingdao","nanjing","shanghai"};
	private String descs[]={
			"Qingdao is a beautiful city.",
			"Nanjing is a city of culture.",
			"Shanghai is a bustling city."
	};
	
	private Directory dir;//目录
	
	@Before
	public void setUp() throws Exception {
		dir=FSDirectory.open(Paths.get("D:\\lucene2"));//索引文件放置的位置
		IndexWriter writer=getWriter();//写索引
		for(int i=0;i<ids.length;i++){
			Document doc=new Document();
			doc.add(new StringField("id", ids[i], Field.Store.YES));//以string方式遍历id数组内容,YES表示储存索引字段值,能查询索引,能打印索引
			doc.add(new StringField("city",citys[i],Field.Store.YES));//以string方式遍历city数组内容
			doc.add(new TextField("desc", descs[i], Field.Store.NO));//以Text方式遍历desc数组内容,NO表示不储存字段值,能查询索引,不能打印索引
			writer.addDocument(doc); // 添加文档
		}
		writer.close();//关闭写索引
	}

	/**
	 * 获取IndexWriter实例
	 * @return
	 * @throws Exception
	 */
	private IndexWriter getWriter()throws Exception{
		Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
		IndexWriterConfig iwc=new IndexWriterConfig(analyzer);//写索引配置,配置一个标准分词器
		IndexWriter writer=new IndexWriter(dir, iwc);//新建写索引实例,需要传索引文件放置的位置路径和索引配置两个参数
		return writer;
	}
	
	/**
	 * 测试写了几个文档
	 * @throws Exception
	 */
	@Test
	public void testIndexWriter()throws Exception{
		IndexWriter writer=getWriter();//测试写一个素索引道指定路径
		System.out.println("写入了"+writer.numDocs()+"个文档");//打印写了几个索引文档
		writer.close();
	}
	
	/**
	 * 测试读取文档
	 * @throws Exception
	 */
	@Test
	public void testIndexReader()throws Exception{
		IndexReader reader=DirectoryReader.open(dir);//读取指定路径下的索引文件
		System.out.println("最大文档数:"+reader.maxDoc());//当未删除时,最大文档数和实际文档数一致
		System.out.println("实际文档数:"+reader.numDocs());
		reader.close();
	}
	
	/**
	 * 测试删除 在合并前
	 * @throws Exception
	 */
	@Test
	public void testDeleteBeforeMerge()throws Exception{
		IndexWriter writer=getWriter();
		System.out.println("删除前:"+writer.numDocs());
		writer.deleteDocuments(new Term("id","1"));
		writer.commit();
		System.out.println("writer.maxDoc():"+writer.maxDoc());
		System.out.println("writer.numDocs():"+writer.numDocs());
		writer.close();
	
		/* out:
		 *  删除前:3
		 *	writer.maxDoc():3
		 *  writer.numDocs():2
		 */
	}
	
	/**
	 * 测试删除 在合并后
	 * @throws Exception
	 */
	@Test
	public void testDeleteAfterMerge()throws Exception{
		IndexWriter writer=getWriter();
		System.out.println("删除前:"+writer.numDocs());
		writer.deleteDocuments(new Term("id","1"));
		writer.forceMergeDeletes(); // 强制删除
		writer.commit();
		System.out.println("writer.maxDoc():"+writer.maxDoc());
		System.out.println("writer.numDocs():"+writer.numDocs());
		writer.close();
		
		/* out:
		 * 	删除前:3
		 *  writer.maxDoc():2
		 *	writer.numDocs():2
		 */
	}

	/**
	 * 测试更新
	 * @throws Exception
	 */
	@Test
	public void testUpdate()throws Exception{
		IndexWriter writer=getWriter();
		Document doc=new Document();
		doc.add(new StringField("id", "1", Field.Store.YES));
		doc.add(new StringField("city","qingdao",Field.Store.YES));
		doc.add(new TextField("desc", "dsss is a city.", Field.Store.NO));
		writer.updateDocument(new Term("id","1"), doc);//更新文档操作,原索引依然能查询到,类似于新加入一个更改的索引
		writer.close();
	}
}


猜你喜欢

转载自blog.csdn.net/maonian1762/article/details/80863830