读书人

Lucene 2.0 课程

发布时间: 2012-11-09 10:18:47 作者: rapoo

Lucene 2.0 教程

中华人民共和国???
Lucene 2.0 课程全国人民???
Lucene 2.0 课程2006年??

?
??????? /**/ /*? 这里放索引文件的位置? */??
?????? File indexDir? =?? new? File( "c:\\index" );??
?????? Analyzer luceneAnalyzer? =?? new? StandardAnalyzer();??
?????? IndexWriter indexWriter? =?? new? IndexWriter(indexDir, luceneAnalyzer,??
??????????????? true );??
?????? File[] textFiles? =? fileDir.listFiles();??
??????? long? startTime? =?? new? Date().getTime();??
?????????
??????? // 增加document到索引去???
????????? for? ( int? i? =?? 0 ; i? <? textFiles.length; i ++ )?? {??
??????????? if? (textFiles[i].isFile()??
??????????????????? &&? textFiles[i].getName().endsWith( ".txt" ))?? {??
?????????????? System.out.println( " File? "?? +? textFiles[i].getCanonicalPath()??
??????????????????????? +?? " 正在被索引. " );??
?????????????? String temp? =? FileReaderAll(textFiles[i].getCanonicalPath(),??
??????????????????????? "GBK" );??
?????????????? System.out.println(temp);??
?????????????? Document document? =?? new? Document();??
?????????????? Field FieldPath? =?? new? Field( "path " , textFiles[i].getPath(),??
?????????????????????? Field.Store.YES, Field.Index.NO);??
?????????????? Field FieldBody? =?? new? Field( "body" , temp, Field.Store.YES,??
?????????????????????? Field.Index.TOKENIZED,??
?????????????????????? Field.TermVector.WITH_POSITIONS_OFFSETS);??
?????????????? document.add(FieldPath);??
?????????????? document.add(FieldBody);??
?????????????? indexWriter.addDocument(document);??
?????????? }???
?????? }???
??????? // optimize()方法是对索引进行优化???
??????? indexWriter.optimize();??
?????? indexWriter.close();??
?????????
??????? // 测试一下索引的时间???
???????? long? endTime? =?? new? Date().getTime();??
?????? System.out??
?????????????? .println( " 这花费了 "??
??????????????????????? +? (endTime? -? startTime)??
??????????????????????? +?? "? 毫秒来把文档增加到索引里面去! "??
??????????????????????? +? fileDir.getPath());??
?? }???
?
??? public?? static? String FileReaderAll(String FileName, String charset)??
??????????? throws? IOException?? {??
?????? BufferedReader reader? =?? new? BufferedReader( new? InputStreamReader(??
??????????????? new? FileInputStream(FileName), charset));??
?????? String line? =?? new? String();??
?????? String temp? =?? new? String();??
?????????
??????? while? ((line? =? reader.readLine())? !=?? null )?? {??
?????????? temp? +=? line;??
?????? }???
?????? reader.close();??
??????? return? temp;??
?? }???
}??

File?C:\s\ 1 .txt正在被索引Lucene 2.0 课程.???
Lucene 2.0 课程中华人民共和国全国人民2006年???
Lucene 2.0 课程File?C:\s\ 2 .txt正在被索引Lucene 2.0 课程.???
Lucene 2.0 课程中华人民共和国全国人民2006年???
Lucene 2.0 课程File?C:\s\ 3 .txt正在被索引Lucene 2.0 课程.???
Lucene 2.0 课程中华人民共和国全国人民2006年???
Lucene 2.0 课程这花费了297?毫秒来把文档增加到索引里面去 ! c:\s??

?????? 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 ?个结果 !

