【搜索那些事】细谈lucene(三)lucene核心API简介


经过前面的简单理论介绍,相信大家对搜索引擎lucene有个简单的了解。前面我们也提到过在lucene中主要包括索引和搜索这两大方面的组件。今天我们我们就通过一个简单的实例来看一下lucene给我们提供的有关这两个组件的简单用法。


一:创建索引

在用lucene搜索之前,我们首先要做的就是是创建索引,只有有索引了,我们才有了搜索的对象,下面我们就根据一个创建索引的小demo来一步步分析一下创建索引的步骤:

public class Indexer {
    private IndexWriter writer = null;
    public Indexer(String indexDir) throws Exception {
       Directory dir = FSDirectory.open(new File(indexDir));//打开保存索引目录
       writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
              true, IndexWriter.MaxFieldLength.UNLIMITED);//创建lucene IndexWriter,创建索引工具
    }
   
    public void close() throws Exception
    {
       writer.close();
    }
 
    public int index(String dataDir, FileFilter filter) throws Exception {
 
       File[] files = new File(dataDir).listFiles();
       for (File file : files) {//遍历文件目录下所有txt文件,把文件加入索引
           if(!file.isDirectory() && !file.isHidden() && file.exists()
                  && (filter == null || filter.accept(file))) {
              indexFile(file);
           }
       }
       return writer.numDocs();
    }
 
    private Document getDocument(File f) {
       Document doc = new Document();
       try {
           doc.add(new Field("content", new FileReader(f)));
           doc.add(new Field("fileName", f.getName(), Field.Store.YES,
                  Field.Index.NOT_ANALYZED));
           doc.add(new Field("filePath", f.getCanonicalPath(),
                  Field.Store.YES, Field.Index.NOT_ANALYZED));
       } catch (Exception e) {
           e.printStackTrace();
       }
       return doc;
    }
 
    public void indexFile(File f) throws Exception {
       System.out.println("make indexfile is " + f.getCanonicalPath());
       Document doc = getDocument(f);//创建
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值