Lucene技术杂谈

Lucene教程html

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就是该文本文件。该构造函数其实是读去文件内容,并对其进行索引,但不存储。java

 
 

Lucene 2 教程 程序员

Lucene是apache组织的一个用java实现全文搜索引擎的开源项目。 其功能很是的强大,api也很简单。总得来讲用Lucene来进行创建 和搜索和操做数据库是差很少的(有点像),Document能够看做是 数据库的一行记录,Field能够看做是数据库的字段。用lucene实 现搜索引擎就像用JDBC实现链接数据库同样简单。数据库

Lucene2.0,它与之前普遍应用和介绍的Lucene 1.4.3并不兼容。 Lucene2.0的下载地址是http://apache.justdn.org/lucene/java/apache

例子一 :
一、在windows系统下的的C盘,建一个名叫s的文件夹,在该文件夹里面随便建三个txt文件,随便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
其中1.txt的内容以下: windows

中华人民共和国   全国人民   2006年   api

而"2.txt"和"3.txt"的内容也能够随便写几写,这里懒写,就复制一个和1.txt文件的内容同样吧
二、下载lucene包,放在classpath路径中
创建索引: 多线程

package lighter.javaeye.com;     import java.io.BufferedReader;   import java.io.File;   import java.io.FileInputStream;   import java.io.IOException;   import java.io.InputStreamReader;   import java.util.Date;     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.index.IndexWriter;     /** */ /**   * author lighter date 2006-8-7    */   public   class TextFileIndexer  {       public   static   void  main(String[] args)  throws Exception  {           /**/ /*  指明要索引文件夹的位置,这里是C盘的S文件夹下 */           File fileDir =   new  File( " c:\\s " );             /**/ /*  这里放索引文件的位置  */           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正在被索引.   中华人民共和国全国人民2006年   File C:\s\ 2 .txt正在被索引.   中华人民共和国全国人民2006年   File C:\s\ 3 .txt正在被索引.   中华人民共和国全国人民2006年   这花费了297 毫秒来把文档增长到索引里面去 ! c:\s  

三、创建了索引以后,查询啦....

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:字段。

IndexSearcher:是lucene中最基本的检索工具,全部的检索都会用到IndexSearcher工具;

Query:查询,lucene中支持模糊查询,语义查询,短语查询,组合查询等等,若有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些类。

QueryParser: 是一个解析用户输入的工具,能够经过扫描用户输入的字符串,生成Query对象。

Hits:在搜索完成以后,须要把搜索结果返回并显示给用户,只有这样才算是完成搜索的目的。在lucene中,搜索的结果的集合是用Hits类的实例来表示的。

上面做了一大堆名词解释,下面就看几个简单的实例吧: 一、简单的的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中内置的"标准分析器",能够作以下功能: 一、对原有句子按照空格进行了分词
二、全部的大写字母均可以能转换为小写的字母 三、能够去掉一些没有用处的单词,例如"is","the","are"等单词,也删除了全部的标点
查看一下结果与"newStringReader("lighter javaeye com is the are on")"做一个比较就清楚明了。
这里不对其API进行解释了,具体见lucene的官方文档。须要注意一点,这里的代码使用的是lucene2的API,与1.43版有一些明显的差异。

二、看另外一个实例,简单地创建索引,进行搜索

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

到如今咱们已经能够用lucene创建索引了 下面介绍一下几个功能来完善一下: 1.索引格式
其实索引目录有两种格式,
一种是除配置文件外,每个Document独立成为一个文件(这种搜索起来会影响速度)。
另外一种是所有的Document成一个文件,这样属于复合模式就快了。
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的具体信息了。

其实1 ,2两步就是为了弄出个Query 实例,到底是什么类型的看分析器了。
拿之前的例子来讲吧

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);

无论是什么类型,无非返回的就是Query的子类,咱们彻底能够不用这两步直接new个Query的子类的实例就ok了,不过通常仍是用这两步由于它返回的是PhraseQuery这个是很是强大的query子类它能够进行多字搜索用QueryParser能够设置各个关键字之间的关系这个是最经常使用的了。 IndexSearcher: 其实IndexSearcher它内部自带了一个IndexReader用来读取索引的,IndexSearcher有个close()方法,这个方法不是用来关闭IndexSearche的是用来关闭自带的IndexReader。
QueryParser呢能够用parser.setOperator()来设置各个关键字之间的关系(与仍是或)它能够自动经过空格从字串里面将关键字分离出来。 注意:用QueryParser搜索的时候分析器必定的和创建索引时候用的分析器是同样的。 Query: 能够看一个lucene2.0的帮助文档有不少的子类: BooleanQuery, ConstantScoreQuery, ConstantScoreRangeQuery, DisjunctionMaxQuery,FilteredQuery, MatchAllDocsQuery, MultiPhraseQuery, MultiTermQuery,PhraseQuery, PrefixQuery, RangeQuery, SpanQuery, TermQuery 各自有用法看一下文档就能知道它们的用法了 下面一部分讲一下lucene的分析器: 分析器是由分词器和过滤器组成的,拿英文来讲吧分词器就是经过空格把单词分开,过滤器就是把the,to,of等词去掉不被搜索和索引。 咱们最经常使用的是StandardAnalyzer()它是lucene的标准分析器它集成了内部的许多的分析器。 最后一部分了:lucene的高级搜索了 1.排序 Lucene有内置的排序用IndexSearcher.search(query,sort)可是功能并不理想。咱们须要本身实现自定义的排序。 这样的话得实现两个接口: ScoreDocComparator,SortComparatorSource 用IndexSearcher.search(query,newSort(new SortField(String Field,SortComparatorSource))); 就看个例子吧: 这是一个创建索引的例子:

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

下面是搜索的例子: [code] public void SearchSort1() throws IOException, ParseException {         IndexSearcher indexSearcher = newIndexSearcher("C:\\indexStore");         QueryParser queryParser = newQueryParser("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 = newIndexSearcher("C:\\indexStore");         Query query = new RangeQuery(newTerm("sort","1"),newTerm("sort","9"),true);//这个地方前面没有提到,它是用于范围的Query能够看一下帮助文档.         Hits hits =indexSearcher.search(query,new Sort(new SortField("sort",newMySortComparatorSource())));         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]=newInteger(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)             throwsIOException     {        if(fieldname.equals("sort"))             return newMyScoreDocComparator("sort",reader,fieldname);         return null;     } }[/code] SearchSort1()输出的结果没有排序,SearchSort2()就排序了。 2.多域搜索MultiFieldQueryParser 若是想输入关键字而不想关心是在哪一个Field里的就能够用MultiFieldQueryParser了 用它的构造函数便可后面的和一个Field同样。 MultiFieldQueryParser. parse(String[] queries, String[] fields,BooleanClause.Occur[] flags, Analyzeranalyzer)                                         ~~~~~~~~~~~~~~~~~ 第三个参数比较特殊这里也是与之前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);
一、lucene的索引不能太大,要否则效率会很低。大于1G的时候就必须考虑分布索引的问题

二、不建议用多线程来建索引,产生的互锁问题很麻烦。常常发现索引被lock,没法从新创建的状况

三、中文分词是个大问题,目前免费的分词效果都不好。若是有能力仍是本身实现一个分词模块,用最短路径的切分方法,网上有教材和demo源码,能够参考。

四、建增量索引的时候很耗cpu,在访问量大的时候会致使cpu的idle为0

五、默认的评分机制不太合理,须要根据本身的业务定制

 

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