package ?lighter.javaeye.com;???
Lucene 2.0 课程??
Lucene 2.0 课程 import ?java.io.IOException;???
Lucene 2.0 课程 import ?java.io.StringReader;???
Lucene 2.0 课程??
Lucene 2.0 课程 import ?org.apache.lucene.analysis.Analyzer;???
Lucene 2.0 课程 import ?org.apache.lucene.analysis.Token;???
Lucene 2.0 课程 import ?org.apache.lucene.analysis.TokenStream;???
Lucene 2.0 课程 import ?org.apache.lucene.analysis.standard.StandardAnalyzer;???
Lucene 2.0 课程??
Lucene 2.0 课程 public ? class ?StandardAnalyzerTest????
Lucene 2.0 课程Lucene 2.0 课程 Lucene 2.0 课程{???
Lucene 2.0 课程???? // 构造函数,???
Lucene 2.0 课程???? public ?StandardAnalyzerTest()???
Lucene 2.0 课程Lucene 2.0 课程???? Lucene 2.0 课程{???
Lucene 2.0 课程????} ???
Lucene 2.0 课程???? public ? static ? void ?main(String[]?args)????
Lucene 2.0 课程Lucene 2.0 课程???? Lucene 2.0 课程{???
Lucene 2.0 课程???????? // 生成一个StandardAnalyzer对象???
Lucene 2.0 课程????????Analyzer?aAnalyzer? = ? new ?StandardAnalyzer();???
Lucene 2.0 课程???????? // 测试字符串???
Lucene 2.0 课程????????StringReader?sr? = ? new ?StringReader( " lighter?javaeye?com?is?the?are?on " );???
Lucene 2.0 课程???????? // 生成TokenStream对象???
Lucene 2.0 课程????????TokenStream?ts? = ?aAnalyzer.tokenStream( " name " ,?sr);????
Lucene 2.0 课程Lucene 2.0 课程???????? try ? Lucene 2.0 课程{???
Lucene 2.0 课程???????????? int ?i = 0 ;???
Lucene 2.0 课程????????????Token?t? = ?ts.next();???
Lucene 2.0 课程???????????? while (t != null )???
Lucene 2.0 课程Lucene 2.0 课程???????????? Lucene 2.0 课程{???
Lucene 2.0 课程???????????????? // 辅助输出时显示行号???
Lucene 2.0 课程????????????????i ++ ;???
Lucene 2.0 课程???????????????? // 输出处理后的字符???
Lucene 2.0 课程????????????????System.out.println( " 第 " + i + " 行: " + t.termText());???
Lucene 2.0 课程???????????????? // 取得下一个字符???
Lucene 2.0 课程????????????????t = ts.next();???
Lucene 2.0 课程????????????} ???
Lucene 2.0 课程Lucene 2.0 课程????????} ? catch ?(IOException?e)? Lucene 2.0 课程{???
Lucene 2.0 课程????????????e.printStackTrace();???
Lucene 2.0 课程????????} ???
Lucene 2.0 课程????} ???
Lucene 2.0 课程} ???
Lucene 2.0 课程

第1行:lighter?
Lucene 2.0 课程第2行:javaeye?
Lucene 2.0 课程第3行:com

