初识Apache lucene

本文介绍了如何使用Lucene进行文档索引和搜索的基本流程。主要内容包括创建文档、建立索引、查询解析及搜索结果处理等关键步骤,并通过示例代码演示了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In Lucene 2.3.2 of core API documention,you should be see following description:

 

   To use Lucene, an application should:
   1. Create Documents by adding Fields;
   2. Create an IndexWriter and add documents to it with addDocument();
   3. Call QueryParser.parse() to build a query from a string; and
   4. Create an IndexSearcher and pass the query to its search() method.

Analyzer analyzer = new StandardAnalyzer();

    // Store the index in memory:
    Directory directory = new RAMDirectory();
    // To store an index on disk, use this instead:
    //Directory directory = FSDirectory.getDirectory("/tmp/testindex");
    IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
    iwriter.setMaxFieldLength(25000);
    Document doc = new Document();
    String text = "This is the text to be indexed.";
    doc.add(new Field("fieldname", text, Field.Store.YES,
        Field.Index.TOKENIZED));
    iwriter.addDocument(doc);
    iwriter.optimize();
    iwriter.close();
    
    // Now search the index:
    IndexSearcher isearcher = new IndexSearcher(directory);
    // Parse a simple query that searches for "text":
    QueryParser parser = new QueryParser("fieldname", analyzer);
    Query query = parser.parse("text");
    Hits hits = isearcher.search(query);
    assertEquals(1, hits.length());
    // Iterate through the results:
    for (int i = 0; i < hits.length(); i++) {
      Document hitDoc = hits.doc(i);
      assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
    }
    isearcher.close();
    directory.close();
 

 

 

 

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类的实例来表示的。

 

一个实例,简单地建立索引,进行搜索

 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() + "个结果");  
     }
}  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值