1 lucen目录介绍
2
lucene-core-3.6.2.jar是lucene开发核心jar包
contrib 目录存放,包含一些扩展jar包
3
案例
建立第一个lucene项目:lucene3_day1
(1)需要先将数据转换成为document对象,每一个数据信息转换成为field(string
name, string value, field.store store, field.indexindex)
(2)指定索引库位置directorydirectory = fsdirectory.open(new
file("index"));// 当前index目录
(3)分词器analyzeranalyzer =
new standardanalyzer(version.lucene_36);
(4)写入索引:
indexwriterconfig indexwriterconfig =
new
indexwriterconfig(
version.lucene_36, analyzer);
indexwriter indexwriter =
new indexwriter(directory,indexwriterconfig);
//将document数据写入索引库
indexwriter.adddocument(document);
//关闭索引
indexwriter.close();
案例编写:
案例目录:
article.java
package cn.toto.lucene.quickstart;
public
class article {
private
int
id;
private string
title;
content;
/**
* @return the
id
*/
int getid() {
return
}
* @param id
the id to set
void setid(int
id) {
this.id
= id;
title
public string gettitle() {
* @param title
the title to set
void settitle(string title) {
this.title
= title;
content
public string getcontent() {
* @param content
the content to set
void setcontent(string content) {
this.content
= content;
import java.io.file;
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.indexwriterconfig;
import org.apache.lucene.store.directory;
import org.apache.lucene.store.fsdirectory;
import org.apache.lucene.util.version;
import org.junit.test;
*
@brief lucenetest.java
测试lucene的案例
@attention
@author
toto-pc
@date 2014-12-7
@note begin modify by
涂作权 2014/12/07 null
class lucenetest {
@test
void buildindex()
throws exception {
article article = new article();
article.setid(100);
article.settitle("lucene快速入门");
article.setcontent("lucene是提供了一个简单却强大的应用程式接口,"
+ "能够做全文检索索引和搜寻,在java开发环境里lucene是"
+
"一个成熟的免费的开放源代码工具。");
//
将索引数据转换成为document对象(lucene要求)
document document = new document();
document.add(new field("id",
字段
article.getid() + "", store.yes,
是否建立索引
index.analyzed
表示使用分词索引
));
document.add(new field("title",
article.gettitle(), store.yes,index.analyzed));
document.add(new field("content",
article.getcontent(), store.yes, index.analyzed));
建立索引库
索引目录位置
directory directory = fsdirectory.open(new
file("index"));//
当前index目录
分词器
analyzer analyzer = new standardanalyzer(version.lucene_36);
写入索引
indexwriterconfig indexwriterconfig = new indexwriterconfig(
indexwriter indexwriter = new indexwriter(directory,
indexwriterconfig);
将document数据写入索引库
关闭索引
运行单元测试后的结果:
运行后index目录下的结果:
4
可以通过luke工具查看索引库中内容(它是一个jar包)
下载网址:http://code.google.com/p/luke/
打开方式:
如果用这种方式打不可以,可以用命令的方式打开文件,进入这个目录,选中shift+鼠标右键—>此处打开命令窗口—>输入命令:java
-jar lukeall-3.5.0.jar
工具的截图如下:
点击ok后的结果:
通过overview可以查看到索引信息,通过document可以查看文档对象信息
5
查找
和上面的并集的query代码如下:
void searchindex()
throws exception
{
//建立query对象--根据标题
string querystring = "lucene";
//第一个参数,版本号
//第二个参数,字段
//第三个参数,分词器
analyzer analyzer = new
standardanalyzer(version.lucene_36);
queryparser queryparser = new queryparser(version.lucene_36,"title",analyzer);
query query = queryparser.parse(querystring);
//根据query查找
file("index"));
indexsearcher indexsearcher = new indexsearcher(indexreader.open(directory));
//查询满足结果的前100条数据
topdocs topdocs = indexsearcher.search(query, 100);
system.out.println("满足结果记录条数:"
+ topdocs.totalhits);
//获取结果
scoredoc[] scoredocs = topdocs.scoredocs;
for (int
i = 0; i < scoredocs.length; i++) {
//先获得document下标
int docid = scoredocs[i].doc;
document document = indexsearcher.doc(docid);
system.out.println("id:"
+ document.get("id"));
system.out.println("title:"
+ document.get("title"));
system.out.println("content:"
+ document.get("content"));
indexsearcher.close();
运行结果:
luke查看的索引库内容:
索引库中信息,包括两大部分:
a
索引词条信息
b
文档对象信息
每个field中都存在一个store和一个index
索引内容和document内容有什么关系
查找时,通过索引内容
查找
索引的查找过程