Lucene

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.Reader;

import java.util.Date;

 

import org.apache.lucene.analysis.Analyzer;

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

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.index.Term;

import org.apache.lucene.queryParser.ParseException;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.ScoreDoc;

import org.apache.lucene.search.TermQuery;

import org.apache.lucene.search.TopDocs;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.store.LockObtainFailedException;

import org.apache.lucene.util.Version;

 

publicclass TestLucene {

 

   /**

    *@paramargs

    *@throwsIOException

    *@throwsLockObtainFailedException

    *@throwsCorruptIndexException

    */

   @SuppressWarnings("deprecation")

   publicstaticvoid main(String[] args) throws CorruptIndexException,

         LockObtainFailedException, IOException {

 

      /**

       *创建索引

       */

      // indexDir is the directory that hosts Lucene's index files

      File indexDir = new File("D:\\luceneIndex");

 

      // dataDir is the directory that hosts the text files that to be indexed

 

      Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);

      IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),

            new IndexWriterConfig(Version.LUCENE_35, luceneAnalyzer));

      indexWriter.deleteAll();

      File dataDir = new File("D:\\luceneData");

      File[] dataFiles = dataDir.listFiles();

 

      long startTime = new Date().getTime();

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

         if (dataFiles[i].isFile()

                && dataFiles[i].getName().endsWith(".txt")) {

            System.out.println("Indexing file "

                   + dataFiles[i].getCanonicalPath());

            Field fieldPath = new Field("path", dataFiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.ANALYZED);

            Field fieldBody = new Field("body", new FileReader(dataFiles[i]));

            Document document = new Document();

            document.add(fieldPath);

            document.add(fieldBody);

            indexWriter.addDocument(document);

         }

      }

      indexWriter.close();

      long endTime = new Date().getTime();

      System.out.println("It takes " + (endTime - startTime)

            + " milliseconds to create index for the files in directory "

            + dataDir.getPath());

 

      /**

       *搜索

       */

      FSDirectory directory = FSDirectory.open(indexDir);

      IndexReader reader = null;

      if (reader == null) {

         reader = IndexReader.open(directory);

      } else {

         IndexReader ir = IndexReader.openIfChanged(reader);

         if (ir != null) {

            reader.close();

            reader = ir;

         }

      }

     

/*    IndexSearcher searcher = new IndexSearcher(reader);

 

      //判断Index目录是否存在

      if (!indexDir.exists()) {

         System.out.println("The Lucene index is not exist");

         return;

      }

     

      String queryStr = "132";

      Term term = new Term("body", queryStr);

      TermQuery luceneQuery = new TermQuery(term);

      TopDocs topDocs = searcher.search(luceneQuery, 10);

      System.out.println("总共查询了:"+topDocs.totalHits);

     

      for(ScoreDoc sd : topDocs.scoreDocs){

         Document doc=searcher.doc(sd.doc);

         System.out.println("("+doc.get("id")+")"+"["+doc.get("name")+"]");

      }

     

      searcher.close();*/

     

     

      search();

     

   }

  

   /**

    *搜索

    */

   publicstaticvoid search(){

      Directory directory=null;

      IndexReader reader=null;

      IndexSearcher searcher=null;

      QueryParser parser=null;

      Query query=null;

      TopDocs tds=null;

      try {

         //1.创建Director

         directory=FSDirectory.open(new File("D:\\luceneIndex"));

         //2.创建IndexReader

         reader=IndexReader.open(directory);

         //3.通过IndexReader创建IndexSearcher

         searcher=new IndexSearcher(reader);

         //4.创建Query

         //创建parser来确定文搜索的内容,第二个参数代表搜索的域

         parser=new QueryParser(Version.LUCENE_35, "body",new StandardAnalyzer(Version.LUCENE_35));

         //创建query,表示搜索域为content包含mysqld的文档

         query=parser.parse("157");

         //5.根据search搜索并返回TopDocs

         tds=searcher.search(query, 1000);

         //6.根据TopDocs获取ScoreDoc

         ScoreDoc[] sds=tds.scoreDocs;

         for(ScoreDoc sd:sds){

            //7.根据searchScoreDoc获取具体的Document对象

            Document doc=searcher.doc(sd.doc);

            //8.更具Document对象获取具体的值

            System.out.println(doc.get("filename")+"["+doc.get("content")+"]");

         }

        

         //9.关闭reader

         reader.close();

      } catch (IOException e) {

         e.printStackTrace();

      } catch (ParseException e) {

         e.printStackTrace();

      }

     

   }

 

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙殿殿主

你的打赏是我精心创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值