Lucene给文本索引和搜索功能的应用(3)


import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;


// From chapter 1


/**
 * This code was originally written for  searcher
 *
 */
public class Searcher {


  public static void main(String[] args) throws IllegalArgumentException,
        IOException, ParseException {
                   
    final String indexDir = "e:\\soso\\soso";
        String q = " ";//输入你添加的所以 进行模糊搜索
    docs = query(indexDir, q)
   
    }
   
  public static void search(String indexDir, String q)
    throws IOException, ParseException {
 IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir)));
  // Directory dir = FSDirectory.open(new File(indexDir)); //3
    IndexSearcher is = new IndexSearcher(reader);  //3 


    QueryParser parser = new QueryParser(Version.LUCENE_47,"contents",new SmartChineseAnalyzer(Version.LUCENE_47));       
    Query query = parser.parse(q);              //4 
    long start = System.currentTimeMillis();
    TopDocs hits = is.search(query, 500); //5
 
    //ScoreDoc[] hits = is.search(query, null, 10).scoreDocs;
    long end = System.currentTimeMillis();


    System.err.println("Found " + hits.totalHits +  //6 
      " document(s) (in " + (end - start) +        // 6
      " milliseconds) that matched query '" +    // 6
      q + "':");                                  // 6


    for(ScoreDoc scoreDoc : hits.scoreDocs) {
      Document doc = is.doc(scoreDoc.doc);              //7     
      System.out.println(doc.get("contents"));
    }
    reader.close();
  }
 
    private static List<String> query(String indexDir, String searcher) throws IOException, ParseException{
        if (searcher == null || searcher.length() == -1) {
            return null;
          }
   
        searcher = searcher.trim();
          if (searcher.length() == 0) {
            return null;
          }
         
    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir)));//open the index
    //IndexReader reader = DirectoryReader.open(SimpleFSDirectory.open(new File(indexDir)));//open the index
    IndexSearcher is = new IndexSearcher(reader);//find the content
    QueryParser parser = new QueryParser(Version.LUCENE_47, "contents", new SmartChineseAnalyzer(Version.LUCENE_47));//parser the content
    Query query = parser.parse(searcher);
    TopFieldDocs hits = is.search(query, 100, new Sort(new SortField("contents", SortField.Type.SCORE, false)));
    TopDocs hits1 = is.search(query, 200);//搜索出前200条数据  按照评分进行排序
    List<String> list = new ArrayList<String>();
    for(ScoreDoc scoreDoc : hits.scoreDocs){
    Document doc = is.doc(scoreDoc.doc);
    list.add(doc.get("contents"));
    }
    reader.close();
    return list;
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c2b8cd7d772c9b7f3bc1164f8f47b3cf.html