lucene简单入门demo

package com.pdsu.lucene;

/**
 * 类说明:
 * 
 * @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 + "]";
	}

}

lucene简单应用


package com.pdsu.lucene;

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

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.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

/**
 * 类说明:
 * 
 * @author 作者: LiuJunGuang
 * @version 创建时间:2011-12-29 下午04:42:21
 */

public class Lucene {

	@Test
	public void insert() throws IOException {
		Article article = new Article(1, "lucene is very good!",
				"lucene very good!,lucene very good!");

		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);// 分词器
		Directory dir = FSDirectory.open(new File("./store")); // 数据存储目录
		IndexWriter index = new IndexWriter(dir, analyzer,
				MaxFieldLength.LIMITED);
		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);
		index.addDocument(document);// 添加文档对象
		index.close();
	}
	
	@Test
	public  void query() throws Exception{
		Directory dir = FSDirectory.open(new File("./store")); // 数据存储目录
		IndexSearcher indexSearcher = new IndexSearcher(dir);
		
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);// 分词器
		QueryParser qp = new QueryParser(Version.LUCENE_30, "title", analyzer);
		Query query = qp.parse("lucene");
		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 = new Article(Integer.parseInt(document.get("id")), document.get("title"),document.get("content"));
			articles.add(article);
		}
		System.out.println(articles);
	}
}


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