当前访客身份:游客 [ 登录  | 注册加入尚学堂]
直播

小鸟飞

拥有积分:3
这家伙太懒,还没有签名!

博客分类

笔记中心

课题中心

提问中心

答题中心

解答题中心

Lucene4.4学习案例-基本操作

小鸟飞 发表于 2年前 (2014-09-28 05:03:30)  |  评论(1)  |  阅读次数(744)| 0 人收藏此文章,   我要收藏   
package com.bean.lucene.util;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * Lucene模版工具类
 * @author 周景
 *
 */
public class LuceneTemplet {
	
	/**
	 * 索引库URL
	 */
	public static final String INDEX_URL = System.getProperty("user.dir") + File.separator + "index";
	/**
	 * 索引目录
	 */
	private static Directory directory;
	/**
	 * 读索引
	 */
	private static DirectoryReader reader;
	/**
	 * 写索引
	 */
	private static IndexWriter writer;

	/**
	 * 加载
	 */
	static {
		try {
			directory = FSDirectory.open(new File(INDEX_URL));
			reader = DirectoryReader.open(directory);
			writer = new IndexWriter(directory, new IndexWriterConfig( Version.LUCENE_44, new StandardAnalyzer(Version.LUCENE_44) ));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取索引搜索对象
	 * @return
	 */
	public static IndexSearcher getSearcher() {
		try {
			DirectoryReader r = DirectoryReader.openIfChanged(reader);
			if (r != null) {
				reader.close();
				reader = r;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new IndexSearcher(reader);
	}
	
	/**
	 * 创建索引
	 * @param ci
	 */
	public static void writer(WriterIndex w) {
		w.writer(writer);
	}
	
	/**
	 * 搜索索引
	 * @param si
	 */
	public static void search(SearchIndex s) {
		s.search(getSearcher());
	}
	
}

/**
 * 操作索引抽象类
 * @author 周景
 * 
 */
abstract class WriterIndex {
	/**
	 * 操作索引
	 */
	public abstract void writer(IndexWriter writer);
}

/**
 * 搜索索引抽象类
 * @author 周景
 * 
 */
abstract class SearchIndex {
	/**
	 * 搜索索引
	 */
	public abstract void search(IndexSearcher searcher);
}

package com.bean.lucene.util;

import static org.junit.Assert.*;

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.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class LuceneTempletTest {
	
	/**
	 * 初始化索引
	 */
	@Test
	public void createrIndex() {
		final int[] ids = {1, 2, 3};
		final String[] titles =  {"title1", "title2", "title3"};
		final String[] contents = {"hello world love Anthena!", "my go Home. Go To Home", "I love you too Home love."};
		
		LuceneTemplet.writer(new WriterIndex() {
			@Override
			public void writer(IndexWriter writer) {
				Document doc = null;
				try {
					for (int i=0; i<ids.length; i++) {
						doc = new Document();
						doc.add(new IntField("id", ids[i], Field.Store.YES));
						doc.add(new StringField("title", titles[i], Field.Store.YES));
						doc.add(new TextField("content", contents[i], Field.Store.YES));
						writer.addDocument(doc);
					}
					writer.commit();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 查看索引数量
	 */
	@Test
	public void lookIndexCount() {
		LuceneTemplet.search(new SearchIndex() {
			@Override
			public void search(IndexSearcher searcher) {
				IndexReader reader = searcher.getIndexReader();
				System.out.println("NumDocs:" + reader.numDocs() + "\tMaxDocs:" + reader.maxDoc());
			}
		});
	}
	
	/**
	 * 清空索引
	 */
	@Test
	public void clearIndex() {
		LuceneTemplet.writer(new WriterIndex() {
			@Override
			public void writer(IndexWriter writer) {
				try {
					writer.deleteAll();
					writer.commit();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 精准搜索
	 */
	@Test
	public void search1() {
		LuceneTemplet.search(new SearchIndex() {
			@Override
			public void search(IndexSearcher searcher) {
				try {
					TermQuery query = new TermQuery(new Term("content", "love"));
					TopDocs docs = searcher.search(query, 10);
					for (ScoreDoc d : docs.scoreDocs) {
						Document doc = searcher.doc(d.doc);
						System.out.println(d.doc + "-id:" + doc.get("id") + "\ttitle:" + doc.get("title") + "\tcontent:" + doc.get("centent"));
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 通用搜索
	 */
	@Test
	public void search2() {
		LuceneTemplet.search(new SearchIndex() {
			@Override
			public void search(IndexSearcher searcher) {
				try {
					QueryParser parser = new QueryParser(Version.LUCENE_44, "content", new StandardAnalyzer(Version.LUCENE_44));
					Query query = parser.parse("love you");
					TopDocs docs = searcher.search(query, 10);
					for (ScoreDoc d : docs.scoreDocs) {
						Document doc = searcher.doc(d.doc);
						System.out.println(d.doc + "-id:" + doc.get("id") + "\ttitle:" + doc.get("title") + "\tcontent:" + doc.get("centent"));
					}
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		});
	}

}

 

所需jar包:

lucene-analyzers-common-4.4.0.jar

lucene-core-4.4.0.jar

lucene-queryparser-4.4.0.jar

分享到:0
关注微信,跟着我们扩展技术视野。每天推送IT新技术文章,每周聚焦一门新技术。微信二维码如下:
微信公众账号:尚学堂(微信号:bjsxt-java)
北京总部地址:北京市海淀区西三旗桥东建材城西路85号神州科技园B座三层尚学堂 咨询电话:400-009-1906 010-56233821
Copyright 2007-2015 北京尚学堂科技有限公司 京ICP备13018289号-1 京公网安备11010802015183