第一个lucene程序
来源:互联网 发布:js构造函数模式 编辑:程序博客网 时间:2024/05/23 19:06
准备lucene的开发环境
搭建lucene的开发环境,要准备lucene的jar包,要加入的jar包至少有:
1) lucene-core-3.1.0.jar (核心包)
2) lucene-analyzers-3.1.0.jar (分词器)
3) lucene-highlighter-3.1.0.jar (高亮器)
4) lucene-memory-3.1.0.jar (高亮器)
Lucene的主页http://lucene.apache.org/。本文用的是3.0.1版本。
建立索引
开发代码
/** * 1.把article对象放在索引库中 * 2.根据关键词把对象从索引库中提取出来 * @author c * */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,goole都是很好的搜索引擎"); //调用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);////MaxFieldLength存储最大长度 //把一个article对象转化为document Document document = new Document(); /** * name 代表Key * value 代表value * store 表示是否存储在索引库中 * NO 不存储在索引库 * YES 存储在索引库 * * index 是否更新索引列表 * NO 表示在增加内容的是否,不往索引目录里添加 * ANALZER 表示在增加内容的时候,使用分词往目录里添加相应的目录 * NOT_ANALYZER 表示在增加内容的时候,整个字符串作为一个索引添加到相应的目录 */ 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) 关闭资源
搜索
public void testSearchIndex() throws Exception{ /** * 1、创建一个 IndexSearch对象 * 2、调用search方法进行检索 * 3、输出内容 */ //第一步:创建一个 IndexSearch对象 Directory directory = FSDirectory.open(new File("./indexDir")); IndexSearcher indexSearcher = new IndexSearcher(directory); //第二步:创建Query对象 //调用search方法进行检索 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); //创建分词器 /** * QueryParser的构造函数的三个参数 * Version.LUCENE_30 表示版本 * f Field 那个字段 * Analyzer 分词器 */ QueryParser queryParser = new QueryParser(Version.LUCENE_30,"title",analyzer); Query query = queryParser.parse("lucene");//关键词 //第三步:进行搜索 /** * search的参数 * query 表示搜索条件 * n 表示显示前N行记录 */ TopDocs topDocs = indexSearcher.search(query, 2); //第四步:获得总记录数和前N行的目录ID列表 int count = topDocs.totalHits;//根据关键词查询出来的总的记录数 ScoreDoc[] scoreDocs = topDocs.scoreDocs;//获取前N行目录的ID列表 //第五步:根据目录的行ID获取每行的document,并把Document转化为Article放入集合中 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")); //把Article加入集合中 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这个参数表明是否存放关键字到索引目录中。
- 第一个lucene程序
- lucene第一个小程序
- lucene001 第一个Lucene程序
- Lucene学习笔记:第一个程序
- LUCENE第一个小程序(有错)
- 我的第一个 lucene程序
- Lucene入门,第一个HelloWorld程序
- 写了第一个lucene程序(lucene学习笔记)
- 第一个lucene例子
- 第一个Lucene实例
- Lucene之——第一个Lucene3.0程序
- 【Lucene】创建第一个Lucene索引库
- 第一个Lucene.NET搜索引挚
- 中文分词-lucene 第一个版本
- 第一个C++程序
- 第一个MIDP程序
- 第一个Swing程序
- 第一个Ruby程序
- JAVA实现图的基本操作——生成邻接表结构的图、输出邻接矩阵、深度优先遍历
- 【笔试】Webank 判断4条线是否能够连成平行坐标轴的矩形
- A. Mike and palindrome
- 赢球票
- Mac版myeclipse破解
- 第一个lucene程序
- UGUI 列表循环使用
- 第一章 51单片机环境的搭建-1.3指令下载/烧写软件
- 数据结构与算法--二叉树子结构问题
- Train Problem I ( 顺序是不是栈操作)
- 012.栈应用Hanoi递归C实例
- [AHK]用ahk执行VBScript脚本
- 《水知道答案2》观后小结
- E