Package lucene

Source Code of lucene.IndexTest

package lucene;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
import java.util.Scanner;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

public class IndexTest {
 
  public static String indexDir = "data\\lucene\\luceneIndex";
  public static String dataDir = "data\\lucene\\luceneData";
 
  public static void main(String[] args) throws ParseException, IOException  {
    // indexDir is the directory that hosts Lucene's index files
    makeIndex(indexDir, dataDir);
    while(true){
      System.out.print("輸入搜索關鍵字:");
      Scanner sca = new Scanner(System.in)
      String queryStr = sca.next();
      if (queryStr!=null) {
        if ("0".equals(queryStr)) {
          System.exit(0);
        }
        search1(queryStr, indexDir);
      }
    }
  }
 
  public static void makeIndex(String indexDirStr,String dataDirStr) throws IOException{
    File indexDir = new File(indexDirStr);
    // dataDir is the directory that hosts the text files that to be indexed
    File dataDir = new File(dataDirStr);
//    Analyzer luceneAnalyzer = new StandardAnalyzer();
    File[] dataFiles = dataDir.listFiles();
    //IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,true);
    IndexWriter indexWriter = new IndexWriter(indexDir, new ChineseAnalyzer(), true);
    long startTime = System.currentTimeMillis();
    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());
        Document document = new Document();
        Reader txtReader = new FileReader(dataFiles[i]);
        document.add(Field.Text("path", dataFiles[i].getCanonicalPath()));
        document.add(Field.Text("contents", txtReader));
        indexWriter.addDocument(document);
      }
    }
    indexWriter.optimize();
    indexWriter.close();
    long endTime = System.currentTimeMillis();

    System.out.println("It takes " + (endTime - startTime)
        + " milliseconds to create index for the files in directory "
        + dataDir.getPath());
  }
 
  public static int search2(String qc, String indexDirStr) throws ParseException, IOException  {
    // 从索引目录创建索引
    IndexSearcher _searcher = new IndexSearcher(indexDirStr);
    // 创建标准分析器
    Analyzer analyzer = new ChineseAnalyzer();
    // 查询条件
    String line = qc;
    // Query是一个抽象类
    Query query = QueryParser.parse(line, "contents", analyzer);
    Hits hits = _searcher.search(query);
    _searcher.close();
    return hits.length();
  }

  public static void search1(String qc, String indexDirStr) throws ParseException, IOException  {
    // 从索引目录创建索引
    IndexSearcher _searcher = new IndexSearcher(indexDirStr);
    // 创建标准分析器
    Analyzer analyzer = new ChineseAnalyzer();
    // 查询条件
    String line = qc;
    // Query是一个抽象类
    Query query = QueryParser.parse(line, "contents", analyzer);
    Hits hits = _searcher.search(query);
    System.out.println(" 总共找到[" + hits.length() + "]条匹配記錄");

    final int HITS_PER_PAGE = 1;
    for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
      int end = Math.min(hits.length(), start + HITS_PER_PAGE);
      for (int i = start; i < end; i++) {
        Document doc = hits.doc(i);
        String path = doc.get("path");
        if (path != null) {
          System.out.println("File: " + doc.get("path"));  
          System.out.println(doc.get("contents"));
        } else {
          System.out.println("没有找到!");
        }
      }
    }
    _searcher.close();
  }
}
TOP

Related Classes of lucene.IndexTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.