Professional Documents
Culture Documents
Lucene教程 简单Demo
Lucene教程 简单Demo
1 lucene 简介
1.1 什么是 lucene
Lucene 是一个全文搜索框架,而不是应用产品。因此它并不像 www.baidu.com 或者
google Desktop 那么拿来就能用,它只是提供了一种工具让你能实现这些产品。
2 lucene 的工作方式
lucene 提供的服务实际包含两部分:一入一出。所谓入是写入,即将你提供的源(本
质是字符串)写入索引或者将其从索引中删除;所谓出是读出,即向用户提供全文搜
索服务,让用户可以通过关键词定位源。
2.1 写入流程
源字符串首先经过 analyzer 处理,包括:分词,分成一个个单词;去除 stopword(可
选)。
将源中需要的信息加入 Document 的各个 Field 中,并把需要索引的 Field 索引起来,把
需要存储的 Field 存储起来。
将索引写入存储器,存储器可以是内存或磁盘。
2.2 读出流程
用户提供搜索关键词,经过 analyzer 处理。
对处理后的关键词搜索索引找出对应的 Document。
用户根据需要从找到的 Document 中提取需要的 Field。
3 一些需要知道的概念
3.1 analyzer
Analyzer 是分析器,它的作用是把一个字符串按某种规则划分成一个个词语,并去
除其中的无效词语,这里说的无效词语是指英文中的“of”、“the”,中文中的“的”、
“地”等词语,这些词语在文章中大量出现,但是本身不包含什么关键信息,去掉有
利于缩小索引文件、提高效率、提高命中率。
分词的规则千变万化,但目的只有一个:按语义划分。这点在英文中比较容易实
现,因为英文本身就是以单词为单位的,已经用空格分开;而中文则必须以某种方法
将连成一片的句子划分成一个个词语。具体划分方法下面再详细介绍,这里只需了解分
析器的概念即可。
3.2 document
用户提供的源是一条条记录,它们可以是文本文件、字符串或者数据库表的一条记
录等等。一条记录经过索引之后,就是以一个 Document 的形式存储在索引文件中的。用
户进行搜索,也是以 Document 列表的形式返回。
3.3 field
一个 Document 可以包含多个信息域,例如一篇文章可以包含“标题”、“正文”、
“最后修改时间”等信息域,这些信息域就是通过 Field 在 Document 中存储的。
Field 有两个属性可选:存储和索引。通过存储属性你可以控制是否对这个 Field 进行
存储;通过索引属性你可以控制是否对该 Field 进行索引。这看起来似乎有些废话,事
实上对这两个属性的正确组合很重要,下面举例说明:还是以刚才的文章为例子,我
们需要对标题和正文进行全文搜索,所以我们要把索引属性设置为真,同时我们希望
能直接从搜索结果中提取文章标题,所以我们把标题域的存储属性设置为真,但是由
于正文域太大了,我们为了缩小索引文件大小,将正文域的存储属性设置为假,当需
要时再直接读取文件;我们只是希望能从搜索解果中提取最后修改时间,不需要对它
进行搜索,所以我们把最后修改时间域的存储属性设置为真,索引属性设置为假。上面
的三个域涵盖了两个属性的三种组合,还有一种全为假的没有用到,事实上 Field 不允
许你那么设置,因为既不存储又不索引的域是没有意义的。
3.4 term
term 是搜索的最小单位,它表示文档的一个词语,term 由两部分组成:它表示的
词语和这个词语所出现的 field。
3.5 tocken
tocken 是 term 的一次出现,它包含 trem 文本和相应的起止偏移,以及一个类型字符
串。一句话中可以出现多次相同的词语,它们都用同一个 term 表示,但是用不同的
tocken,每个 tocken 标记该词语出现的地方。
3.6 segment
添加索引时并不是每个 document 都马上添加到同一个索引文件,它们首先被写入到
不同的小文件,然后再合并成一个大索引文件,这里每个小文件都是一个 segment。
4 如何建索引
4.1 最简单的能完成索引的代码片断
IndexWriter writer = new IndexWriter(“/data/index/”, new StandardAnalyzer(), true);
Document doc = new Document();
doc.add(new Field("title", "lucene introduction", Field.Store.YES,
Field.Index.TOKENIZED));
doc.add(new Field("content", "lucene works well", Field.Store.YES,
Field.Index.TOKENIZED));
writer.addDocument(doc);
writer.optimize();
writer.close();
下面我们分析一下这段代码。
首先我们创建了一个 writer,并指定存放索引的目录为“/data/index”,使用的分析器为
StandardAnalyzer,第三个参数说明如果已经有索引文件在索引目录下,我们将覆盖它
们。然后我们新建一个 document。
我们向 document 添加一个 field,名字是“title”,内容是“lucene introduction”,对
它进行存储并索引。再添加一个名字是“content”的 field,内容是“lucene works well”,
也是存储并索引。
然后我们将这个文档添加到索引中,如果有多个文档,可以重复上面的操作,创建
document 并添加。
添加完所有 document,我们对索引进行优化,优化主要是将多个 segment 合并到一
个,有利于提高索引速度。
随后将 writer 关闭,这点很重要。
对,创建索引就这么简单!
当然你可能修改上面的代码获得更具个性化的服务。
4.2 索引文本文件
如果你想把纯文本文件索引起来,而不想自己将它们读入字符串创建 field,你可以
用下面的代码创建 field:
Field field = new Field("content", new FileReader(file));
这里的 file 就是该文本文件。该构造函数实际上是读去文件内容,并对其进行索引,
但不存储。
Lucene 2 教程
例子一 :
而"2.txt"和"3.txt"的内容也可以随便写几写,这里懒
写,就复制一个和 1.txt 文件的内容一样吧
索引的结果:
File C:\s\ 1 .txt 正在被索引.
中华人民共和国全国人民 2006 年
File C:\s\ 2 .txt 正在被索引.
中华人民共和国全国人民 2006 年
File C:\s\ 3 .txt 正在被索引.
中华人民共和国全国人民 2006 年
这花费了 297 毫秒来把文档增加到索引里面去 ! c:\s
3、建立了索引之后,查询啦....
package lighter.javaeye.com;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class TestQuery {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null ;
String queryString = " 中华 " ;
Query query = null ;
IndexSearcher searcher = new IndexSearcher( " c:\\index " );
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser( " body " , analyzer);
query = qp.parse(queryString);
} catch (ParseException e) {
}
if (searcher != null ) {
hits = searcher.search(query);
if (hits.length() > 0 ) {
System.out.println( " 找到: " + hits.length() + " 个结果! " );
}
}
}
}
其运行结果:
找到: 3 个结果 !
Lucene 其实很简单的,它最主要就是做两件事:建立索
引和进行搜索
来看一些在 lucene 中使用的术语,这里并不打算作详
细的介绍,只是点一下而已----因为这一个世界有一种
好东西,叫搜索。
IndexWriter:lucene 中最重要的的类之一,它主要是用
来将文档加入索引,同时控制索引过程中的一些参数
使用。
Analyzer:分析器,主要用于分析搜索引擎遇到的各种
文本。常用的有 StandardAnalyzer 分析器,StopAnalyzer
分析器,WhitespaceAnalyzer 分析器等。
Directory:索引存放的位置;lucene 提供了两种索引存
放的位置,一种是磁盘,一种是内存。一般情况将索
引放在磁盘上;相应地 lucene 提供了 FSDirectory 和
RAMDirectory 两个类。
Document:文档;Document 相当于一个要进行索引的
单元,任何可以想要被索引的文件都必须转化为
Document 对象才能进行索引。
Field:字段。
Query:查询,lucene 中支持模糊查询,语义查询,短
语查询,组合查询等等,如有
TermQuery,BooleanQuery,RangeQuery,WildcardQuery
等一些类。
QueryParser: 是一个解析用户输入的工具,可以通过
扫描用户输入的字符串,生成 Query 对象。
Hits:在搜索完成之后,需要把搜索结果返回并显示给
用户,只有这样才算是完成搜索的目的。在 lucene 中,
搜索的结果的集合是用 Hits 类的实例来表示的。
上面作了一大堆名词解释,下面就看几个简单的实例
吧:
1、简单的的 StandardAnalyzer 测试例子
package lighter.javaeye.com;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
public class StandardAnalyzerTest
{
// 构造函数,
public StandardAnalyzerTest()
{
}
public static void main(String[] args)
{
// 生成一个 StandardAnalyzer 对象
Analyzer aAnalyzer = new StandardAnalyzer();
// 测试字符串
StringReader sr = new StringReader( " lighter javaeye com is the are on " );
// 生成 TokenStream 对象
TokenStream ts = aAnalyzer.tokenStream( " name " , sr);
try {
int i = 0 ;
Token t = ts.next();
while (t != null )
{
// 辅助输出时显示行号
i ++ ;
// 输出处理后的字符
System.out.println( " 第 " + i + " 行: " + t.termText());
// 取得下一个字符
t = ts.next();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
显示结果:
第 1 行:lighter
第 2 行:javaeye
第 3 行:com
提示一下:
StandardAnalyzer 是 lucene 中内置的"标准分析器",可
以做如下功能:
1、对原有句子按照空格进行了分词
2、所有的大写字母都可以能转换为小写的字母
3、可以去掉一些没有用处的单词,例
如"is","the","are"等单词,也删除了所有的标点
查看一下结果与"new StringReader("lighter javaeye
com is the are on")"作一个比较就清楚明了。
这里不对其 API 进行解释了,具体见 lucene 的官方文
档。需要注意一点,这里的代码使用的是 lucene2 的
API,与 1.43 版有一些明显的差别。
2、看另一个实例,简单地建立索引,进行搜索
package lighter.javaeye.com;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;
public class FSDirectoryTest {
// 建立索引的路径
public static final String path = " c:\\index2 " ;
public static void main(String[] args) throws Exception {
Document doc1 = new Document();
doc1.add( new Field( " name " , " lighter javaeye com
" ,Field.Store.YES,Field.Index.TOKENIZED));
Document doc2 = new Document();
doc2.add( new Field( " name " , " lighter blog
" ,Field.Store.YES,Field.Index.TOKENIZED));
IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true ),
new StandardAnalyzer(), true );
writer.setMaxFieldLength( 3 );
writer.addDocument(doc1);
writer.setMaxFieldLength( 3 );
writer.addDocument(doc2);
writer.close();
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null ;
Query query = null ;
QueryParser qp = new QueryParser( " name " , new StandardAnalyzer());
query = qp.parse( " lighter " );
hits = searcher.search(query);
System.out.println( " 查找\ " lighter\ " 共 " + hits.length() + " 个结果 " );
query = qp.parse( " javaeye " );
hits = searcher.search(query);
System.out.println( " 查找\ " javaeye\ " 共 " + hits.length() + " 个结果 " );
}
}
运行结果:
查找 " lighter " 共 2 个结果
查找 " javaeye " 共 1 个结果
其实索引目录有两种格式,
2.索引文件可放的位置:
索引可以存放在两个地方 1.硬盘,2.内存
放在硬盘上可以用 FSDirectory(),放在内存的用
RAMDirectory()不过一关机就没了
FSDirectory.getDirectory(File file, boolean create)
FSDirectory.getDirectory(String path, boolean create)
两个工厂方法返回目录
New RAMDirectory()就直接可以
再和
IndexWriter(Directory d, Analyzer a, boolean create)
一配合就行了
如:
IndexWrtier indexWriter = new
IndexWriter(FSDirectory.getDirectory(“c:\\index”, true ), new StandardAnlyazer(),
true );
IndexWrtier indexWriter = new IndexWriter( new RAMDirectory(), new
StandardAnlyazer(), true );
3.索引的合并
这个可用
IndexWriter.addIndexes(Directory[] dirs)
将目录加进去
来看个例子:
public void UniteIndex() throws IOException
{
IndexWriter writerDisk = new IndexWriter(FSDirectory.getDirectory( " c:\\
indexDisk " , true ), new StandardAnalyzer(), true );
Document docDisk = new Document();
docDisk.add( new Field( " name " , " 程 序 员 之 家
" ,Field.Store.YES,Field.Index.TOKENIZED));
writerDisk.addDocument(docDisk);
RAMDirectory ramDir = new RAMDirectory();
IndexWriter writerRam = new IndexWriter(ramDir, new StandardAnalyzer(),
true );
Document docRam = new Document();
docRam.add( new Field( " name " , " 程 序 员 杂 志
" ,Field.Store.YES,Field.Index.TOKENIZED));
writerRam.addDocument(docRam);
writerRam.close(); // 这个方法非常重要,是必须调用的
writerDisk.addIndexes( new Directory[] {ramDir} );
writerDisk.close();
}
public void UniteSearch() throws ParseException, IOException
{
QueryParser queryParser = new QueryParser( " name " , new
StandardAnalyzer());
Query query = queryParser.parse( " 程序员 " );
IndexSearcher indexSearcher = new IndexSearcher( " c:\\indexDisk " );
Hits hits = indexSearcher.search(query);
System.out.println( " 找到了 " + hits.length() + " 结果 " );
for ( int i = 0 ;i
{
Document doc = hits.doc(i);
System.out.println(doc.get( " name " ));
}
}
这个例子是将内存中的索引合并到硬盘上来.
注意:合并的时候一定要将被合并的那一方的
IndexWriter 的 close()方法调用。
4.对索引的其它操作:
IndexReader 类是用来操作索引的,它有对
Document,Field 的删除等操作。
下面一部分的内容是:全文的搜索
全文的搜索主要是用:
IndexSearcher,Query,Hits,Document(都是 Query 的子
类),有的时候用 QueryParser
主要步骤:
1 . new QueryParser(Field 字段, new 分析器)
2 .Query query = QueryParser.parser(“要查询的字串”);这个地方我们可以用反
射 api 看一下 query 究竟是什么类型
3 . new IndexSearcher(索引目录).search(query);返回 Hits
4 .用 Hits.doc(n);可以遍历出 Document
5 .用 Document 可得到 Field 的具体信息了。
拿以前的例子来说吧
QueryParser queryParser = new QueryParser( " name " , new
StandardAnalyzer());
Query query = queryParser.parse( " 程序员 " );
/* 这里返回的就是 org.apache.lucene.search.PhraseQuery */
IndexSearcher indexSearcher = new IndexSearcher( " c:\\indexDisk " );
Hits hits = indexSearcher.search(query);
下面是搜索的例子:
[code]
public void SearchSort1() throws IOException,
ParseException
{
IndexSearcher indexSearcher = new
IndexSearcher("C:\\indexStore");
QueryParser queryParser = new
QueryParser("sort",new StandardAnalyzer());
Query query = queryParser.parse("4");
Hits hits = indexSearcher.search(query);
System.out.println("有"+hits.length()+"个结果");
Document doc = hits.doc(0);
System.out.println(doc.get("sort"));
}
public void SearchSort2() throws IOException,
ParseException
{
IndexSearcher indexSearcher = new
IndexSearcher("C:\\indexStore");
Query query = new RangeQuery(new
Term("sort","1"),new Term("sort","9"),true);//这个地方
前面没有提到,它是用于范围的 Query 可以看一下帮
助文档.
Hits hits = indexSearcher.search(query,new
Sort(new SortField("sort",new
MySortComparatorSource())));
System.out.println("有"+hits.length()+"个结果");
for(int i=0;i
{
Document doc = hits.doc(i);
System.out.println(doc.get("sort"));
}
}
public class MyScoreDocComparator implements
ScoreDocComparator
{
private Integer[]sort;
public MyScoreDocComparator(String s,IndexReader
reader, String fieldname) throws IOException
{
sort = new Integer[reader.maxDoc()];
for(int i = 0;i
{
Document doc =reader.document(i);
sort[i]=new Integer(doc.get("sort"));
}
}
public int compare(ScoreDoc i, ScoreDoc j)
{
if(sort[i.doc]>sort[j.doc])
return 1;
if(sort[i.doc]
return -1;
return 0;
}
public int sortType()
{
return SortField.INT;
}
public Comparable sortValue(ScoreDoc i)
{
// TODO 自动生成方法存根
return new Integer(sort[i.doc]);
}
}
public class MySortComparatorSource implements
SortComparatorSource
{
private static final long serialVersionUID = -
9189690812107968361L;
public ScoreDocComparator
newComparator(IndexReader reader, String fieldname)
throws IOException
{
if(fieldname.equals("sort"))
return new
MyScoreDocComparator("sort",reader,fieldname);
return null;
}
}[/code]
SearchSort1()输出的结果没有排序,SearchSort2()就排
序了。
2.多域搜索 MultiFieldQueryParser
如果想输入关键字而不想关心是在哪个 Field 里的就
可以用 MultiFieldQueryParser 了
用它的构造函数即可后面的和一个 Field 一样。
MultiFieldQueryParser. parse(String[] queries, String[]
fields, BooleanClause.Occur[] flags, Analyzer
analyzer)
~~~~~~~~~~~~~~~~~
第三个参数比较特殊这里也是与以前 lucene1.4.3 不一
样的地方
看一个例子就知道了
String[] fields = {"filename", "contents", "description"};
BooleanClause.Occur[] flags =
{BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST,//在这个 Field 里
必须出现的
BooleanClause.Occur.MUST_NOT};//在这个
Field 里不能出现
MultiFieldQueryParser.parse("query", fields, flags,
analyzer);
1、lucene 的索引不能太大,要不然效率会很低。大于
1G 的时候就必须考虑分布索引的问题
2、不建议用多线程来建索引,产生的互锁问题很麻
烦。经常发现索引被 lock,无法重新建立的情况
3、中文分词是个大问题,目前免费的分词效果都很
差。如果有能力还是自己实现一个分词模块,用最短
路径的切分方法,网上有教材和 demo 源码,可以参
考。
4、建增量索引的时候很耗 cpu,在访问量大的时候会
导致 cpu 的 idle 为 0
5、默认的评分机制不太合理,需要根据自己的业务
定制