java lucene入门例子

来源:互联网 发布:铁路工程造价软件下载 编辑:程序博客网 时间:2024/05/22 07:04

 

import org.apache.lucene.search.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.index.*;
import org.apache.lucene.document.*;
public class SearchTest 
{
    
//声明一个IndexSearcher对象
    private IndexSearcher searcher = null;
    
//声明一个Query对象
    private Query query = null;
    
//构造函数
    public SearchTest()
    
{
        
try 
        
{
            searcher 
= new IndexSearcher(IndexReader.open(IndexTest.index_path));
        }

        
catch (Exception e)
        
{
            e.printStackTrace();
        }

    }

    
//查找索引,返回为Hits对象
    public Hits search(String keyword)
    
{
        
try
        
{
            
//将关键字包装成Query对象
             QueryParser parser = new QueryParser("Content",  new StandardAnalyzer());
            query 
=parser.parse(keyword);
            
return searcher.search(query);
        }

        
catch(Exception e)
        
{
            e.printStackTrace();
            
return null;
        }

    }

    
    
public static void main(String[] args)
    
{
        SearchTest test 
= new SearchTest();
        
//检索关键字
        Hits h = test.search("Robin");
        
for(int i=0;i<h.length();i++)
        
{
            
try
            
{
                Document doc 
= h.doc(i);
                System.out.print(
"This is No."+i+" result indexed:");
                System.out.println(doc.get(
"Content"));
            }

            
catch(Exception e)
            
{
                e.printStackTrace();
            }

        }

    }

}

lucene是java社区中最著名的全文检索工具包,在进行web开发时,常用到检索信息功能,例如论坛中检索文章,回复。此时,光靠数据库的检索功能可能已经无法满足要求,如能用外部api实现站内检索将会非常有吸引力。本文作为一个基本入门文章,仅介绍lucene的基本使用方法。

 这里介绍的主要为在使用中经常碰到一些概念,以大家都比较熟悉的数据库来进行类比的讲解,使用Lucene进行全文检索的过程有点类似数据库的这个过程,table---à查询相应的字段或查询条件----à返回相应的记录,首先是IndexWriter,通过它建立相应的索引表,相当于数据库中的table,在构建此索引表时需指定的为该索引表采用何种方式进行构建,也就是说对于其中的记录的字段以什么方式来进行格式的划分,这个在Lucene中称为Analyzer,Lucene提供了几种环境下使用的Analyzer:SimpleAnalyzer、StandardAnalyzer、GermanAnalyzer等,其中StandardAnalyzer是经常使用的,因为它提供了对于中文的支持,在表建好后我们就需要往里面插入用于索引的记录,在Lucene中这个称为Document,有点类似数据库中table的一行记录,记录中的字段的添加方法,在Lucene中称为Field,这个和数据库中基本一样,对于Field Lucene分为可被索引的,可切分的,不可被切分的,不可被索引的几种组合类型,通过这几个元素基本上就可以建立起索引了。在查询时经常碰到的为另外几个概念,首先是Query,Lucene提供了几种经常可以用到的Query:TermQuery、MultiTermQuery、BooleanQuery、cardQuery、PhraseQuery、PrefixQuery、PhrasePrefixQuery、FuzzyQuery、RangeQuery、SpanQuery,Query其实也就是指对于需要查询的字段采用什么样的方式进行查询,如模糊查询、语义查询、短语查询、范围查询、组合查询等,还有就是QueryParser,QueryParser可用于创建不同的Query,还有一个MultiFieldQueryParser支持对于多个字段进行同一关键字的查询,IndexSearcher概念指的为需要对何目录下的索引文件进行何种方式的分析的查询,有点象对数据库的哪种索引表进行查询并按一定方式进行记录中字段的分解查询的概念,通过IndexSearcher以及Query即可查询出需要的结果,Lucene返回的为Hits.通过遍历Hits可获取返回的结果的Document,通过Document则可获取Field中的相关信息了。

 

首先从www.apche.org 下载最新版本lucene,我用的是2.3.1,将jar包导入build path

1.建立索引:

 

import org.apache.lucene.index.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.document.Field;
public class IndexTest 
{
 
//索引存储目录
 public final static String index_path="d:/index";
 
//被索引的内容
 public final static String index_content1 = "Robin China";
 
public final static String index_content2 = "US Robin";
 
 
//索引器
 private IndexWriter writer = null;
 
 
//构造函数
 public IndexTest()
 
{
  
try
  
{
   writer 
= new IndexWriter(index_path,new StandardAnalyzer(),true);
  }

  
catch(Exception e)
  
{
   e.printStackTrace();
  }

 }

 
 
//建立document对象,并添加域"content"
 public Document getDocument(String content)
 
{
  Document doc 
= new Document();
  doc.add(
new Field("Content",content,Field.Store.YES,Field.Index.TOKENIZED));
  
return doc;
 }

 
 
//写入索引
 public void writerToIndex()
 
{
  
try
  
{
   writer.addDocument(getDocument(
this.index_content1));
   writer.addDocument(getDocument(
this.index_content2));
   writer.close();
  }

  
catch(Exception e)
  
{
   e.printStackTrace();
  }

 }

 
 
public static void main(String[] args)
 
{
  IndexTest test 
= new IndexTest();
  test.writerToIndex();
 }

}

2.查找索引

 

原创粉丝点击