Lucene源码解析--lucene原理及索引和检索的过程

Lucene是一个高效的,基于Java的全文检索库。全文检索的基本思路,也即将非结构化数据中的一部分信息提取出来,重新组织,使其变得有一定结构,然后对此有一定结构的数据进行搜索,从而达到搜索相对较快的目的。这部分从非结构化数据中提取出的然后重新组织的信息,我们称之索引。索引:已知文档中查找包含哪些字符串;反向索引:已知字符串查找在哪些文档中包含。
bb
一:索引过程
public class IndexTest {
public static void main(String[] args)
{
try {
    File fileDir =new File("F:\\document");
    IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_43, new StandardAnalyzer(Version.LUCENE_43));
    config.setOpenMode(OpenMode.CREATE);
    IndexWriter writer=new IndexWriter(FSDirectory.open(new File("F:\\index")),config);
    for(File file:fileDir.listFiles())
    {
        Document document=new Document();
        document.add(new TextField("content", new FileReader(file)));
        document.add(new StringField("title", file.getName(), Store.YES));
        writer.addDocument(document);
    }
    writer.close();
} catch (Exception e) {
    e.printStackTrace();
}   
}
}
1) 有一系列被索引文件
2) 被索引文件经过语法分析和语言处理形成一系列词(Term)。
3) 经过索引创建形成词典和反向索引表。
4) 通过索引存储将索引写入硬盘。


二:搜索过程
public class SearchTest {
public static void main(String[] args)
{
 try {
     IndexReader reader=DirectoryReader.open(FSDirectory.open(new File("F:\\index")));
     IndexSearcher searcher=new IndexSearcher(reader);
     Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43);
     
      QueryParser queryParser=new QueryParser(Version.LUCENE_43, "content", analyzer);
     
      Query query=queryParser.parse("lucene");
      TopDocs topDocs=searcher.search(query, 10);
     
      ScoreDoc[] hits=topDocs.scoreDocs;
     
      for(int i=0;i      {
          System.out.println("score:"+hits[i].score);
          System.out.println("title:"+searcher.doc((hits[i].doc)).get("title"));
      }
   
} catch (Exception e) {
    e.printStackTrace();
}   
}
}

1) 用户输入查询语句。
2) 对查询语句经过语法分析和语言分析得到一系列词(Term)。
3) 通过语法分析得到一个查询树。
4) 通过索引存储将索引读入到内存。
5) 利用查询树搜索索引,从而得到每个词(Term)的文档链表,对文档链表进行交,差,并得
到结果文档。
6) 将搜索到的结果文档对查询的相关性进行排序。
7) 返回查询结果给用户。


fj.pngii.jpg

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28624388/viewspace-765974/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28624388/viewspace-765974/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值