Lucene简单使用

     最近看下Lucene的东西,把它写下来可以看下。Lucene结构和工作原理我就不说了,网上好多。

我的环境是Lucene2.0
写一个简单使用Lucene的示例。此类首创建索引,然后显示索引文档的情况,最后搜索(只在content找,和在title或content里找)。

package  net.blogjava.chenlb.lucene;

import  org.apache.lucene.analysis.standard.StandardAnalyzer;
import  org.apache.lucene.document.Document;
import  org.apache.lucene.document.Field;
import  org.apache.lucene.index.IndexReader;
import  org.apache.lucene.index.IndexWriter;
import  org.apache.lucene.queryParser.MultiFieldQueryParser;
import  org.apache.lucene.queryParser.QueryParser;
import  org.apache.lucene.search.BooleanClause;
import  org.apache.lucene.search.Hits;
import  org.apache.lucene.search.IndexSearcher;
import  org.apache.lucene.search.Query;

/**
 * Lucene简单使用
 * 
@author  chenlb 2008-3-8 下午11:42:55
 
*/
public   class  LuceneUse {

    
public   static   void  main(String[] args)  throws  Exception {
        LuceneUse liu 
=   new  LuceneUse();
        
// 索引
        IndexWriter iw  =   new  IndexWriter( " index " new  StandardAnalyzer(),  true );
        
// 添加要索引的Lucene文档
        Document doc  =  liu.createDoc( " Lucene创建索引示例 " " chenlb " " 2008-03-08 " " Lucene索引的内容在这里,这些内容不被存储. " );
        iw.addDocument(doc);
        
        doc 
=  liu.createDoc( " 文档2 " " bin " " 2007-10-03 " " 这是索引的另一个文档 " );
        iw.addDocument(doc);
        
        doc 
=  liu.createDoc( " 学习内容 " " chenlb " " 2008-3-3 " " 要努力奋斗,祝网友们天天快乐 " );
        iw.addDocument(doc);
        
        iw.optimize();    
// 优化
        iw.close();
        
        
//
        System.out.println( " ===========索引文档内容============= " );
        IndexReader reader 
=  IndexReader.open( " index " );
        
for ( int  i = 0 ; i < reader.numDocs(); i ++ ) {
            Document d 
=  reader.document(i);
            liu.printDoc(d);
        }
        
        System.out.println(
" ===========以下是单域查找'天天'结果============ " );
        
// 单域搜索
        IndexSearcher searcher  =   new  IndexSearcher( " index " );
        QueryParser parser 
=   new  QueryParser( " content " new  StandardAnalyzer());
        Query q 
=  parser.parse( " 天天 " ); 
        
        
long  start  =  System.currentTimeMillis();
        Hits hits 
=  searcher.search(q);
        
long  end  =  System.currentTimeMillis();
        
for ( int  i = 0 ; i < hits.length(); i ++ ) {
            liu.printDoc(hits.doc(i));
        }
        System.out.println(
" 共找到:  " + hits.length() + "  个文档,花了: " + (end - start) + " ms " );
        
        
// 多域搜索
        System.out.println( " ===========以下多域是查找'内容'结果============ " );
        
// 从title或content找
        q  =  MultiFieldQueryParser.parse( " 内容 " new  String[] { " title " " content " },  new  BooleanClause.Occur[] {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD},  new  StandardAnalyzer());
        start 
=  System.currentTimeMillis();
        hits 
=  searcher.search(q);
        end 
=  System.currentTimeMillis();
        
for ( int  i = 0 ; i < hits.length(); i ++ ) {
            liu.printDoc(hits.doc(i));
        }
        System.out.println(
" 共找到:  " + hits.length() + "  个文档,花了: " + (end - start) + " ms " );
    }
    
    
/**
     * 显示文档内容
     
*/
    
private   void  printDoc(Document d) {
        System.out.println(
" 标题:  " + d.get( " title " ) + " , 作者:  " + d.get( " author " ) + " , 日期:  " + d.get( " date " ) + " , 内容:  " + d.get( " content " ));
    }
    
    
/**
     * 创建一个Lucene文档
     
*/
    
private  Document createDoc(String title, String author, String date, String content) {
        Document doc 
=   new  Document();
        doc.add(
new  Field( " title " , title, Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(
new  Field( " author " , author, Field.Store.YES, Field.Index.NO));
        doc.add(
new  Field( " date " , date, Field.Store.YES, Field.Index.NO));
        doc.add(
new  Field( " content " , content, Field.Store.YES, Field.Index.TOKENIZED));
        
return  doc;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lucene.net 是一个开源的全文检索引擎库,它提供了一些基本的 API 来创建和维护索引,并且可以通过这些 API 来搜索索引中的文档。下面是一些使用 Lucene.net 的基本步骤: 1. 创建索引:使用 Lucene.net 的 API,可以创建一个空的索引。可以将文档添加到索引中,以便后续搜索。 2. 添加文档:使用 Lucene.net 的 API,可以将文档添加到索引中。可以为每个文档定义一个或多个字段。 3. 搜索索引:使用 Lucene.net 的 API,可以搜索索引中的文档。可以使用查询对象来指定搜索条件,例如搜索某个字段中包含特定关键字的文档。 4. 处理搜索结果:搜索结果是一组匹配查询条件的文档。可以使用 Lucene.net 的 API 来访问每个文档的字段,以便将搜索结果呈现给用户。 以下是一个简单的示例代码,可用于创建索引、添加文档和搜索索引: ``` // 创建索引 var indexDirectory = FSDirectory.Open(@"C:\myindex"); var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); var indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer); var writer = new IndexWriter(indexDirectory, indexConfig); // 添加文档 var doc = new Document(); doc.Add(new TextField("title", "Lucene.net tutorial", Field.Store.YES)); doc.Add(new TextField("content", "This is a tutorial on how to use Lucene.net for full text search.", Field.Store.YES)); writer.AddDocument(doc); // 搜索索引 var searcher = new IndexSearcher(writer.GetReader(true)); var queryParser = new QueryParser(LuceneVersion.LUCENE_48, "content", analyzer); var query = queryParser.Parse("full text search"); var topDocs = searcher.Search(query, 10); foreach (var scoreDoc in topDocs.ScoreDocs) { var doc = searcher.Doc(scoreDoc.Doc); Console.WriteLine(doc.Get("title")); } ``` 此示例创建一个名为“myindex”的索引目录,添加一个文档,然后搜索包含“full text search”关键字的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值