lucene入门DEMO2

简单的JavaBean

package com.pdsu.lucene.simple;

/**
 * 类说明:
 * 
 * @author 作者: LiuJunGuang
 * @version 创建时间:2011-12-29 下午04:40:39
 */
public class Article {
	private int id;
	private String title;
	private String content;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Article(int id, String title, String content) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
	}

	public Article() {
		super();
	}

	@Override
	public String toString() {
		return "Article [id=" + id + ", title=" + title + ", content="
				+ content + "]";
	}

}

文章工具类:
package com.pdsu.lucene.simple;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

/**
 * 类说明: Articlede
 * 
 * @author 作者: LiuJunGuang
 * @version 创建时间:2011-12-29 下午05:44:02
 */
public class ArticleUtils {

	public static Document article2Document(Article article) {
		Document document = new Document();
		Field idField = new Field("id", String.valueOf(article.getId()),
				Store.YES, Index.NOT_ANALYZED);
		Field titleField = new Field("title", article.getTitle(), Store.YES,
				Index.ANALYZED);
		Field contentField = new Field("content", article.getContent(),
				Store.YES, Index.NOT_ANALYZED);
		document.add(idField);
		document.add(titleField);
		document.add(contentField);
		return document;
	}

	public static  Article document2Article(Document document) {
		return new Article(Integer.parseInt(document.get("id")),
				document.get("title"), document.get("content"));
	}
}
Lucene工具类
package com.pdsu.lucene.simple;

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

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * 类说明:Lucene工具类
 * @author 作者: LiuJunGuang
 * @version 创建时间:2011-12-29 下午05:49:06
 */
public class LuceneUtils {

	/**
	 * 存储目录
	 */
	private  static  Directory directory;
	
	/**
	 * indexWriter
	 */
	private static IndexWriter indexWriter;
	/**
	 * 分词器
	 */
	private static Analyzer analyzer;
	static {
		try {
			directory = FSDirectory.open(new File("./store")); // 数据存储目录
			/*
			 * 内存索引库特点:
			 * 		访问数据的速度比较快
			 * 		数据只是暂时的
			 */
			//directory = RAMDirectory.open(); //内存数据存取目录
			 analyzer = new StandardAnalyzer(Version.LUCENE_30);// 分词器
			 indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private LuceneUtils() {
		super();
	}
	/**
	 * 取得存取目录
	 * @return
	 */
	public static Directory getDirectory(){
		return directory;
	}
	/**
	 * 获得IndexWriter
	 * @return
	 */
	public static IndexWriter getIndexWriter(){
		return indexWriter;
	}
	/**
	 *获得分词器
	 * @return
	 */
	public static Analyzer getAnalyzer(){
		return analyzer;
	}
}

Lucene简单的增删改查
package com.pdsu.lucene.simple;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
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.util.Version;
import org.junit.Test;


/**
 * 类说明:
 * 
 * @author 作者: LiuJunGuang
 * @version 创建时间:2011-12-29 下午05:54:29
 */
public class LuceneTest {
	@Test
	public void add() throws Exception {
		IndexWriter indexWriter = LuceneUtils.getIndexWriter();
		/*
		 * 合并的代码写在添加之前
		 */
		indexWriter.setMergeFactor(2);//当超过两个索引时,就合并成一个
		//indexWriter.optimize();//手动合并索引库
		indexWriter.addDocument(ArticleUtils.article2Document(new Article(1,"hello world!","hello content!")));
		indexWriter.commit();
		indexWriter.close();
	}

	@Test
	public void del() throws Exception {
		IndexWriter indexWriter = LuceneUtils.getIndexWriter();
		Term term = new Term("id", "1");
		indexWriter.deleteDocuments(term);
		indexWriter.commit();
		indexWriter.close();
	}

	@Test
	public void update() throws CorruptIndexException, IOException {
		IndexWriter indexWriter = LuceneUtils.getIndexWriter();
		Term term = new Term("id", "2");
		Document doc = ArticleUtils.article2Document(new Article(2,"hello world!","hello content!"));
		indexWriter.updateDocument(term, doc);
		indexWriter.commit();
		indexWriter.close();
	}

	@Test
	public void query() throws Exception {
		IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.getDirectory());
		QueryParser qp = new QueryParser(Version.LUCENE_30, "title", LuceneUtils.getAnalyzer());
		Query query = qp.parse("hello");
		TopDocs tops = indexSearcher.search(query, 10);
		//获得记录总条数
		int count = tops.totalHits;
		ScoreDoc []scoreDocs =  	tops.scoreDocs;//得到目录
		List<Article> articles = new ArrayList<Article>();
		for (int i = 0; i < scoreDocs.length; i++) {
			float score = scoreDocs[i].score;//相关度得分
			int doc = scoreDocs[i].doc;
			Document document = indexSearcher.doc(doc);
			Article article = ArticleUtils.document2Article(document);
			articles.add(article);
		}
		System.out.println(articles);
	}
}




你可能感兴趣的:(exception,String,Lucene,query,Class,工具)