package ?lighter.javaeye.com;???
Lucene 2.0 课程 import ?org.apache.lucene.analysis.standard.StandardAnalyzer;???
Lucene 2.0 课程 import ?org.apache.lucene.document.Document;???
Lucene 2.0 课程 import ?org.apache.lucene.document.Field;???
Lucene 2.0 课程 import ?org.apache.lucene.index.IndexWriter;???
Lucene 2.0 课程 import ?org.apache.lucene.queryParser.QueryParser;???
Lucene 2.0 课程 import ?org.apache.lucene.search.Hits;???
Lucene 2.0 课程 import ?org.apache.lucene.search.IndexSearcher;???
Lucene 2.0 课程 import ?org.apache.lucene.search.Query;???
Lucene 2.0 课程 import ?org.apache.lucene.store.FSDirectory;???
Lucene 2.0 课程??
Lucene 2.0 课程Lucene 2.0 课程 public ? class ?FSDirectoryTest? Lucene 2.0 课程{???
Lucene 2.0 课程??
Lucene 2.0 课程???? // 建立索引的路径???
Lucene 2.0 课程???? public ? static ? final ?String?path? = ? " c:\\index2 " ;???
Lucene 2.0 课程??
Lucene 2.0 课程Lucene 2.0 课程???? public ? static ? void ?main(String[]?args)? throws ?Exception? Lucene 2.0 课程{???
Lucene 2.0 课程????????Document?doc1? = ? new ?Document();???
Lucene 2.0 课程????????doc1.add(? new ?Field( " name " ,? " lighter?javaeye?com " ,Field.Store.YES,Field.Index.TOKENIZED));???
Lucene 2.0 课程??
Lucene 2.0 课程????????Document?doc2? = ? new ?Document();???
Lucene 2.0 课程????????doc2.add( new ?Field( " name " ,? " lighter?blog " ,Field.Store.YES,Field.Index.TOKENIZED));???
Lucene 2.0 课程??
Lucene 2.0 课程????????IndexWriter?writer? = ? new ?IndexWriter(FSDirectory.getDirectory(path,? true ),? new ?StandardAnalyzer(),? true );???
Lucene 2.0 课程????????writer.setMaxFieldLength( 3 );???
Lucene 2.0 课程????????writer.addDocument(doc1);???
Lucene 2.0 课程????????writer.setMaxFieldLength( 3 );???
Lucene 2.0 课程????????writer.addDocument(doc2);???
Lucene 2.0 课程????????writer.close();???
Lucene 2.0 课程??
Lucene 2.0 课程????????IndexSearcher?searcher? = ? new ?IndexSearcher(path);???
Lucene 2.0 课程????????Hits?hits? = ? null ;???
Lucene 2.0 课程????????Query?query? = ? null ;???
Lucene 2.0 课程????????QueryParser?qp? = ? new ?QueryParser( " name " , new ?StandardAnalyzer());???
Lucene 2.0 课程???????????
Lucene 2.0 课程????????query? = ?qp.parse( " lighter " );???
Lucene 2.0 课程????????hits? = ?searcher.search(query);???
Lucene 2.0 课程????????System.out.println( " 查找\ " lighter\ " ?共 " ? + ?hits.length()? + ? " 个结果 " );???
Lucene 2.0 课程??
Lucene 2.0 课程????????query? = ?qp.parse( " javaeye " );???
Lucene 2.0 课程????????hits? = ?searcher.search(query);???
Lucene 2.0 课程????????System.out.println( " 查找\ " javaeye\ " ?共 " ? + ?hits.length()? + ? " 个结果 " );???
Lucene 2.0 课程??
Lucene 2.0 课程????} ???
Lucene 2.0 课程??
Lucene 2.0 课程} ??
Lucene 2.0 课程

查找 " lighter " ?共2个结果???
Lucene 2.0 课程查找 " javaeye " ?共1个结果?

FSDirectory.getDirectory(File?file,? boolean ?create)
Lucene 2.0 课程FSDirectory.getDirectory(String?path,? boolean ?create)

IndexWriter(Directory?d,?Analyzer?a,? boolean ?create)

IndexWrtier?indexWriter? = ? new ?IndexWriter(FSDirectory.getDirectory(“c:\\index”, true ), new ?StandardAnlyazer(), true );
Lucene 2.0 课程IndexWrtier?indexWriter? = ? new ?IndexWriter( new ?RAMDirectory(), new ?StandardAnlyazer(), true );

IndexWriter.addIndexes(Directory[]?dirs)

