新建一个Java Project :LuceneTest
准备lucene的jar包,要加入的jar包至少有:
准备lucene的jar包,要加入的jar包至少有:
1) lucene-core-3.1.0.jar (核心包)新建实体类:Article,
2) lucene-analyzers-3.1.0.jar (分词器)
3) lucene-highlighter-3.1.0.jar (高亮器)
4) lucene-memory-3.1.0.jar (高亮器)
属性:id,title,content; getter和setter方法;
新建类HelloWorld:
package cn.hqu.helloworld;
import java.io.File;
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.Fieldable;
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.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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import cn.hqu.entity.Article;
/**
* 1、把article对象存放在索引库中
* 2、根据关键词把对象从索引库中提取出来
* @author Administrator
*
*/
public class HelloWorld {
@Test
public void testCreateIndex() throws Exception{
/**
* 1、创建一个article对象,并且把信息存放进去
* 2、调用indexWriter的API把数据存放在索引库中
* 3、关闭indexWriter
*/
//创建一个article对象,并且把信息存放进去
Article article = new Article();
article.setId(1L);
article.setTitle("lucene可以做搜索引擎");
article.setContent("baidu,google都是很好的搜索引擎");
//调用indexWriter的API把数据存放在索引库中
/**
* 创建一个IndexWriter
* 参数三个
* 1、索引库 指向索引库的位置
* 2、分词器
*/
//创建索引库
Directory directory = FSDirectory.open(new File("./indexDir"));
//创建分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
//把一个article对象转化成document
Document document = new Document();
Field idField = new Field("id",article.getId().toString(),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.ANALYZED);
document.add(idField);
document.add(titleField);
document.add(contentField);
indexWriter.addDocument(document);
//关闭indexWriter
indexWriter.close();
}
}
代码说明
步骤:
1) 创建IndexWriter对象
2) 把JavaBean转化为Document
3) 利用IndexWriter.addDocument方法增加索引
4) 关闭资源
运行testCreateIndex ,项目工程下多了一个
这样就把一个信息存到索引库了;
代码分析:
把信息放到索引库的过程
根据关键词把对象从索引库中提取出来
@Test
public void testSearchIndex() throws Exception{
/**
* 1、创建一个 IndexSearch对象
* 2、调用search方法进行检索
* 3、输出内容
*/
//创建一个 IndexSearch对象
Directory directory = FSDirectory.open(new File("./indexDir"));
IndexSearcher indexSearcher = new IndexSearcher(directory);
//调用search方法进行检索
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30,"title",analyzer);
Query query = queryParser.parse("lucene");//关键词
TopDocs topDocs = indexSearcher.search(query, 2);
int count = topDocs.totalHits;//根据关键词查询出来的总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for(ScoreDoc scoreDoc:scoreDocs){
float score = scoreDoc.score;//关键词得分
int index = scoreDoc.doc;//索引的下标
Document document = indexSearcher.doc(index);
//把document转化成article
Article article = new Article();
article.setId(Long.parseLong(document.get("id")));//document.getField("id").stringValue()
article.setTitle(document.get("title"));
article.setContent(document.get("content"));
articleList.add(article);
}
for(Article article:articleList){
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
代码说明
步骤:
1) 创建IndexSearch
2) 创建Query对象
3) 进行搜索
4) 获得总结果数和前N行记录ID列表
5) 根据目录ID列表把Document转为为JavaBean并放入集合中。
6) 循环出要检索的内容
例子说明
1) 执行两次建立引索
说明:执行两次同样的JavaBean数据增加的引索都能成功,说明JavaBean中的ID不是唯一确定索引的标示。在lucene中,唯一确定索引的标示(目录ID)是由lucene内部生成的。
2) 在搜索的时候,可以尝试用”Lucene”或者”lucene”来测试,结果是一样的。因为分词器把输入的关键字都变成小写。
3) 在建立索引和搜索索引的时候都用到了分词器。
4) 在索引库中存放的有目录和内容两大类数据。
5) Store这个参数表明是否将内容存放到索引库内容中。
6) Index这个参数表明是否存放关键字到索引目录中。
7)
搜索的 过程分析: