ParalellMultiSearcher:多线程搜索

package  ch4.searcher;

import  java.io.IOException;
import  org.apache.lucene.analysis.standard. * ;
import  org.apache.lucene.document. * ;
import  org.apache.lucene.index. * ;
import  org.apache.lucene.search. * ;
import  org.apache.lucene.search.spans. * ;
import  org.apache.lucene.queryParser.ParseException;
import  org.apache.lucene.queryParser. * ;
public   class  ParallelMultiSearcherTest 
{
    
private   static  String INDEX_STORE_PATH = " d:/ch4/Termindex " ;
    
private   static   int  INDEX_DIR_NUMBER = 100 ;
    
public   static   void  main(String[] args) throws  Exception
    { 
      
for ( int  i = 0 ;i < INDEX_DIR_NUMBER;i ++ )
      {
          IndexWriter writer
= new  IndexWriter(INDEX_STORE_PATH + i, new  StandardAnalyzer(), true );
          Document doc
= new  Document();
          Field f
= new  Field( " bookname " , " 钢铁是怎样炼成的 " ,Field.Store.YES,Field.Index.TOKENIZED);
          doc.add(f);
          writer.close();
      }
      
      IndexSearcher[] searchers
= new  IndexSearcher[INDEX_DIR_NUMBER];
      
for ( int  i = 0 ;i < INDEX_DIR_NUMBER;i ++ )
      {
          searchers[i]
= new  IndexSearcher(INDEX_STORE_PATH + i);
      }
      
      Term t
= new  Term( " bookname " , " " );
      TermQuery q
= new  TermQuery(t);
      
      multiserSearcher(searchers,q);
      paralellSearcher(searchers,q);
    }
    
    
public   static   void  paralellSearcher(IndexSearcher[] searchers,Query q) throws  Exception
    {
        
long  startTime = System.currentTimeMillis(); 
        ParallelMultiSearcher multiSearcher
= new  ParallelMultiSearcher(searchers); 
        Hits hits
= multiSearcher.search(q); 
        
long  endTime = System.currentTimeMillis(); 
        System.out.println(
" ParallelMultiSearcher 搜索耗时为 " + (endTime - startTime) + " 毫秒 " ); 
        
for ( int  i = 0 ;i  < hits.length();i ++ )
        { 
            System.out.println(hits.doc(i)); 
        }

    }
    
public   static   void  multiserSearcher(IndexSearcher[] searchers,Query q) throws  Exception
    {
        
long  startTime = System.currentTimeMillis(); 
        MultiSearcher searcher
= new  MultiSearcher(searchers); 
        Hits hits
= searcher.search(q); 
        
long  endTime = System.currentTimeMillis(); 
        System.out.println(
" MultiSearcher 搜索耗时为 " + (endTime - startTime) + " 毫秒 " ); 
        
for ( int  i = 0 ;i  < hits.length();i ++ )
        { 
        System.out.println(hits.doc(i)); 
        }
    }

}

运行结果:

 

MultiSearcher 搜索耗时为16毫秒
ParallelMultiSearcher 搜索耗时为46毫秒

 

总而言之:索引越多,多线程搜索的效率也就越低;为什么呢?有待提高。

你可能感兴趣的:(多线程,String,Path)