public ? void ?UniteIndex()? throws ?IOException
Lucene 2.0 课程Lucene 2.0 课程???? Lucene 2.0 课程{
Lucene 2.0 课程????????IndexWriter?writerDisk? = ? new ?IndexWriter(FSDirectory.getDirectory( " c:\\indexDisk " ,? true ), new ?StandardAnalyzer(), true );
Lucene 2.0 课程????????Document?docDisk? = ? new ?Document();
Lucene 2.0 课程????????docDisk.add( new ?Field( " name " , " 程序员之家 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writerDisk.addDocument(docDisk);
Lucene 2.0 课程????????RAMDirectory?ramDir? = ? new ?RAMDirectory();
Lucene 2.0 课程????????IndexWriter?writerRam? = ? new ?IndexWriter(ramDir, new ?StandardAnalyzer(), true );
Lucene 2.0 课程????????Document?docRam? = ? new ?Document();
Lucene 2.0 课程????????docRam.add( new ?Field( " name " , " 程序员杂志 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writerRam.addDocument(docRam);
Lucene 2.0 课程????????writerRam.close(); // 这个方法非常重要,是必须调用的
Lucene 2.0 课程Lucene 2.0 课程????????writerDisk.addIndexes( new ?Directory[] Lucene 2.0 课程{ramDir} );
Lucene 2.0 课程????????writerDisk.close();
Lucene 2.0 课程????}
Lucene 2.0 课程???? public ? void ?UniteSearch()? throws ?ParseException,?IOException
Lucene 2.0 课程Lucene 2.0 课程???? Lucene 2.0 课程{
Lucene 2.0 课程????????QueryParser?queryParser? = ? new ?QueryParser( " name " , new ?StandardAnalyzer());
Lucene 2.0 课程????????Query?query? = ?queryParser.parse( " 程序员 " );
Lucene 2.0 课程????????IndexSearcher?indexSearcher? = new ?IndexSearcher( " c:\\indexDisk " );
Lucene 2.0 课程????????Hits?hits? = ?indexSearcher.search(query);
Lucene 2.0 课程????????System.out.println( " 找到了 " + hits.length() + " 结果 " );
Lucene 2.0 课程???????? for ( int ?i = 0 ;i
Lucene 2.0 课程Lucene 2.0 课程???????? Lucene 2.0 课程{
Lucene 2.0 课程????????????Document?doc? = ?hits.doc(i);
Lucene 2.0 课程????????????System.out.println(doc.get( " name " ));
Lucene 2.0 课程????????}
Lucene 2.0 课程}

1 . new ?QueryParser(Field字段, new ?分析器)
Lucene 2.0 课程 2 .Query?query? = ?QueryParser.parser(“要查询的字串”);这个地方我们可以用反射api看一下query究竟是什么类型
Lucene 2.0 课程 3 . new ?IndexSearcher(索引目录).search(query);返回Hits
Lucene 2.0 课程 4 .用Hits.doc(n);可以遍历出Document
Lucene 2.0 课程 5 .用Document可得到Field的具体信息了。

QueryParser?queryParser? = ? new ?QueryParser( " name " , new ?StandardAnalyzer());
Lucene 2.0 课程????????Query?query? = ?queryParser.parse( " 程序员 " );
Lucene 2.0 课程Lucene 2.0 课程 /**/ /* 这里返回的就是org.apache.lucene.search.PhraseQuery */
Lucene 2.0 课程????????IndexSearcher?indexSearcher? = new ?IndexSearcher( " c:\\indexDisk " );
Lucene 2.0 课程????????Hits?hits? = ?indexSearcher.search(query);

public ? void ?IndexSort()? throws ?IOException
Lucene 2.0 课程Lucene 2.0 课程 Lucene 2.0 课程{
Lucene 2.0 课程????????IndexWriter?writer? = ? new ?IndexWriter( " C:\\indexStore " , new ?StandardAnalyzer(), true );
Lucene 2.0 课程????????Document?doc? = ? new ?Document()
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 1 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????doc? = ? new ?Document();
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 4 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????doc? = ? new ?Document();
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 3 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????doc? = ? new ?Document();
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 5 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????doc? = ? new ?Document();
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 9 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????doc? = ? new ?Document();
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 6 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????doc? = ? new ?Document();
Lucene 2.0 课程????????doc.add( new ?Field( " sort " , " 7 " ,Field.Store.YES,Field.Index.TOKENIZED));
Lucene 2.0 课程????????writer.addDocument(doc);
Lucene 2.0 课程????????writer.close();
Lucene 2.0 课程}
Lucene 2.0 课程


下面是搜索的例子:
[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、默认的评分机制不太合理,需要根据自己的业务定制

?

整体来说lucene要用好不容易,必须在上述方面扩充他的功能,才能作为一个商用的搜索引擎

读书人网 >编程

热点推荐