LUCENCE基础应用

  rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

一、建立索引部分

XMLFilesIndexer.java

import java.io.File;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.index.IndexWriter;

 

public class XMLFilesIndexer {

 

    public void createIndex(String indexFileDir, String sourceDir) {

       try {

           IndexWriter writer = new IndexWriter(indexFileDir,

                  new StandardAnalyzer(), true);

           System.out.println("Indexing to directory '" + indexFileDir

                  + "'...");

           indexDocs(writer, new File(sourceDir));

           System.out.println("Optimizing...");

           writer.optimize();

           writer.close();

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

 

    void indexDocs(IndexWriter writer, File file) throws IOException {

       if (file.canRead()) {

           if (file.isDirectory()) {

              String[] files = file.list();

              if (files != null) {

                  for (int i = 0; i < files.length; i++) {

                     indexDocs(writer, new File(file, files[i]));

                  }

              }

           } else {

              if (file.getName().endsWith("xml")) {

                  System.out.println("adding " + file);

                  try {                writer.addDocument(XMLDocument.documetnByNode(file));

                  } catch (Exception e) {

                     e.printStackTrace();

                  }

              }

           }

       }

    }

}

 

二、检索部分

XMLFilesSearcher.java

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.*;

 

public class XMLFilesSearcher {

 

    public void search(String keyword, String indexDir)

           throws Exception {

       String field = "contents";

       IndexReader reader = IndexReader.open(indexDir);

       Searcher searcher = new IndexSearcher(reader);

       Analyzer analyzer = new StandardAnalyzer();

       QueryParser parser = new QueryParser(field, analyzer);

       Query query = parser.parse(keyword);

       Hits hits = searcher.search(query);

       for (int i = 0; i < hits.length(); i++) {

           Document doc = hits.doc(i);

           String path = doc.get("path");

           if (path != null) {

              System.out.println((i + 1) + ". " + path);

           } else {

              System.out.println((i + 1) + ". " + "No path for this document");

           }

       }

       reader.close();

    }

}

 

三、应用部分

public class Main {

 

    public static void main(String[] args) {

//     XMLFilesIndexer xmlFilesIndexer = new XMLFilesIndexer();

//     xmlFilesIndexer.createIndex("Data//index", "Data//data");

      

       XMLFilesSearcher xmlFilesSearcher = new XMLFilesSearcher();

       try {

           xmlFilesSearcher.search(" OR ", "Data//index");

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

}

 

XML辅助类:XMLDocument

import java.io.CharArrayReader;

import java.io.File;

import java.io.FileReader;

import nu.xom.Builder;

import nu.xom.Nodes;

import nu.xom.Document;

import nux.xom.pool.XQueryFactory;

import nux.xom.xquery.XQuery;

import org.apache.lucene.document.Field;

 

public class XMLDocument {

   

    public static org.apache.lucene.document.Document documentByFile(File file){

       org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();

       try{

           doc.add(new Field("path", file.getPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));

           doc.add(new Field("contents", new FileReader(file)));

       }catch(Exception e){

           e.printStackTrace();

       }

       return doc;

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值