Lucene构建索引

首先 创建一个Maven项目


在pom.xml贴上相关依赖

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

在D盘创建一个Lucene2文件夹,用于存放生成的索引文件

代码

package com.gcx.lucene;

import java.io.IOException;
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;

@SuppressWarnings("all")
public class IndexTest {
	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));
			doc.add(new StringField("city",citys[i],Field.Store.YES));
			doc.add(new TextField("desc",descs[i],Field.Store.NO));
			writer.addDocument(doc);
		}
		writer.close();
	}
	/**
	 * 测试写了几个文档
	 * @throws Exception 
	 */
	@Test
	public void testIndexWriter() throws Exception{
		IndexWriter writer=getWriter();
		System.out.println("写入"+writer.maxDoc()+"文档");
	}
	/**
	 * 测试读取文档 
	 * @throws IOException 
	 */
	@Test
	public void testIndexReader() throws IOException{
		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());
		System.out.println("实际文档数:"+writer.numDocs());
		writer.close();
	}
	/**
	 * 删除文档在合并后(索引删除)
	 * @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());
		System.out.println("实际文档数:"+writer.numDocs());
		writer.close();
	}
	/**
	 * 测试更新(比较耗时,底层先删除再创键)
	 * @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","this is a city",Field.Store.NO));
		writer.updateDocument(new Term("id","1"), 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;
	}

}
生成的索引文件用 luke查看 百度自行下载,一搜就出来 双击


打开后,选择D:\\lucene2


然后就完事了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值