Lucene实例代码

 lunece需要下载的jar文件在官方有下载

http://www.apache.org/dyn/closer.cgi/lucene/java/5.1.0

1,cn.jiayen.Dao

------------------------

package cn.jiayen.Dao;
import cn.jiayen.domain.Article;
import cn.jiayen.domain.QueryResult;

public interface LuceneDaoInterFace {
public abstract void save(Article article);
public abstract void delete(Integer id)
public abstract void update(Article article);
public abstract QueryResult<Article> query(String queryString, int firstResult,
int maxResult);
}

----------------------

package cn.jiayen.Dao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
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 cn.jiayen.Util.LuceneUtil;
import cn.jiayen.domain.Article;
import cn.jiayen.domain.QueryResult;
/**
 * 关于lucene的优势,直接把数据库的文件先写入到文件中,
 * 在文件中进行增删改查(在这里可以同时改数据库的值),从而实现 了大数据的查询时间
 *  
 * @author hwz 2015-4-30下午12:48:05
 */
public class LuceneDao implements LuceneDaoInterFace {
IndexWriter indexWriter = LuceneUtil.getIndexWriter();
//添加索引库
@Override
public void save(Article article){
try {
indexWriter.addDocument(LuceneUtil.getArticle2doucment(article));
indexWriter.commit();
} catch (IOException e) {
LuceneUtil.rollBack();
e.printStackTrace();
}
}
@Override
public void delete(Integer id){
// IndexWriter indexWriter = LuceneUtil.getIndexWriter();
Term term = new Term("id", id.toString());
try {
indexWriter.deleteDocuments(term);
indexWriter.commit();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void update(Article article){
//更新是先删除,再创建
// IndexWriter indexWriter = LuceneUtil.getIndexWriter();
Term term = new Term("id", article.getId().toString());
try {
indexWriter.updateDocument(term, LuceneUtil.getArticle2doucment(article));
}catch (IOException e) {
e.printStackTrace();
}
}
@Override
public  QueryResult<Article> query(String queryString,int firstResult ,int maxResult){
IndexSearcher indexSearcher = null;
try {
//把查询结构转换为Query对象
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30,new String[]{"content","title"}, LuceneUtil.getAnalyzer());
Query query = queryParser.parse(queryString);
//执行查询结果
indexSearcher = new IndexSearcher(LuceneUtil.getDirecotry());
TopDocs topDocs = indexSearcher.search(query, 1000);
int count = topDocs.totalHits;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//分页处理
int endIndex = Math.min(firstResult+maxResult, scoreDocs.length);
//处理查询结果
List<Article> list = new ArrayList<Article>();
for (int i = firstResult; i < endIndex; i++) {
// float score = scoreDocs[i].score;//相关度得分
//根据编号获取document对象
int docNo = scoreDocs[i].doc;//内部编号
Document document  = indexSearcher.doc(docNo);
//把document对象转换为article,添加到集合
Article article = LuceneUtil.getDoucment2article(document);
list.add(article);
}
return new QueryResult<Article>(list, count);
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if (indexSearcher != null) {
try {
indexSearcher.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2,cn.jiayen.domain

---------------------

package cn.jiayen.domain;


public class Article {
private Integer id;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}

}

----------------------------

package cn.jiayen.domain;


import java.util.List;


public class QueryResult<T> {
private List<T> list;
private int count;

public QueryResult(List<T> list, int count) {
this.list = list;
this.count = count;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}

}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3,cn.jiayen.lunece

---------------------------

package cn.jiayen.lunece;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.management.RuntimeErrorException;
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.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
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;
import cn.jiayen.domain.Article;

public class Lucene {
//初始化存放文件位置
private static Directory directory;
private static Analyzer analyzer;
static {
try {
directory = FSDirectory.open(new File("./indexDir/"));
analyzer = new StandardAnalyzer(Version.LUCENE_30);
} catch (IOException e) {
throw  new RuntimeException(e);
}
 
}
//创建索引
@Test
public  void createIndex() throws Exception {
//实例article
Article article = new Article();
article.setId(1);
article.setTitle("lucene实例");
article.setContent("搜索的内容是lucene");
//��Articleת��Ϊdocument
Document document = new Document();
document.add(new Field("title", article.getTitle(), Store.YES, Index.ANALYZED));
document.add(new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED));
document.add(new Field("content", article.getContent(), Store.YES, Index.ANALYZED));

//把文档写入索引中,也就是写入的硬盘中
IndexWriter index = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
index.addDocument(document);
index.close();
}
//测试查询,此处可以返回一个list给javaweb使用
@Test
public void testSearch() throws Exception {
String querString = "搜索";

//根据要查询的子符串,得到query对象
QueryParser queryParser = new QueryParser(Version.LUCENE_30,"content",analyzer);
Query query = queryParser.parse(querString);
//更具directory获取索引
IndexSearcher search = new IndexSearcher(directory);
//TopDocs 搜索返回的结果  
TopDocs topDocs = search.search(query, 100);//只返回前100条记录 
int count = topDocs.totalHits;//搜索结果总数量
ScoreDoc[] sDocs = topDocs.scoreDocs;//搜索返回的结果集合 

List<Article> list = new ArrayList<Article>();
//遍历出document的值放入article中
for (ScoreDoc doc : sDocs) {
/*score lucene的评分是有一套自己的机制的,
输入某一个关键字,lucene会对命中的记录进行评分,默认情况下,分数越高的结果会排在结果的越前面。
如果在创建索引的时候,没有对某个域进行加权,
那么默认分数的上限是5分,如果有对域做加权,检索结果的评分可能会出现大于5分的情况。*/
float score = doc.score;//记录评分
System.out.println("记录:"+score);
//搜索到的结果记录,一个docNo就是一个记录号
int docNo = doc.doc;//
System.out.println("docNo:"+docNo);
Document document = search.doc(docNo);
//把从document中搜索到的值赋给article然后放在list中
Article article = new Article();
article.setId(Integer.parseInt(document.get("id")));
article.setTitle(document.get("title"));
article.setContent(document.get("content"));
list.add(article);
}
search.close();
System.out.println("搜索到"+count+"数据");
//打印搜索到的数据
for(Article article : list){
System.out.println("ID是:"+article.getId());
System.out.println("titel是:"+article.getTitle());
System.out.println("content是:"+article.getContent());

}
}
public static void main(String[] args) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
System.out.println("��ʼִ������:"+System.currentTimeMillis());
// timer.cancel();
}
}, 0,3000);
System.out.println("׼��ִ������:"+System.currentTimeMillis());
}

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4,cn.jiayen.Test

-------------------------------

package cn.jiayen.Test;
import java.util.List;
import org.junit.Test;
import cn.jiayen.Dao.LuceneDao;
import cn.jiayen.Dao.LuceneDaoInterFace;
import cn.jiayen.domain.Article;
import cn.jiayen.domain.QueryResult;
public class LuceneDaoTest {
LuceneDaoInterFace lucene = new LuceneDao();
@Test
public void testSave() {
for (int i = 0; i < 60; i++) {
Article article = new Article();
article.setId(i);
article.setTitle("lucene搜索"+i);
article.setContent("没有搜索功能的时候就是这样的咯哈哈,没有搜索功能的时候就是这样的咯哈哈没有搜索功能的时候就是这样的咯哈哈没有搜索功能的时候就是这样的咯哈哈"+i);
lucene.save(article);
System.out.println(i);
}
}


@Test
public void testDelete() {
for (int i = 0; i < 150; i++) {
int id = i;
lucene.delete(id);
}
// lucene.delete(4);
}


@Test
public void testUpdate() {
Article article = new Article();
article.setId(2);
article.setTitle("lucene搜索");
article.setContent("这是改过的结果.........");
lucene.update(article);
}


@Test
public void testQuery() {
String queryString = "搜索";
QueryResult<Article> queryResult = lucene.query(queryString, 0,10);
List<Article> list = queryResult.getList();
System.out.println("共有"+queryResult.getCount()+"条数据..");
for(Article article:list){
System.out.println("》》》》》》》》》ID:"+article.getId());
System.out.println("titel:"+article.getTitle());
System.out.println("content:"+article.getContent());
}
}

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5,cn.jiayen.Util

---------------------------

package cn.jiayen.Util;
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.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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import cn.jiayen.domain.Article;


public class LuceneUtil {
private static Directory direcotry;
private static Analyzer analyzer;
private static IndexWriter indexWriter;

static{
try {
direcotry = FSDirectory.open(new File("./indexDir/"));
analyzer = new StandardAnalyzer(Version.LUCENE_30);
indexWriter = new IndexWriter(direcotry, analyzer, MaxFieldLength.LIMITED);
System.out.println("初始化IndexWriter.....");
//在推出jvm时关闭indexWriter
Runtime.getRuntime().addShutdownHook(new Thread(){


@Override
public void run() {
try {
indexWriter.close();
System.out.println("关闭IndexWriter");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Directory getDirecotry() {
return direcotry;
}
public static Analyzer getAnalyzer() {
return analyzer;
}
public static IndexWriter getIndexWriter() {
return indexWriter;
}
public static Document getArticle2doucment(Article article) {
Document document = new Document();
document.add(new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED));
document.add(new Field("title", article.getTitle(), Store.YES, Index.ANALYZED));
document.add(new Field("content", article.getContent(), Store.YES, Index.ANALYZED));
return document;
}
public static Article getDoucment2article(Document document) {
Article article = new Article();
article.setId(Integer.parseInt(document.get("id")));
article.setTitle(document.get("title"));
article.setContent(document.get("content"));
return article;
}
//让IndexWriter回滚更改
public static void rollBack(){
try {
indexWriter.rollback();
} catch (IOException e) {
throw new RuntimeException();
}

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值