Package org.apache.lucene.search

Examples of org.apache.lucene.search.IndexSearcher


        isFirstRun = false;
      }
    }

    /* Finally we refresh the searchers (this will trigger flushIfNecessary()) */
    IndexSearcher currentSearcher = null;
    try {
      currentSearcher = getCurrentSearcher();
    } finally {
      if (currentSearcher != null)
        disposeIfNecessary(currentSearcher);
View Full Code Here


   * @param indexDir - The directory from which the index can be retrieved.
   */
  public ChannelSearcher(String indexDir) throws IOException {
    this.indexDir = indexDir;
    this.analyzer = new StandardAnalyzer();
    this.searcher = new IndexSearcher(indexDir);
    this.nrOfHits = 0;
  }
View Full Code Here

  public List<E> search(String search) throws Exception {
    try {
      ObjectCollector collector = new ObjectCollector(objects);
      String query = buildQueryString(search);
      IndexSearcher indexSearcher = new IndexSearcher(indexDirectory,
          true);
      indexSearcher.search(queryParser.parse(query), collector);
      indexSearcher.close();
      return collector.getOutput();
    } catch (Exception e) {
      throw new Exception("Query Syntax Error: " + search);
    }
  }
View Full Code Here

  private float query(Object index, Query query) {
//    System.out.println("MB=" + (getMemorySize(index) / (1024.0f * 1024.0f)));
    Searcher searcher = null;
    try {
      if (index instanceof Directory)
        searcher = new IndexSearcher((Directory)index);
      else
        searcher = ((MemoryIndex) index).createSearcher();

      final float[] scores = new float[1]; // inits to 0.0f
      searcher.search(query, new HitCollector() {
View Full Code Here

    if (null!=sText)
      oQry.add(new TermQuery(new Term("text",sText)),BooleanClause.Occur.SHOULD);

  String sSegments = Gadgets.chomp(sLuceneIndexPath,File.separator)+"k_mime_msgs"+File.separator+sWorkArea; 
    if (DebugFile.trace) DebugFile.writeln("new IndexSearcher("+sSegments+")");
    IndexSearcher oSearch = new IndexSearcher(sSegments);

    if (iLimit>0) {
      if (DebugFile.trace) DebugFile.writeln("IndexSearcher.search("+oQry.toString()+", null, "+String.valueOf(iLimit)+")");
      TopDocs oTopSet = oSearch.search(oQry, null, iLimit);
      if (oTopSet.scoreDocs!=null) {
        ScoreDoc[] oTopDoc = oTopSet.scoreDocs;
        int iDocCount = oTopDoc.length;
        if (DebugFile.trace) DebugFile.writeln("doc count is "+String.valueOf(iDocCount));
        aRetArr = new MailRecord[iDocCount];
        for (int d=0; d<iDocCount; d++) {
          Document oDoc = oSearch.doc(oTopDoc[d].doc);
          String[] aAbstract = Gadgets.split(oSearch.doc(oTopDoc[d].doc).get("abstract"), '¨');
          aRetArr[d] = new MailRecord(aAbstract[0], aAbstract[1], aAbstract[2],
                                      aAbstract[3], aAbstract[4], aAbstract[5],
                                      oDoc.get("container"));
        } // next
      } else {
        aRetArr = null;
      }
    } else {
      if (DebugFile.trace) DebugFile.writeln("IndexSearcher.search("+oQry.toString()+")");
      Hits oHitSet = oSearch.search(oQry);
      int iHitCount = oHitSet.length();
      if (DebugFile.trace) DebugFile.writeln("hit count is "+String.valueOf(iHitCount));
      if (iHitCount>0) {
        aRetArr = new MailRecord[iHitCount];
        for (int h=0; h<iHitCount; h++) {
View Full Code Here

  }

  private void createIndexSearcher(String path) throws IOException {
    File indexFile = new File(path);
    Directory directory = FSDirectory.open(indexFile);
    searcher = new IndexSearcher(directory);
    openIndexDate = getCurrentIndexDate();
  }
View Full Code Here

      IndexReader reader = IndexReader.open(ramDir);
      String queryString = QUERY;
      QueryParser parser=new QueryParser(FIELD_NAME, /*new StandardAnalyzer()/*/XFactory.getWriterAnalyzer());
      Query query = parser.parse(queryString);
      System.out.println(query);
      Searcher searcher = new IndexSearcher(ramDir);
      query = query.rewrite(reader);
      System.out.println(query);
      System.out.println("Searching for: " + query.toString(FIELD_NAME));
      Hits hits = searcher.search(query);
     
      BoldFormatter formatter = new BoldFormatter();
      Highlighter highlighter =new Highlighter(formatter,new QueryScorer(query));
      highlighter.setTextFragmenter(new SimpleFragmenter(50));
      for (int i = 0; i < hits.length(); i++)
View Full Code Here

    IndexReader reader = IndexReader.open(ramDir);
    String queryString = QUERY;
    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, FIELD_NAME, analyzer);
    Query query = parser.parse(queryString);
    Searcher searcher = new IndexSearcher(ramDir);
    query = query.rewrite(reader);
    System.out.println("Searching for: " + query.toString(FIELD_NAME));
    TopDocs hits = searcher.search(query, 10);

    BoldFormatter formatter = new BoldFormatter();
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(
        query));
    highlighter.setTextFragmenter(new SimpleFragmenter(50));
    for (int i = 0; i < hits.scoreDocs.length; i++) {
      int docId = hits.scoreDocs[i].doc;
      Document hit = searcher.doc(docId);
      String text = hit.get(FIELD_NAME);
      int maxNumFragmentsRequired = 5;
      String fragmentSeparator = "...";
      TermPositionVector tpv = (TermPositionVector) reader
          .getTermFreqVector(docId, FIELD_NAME);
View Full Code Here

    IndexReader reader = IndexReader.open(ramDir);
    String queryString = QUERY;
    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, FIELD_NAME, analyzer);
    Query query = parser.parse(queryString);
    Searcher searcher = new IndexSearcher(ramDir);
    query = query.rewrite(reader);
    System.out.println("Searching for: " + query.toString(FIELD_NAME));
    TopDocs hits = searcher.search(query, 10);   

    BoldFormatter formatter = new BoldFormatter();
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(
        query));
    highlighter.setTextFragmenter(new SimpleFragmenter(50));
    for (int i = 0; i < hits.scoreDocs.length; i++) {
      int docId = hits.scoreDocs[i].doc;
      Document hit = searcher.doc(docId);
      String text = hit.get(FIELD_NAME);
      int maxNumFragmentsRequired = 5;
      String fragmentSeparator = "...";
      TermPositionVector tpv = (TermPositionVector) reader
          .getTermFreqVector(docId, FIELD_NAME);
View Full Code Here

    Analyzer analyzer = new IKAnalyzer();
  
   
    Directory directory = null;
    IndexWriter iwriter = null;
    IndexSearcher isearcher = null;
    try {
      //建立内存索引对象
      directory = new RAMDirectory();  
      iwriter = new IndexWriter(directory, analyzer, true , IndexWriter.MaxFieldLength.LIMITED);
      Document doc = new Document();
      doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED));
      doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED));
      iwriter.addDocument(doc);
      iwriter.close();
     
        //实例化搜索器  
      isearcher = new IndexSearcher(directory);     
      //在索引器中使用IKSimilarity相似度评估器
      isearcher.setSimilarity(new IKSimilarity());
     
      String keyword = "中文分词工具包";
     
      //使用IKQueryParser查询分析器构造Query对象
      Query query = IKQueryParser.parse(fieldName, keyword);
     
      //搜索相似度最高的5条记录
      TopDocs topDocs = isearcher.search(query , 5);
      System.out.println("命中:" + topDocs.totalHits);
      //输出结果
      ScoreDoc[] scoreDocs = topDocs.scoreDocs;
      for (int i = 0; i < topDocs.totalHits; i++){
        Document targetDoc = isearcher.doc(scoreDocs[i].doc);
        System.out.println("内容:" + targetDoc.toString());
      }     
     
    } catch (CorruptIndexException e) {
      e.printStackTrace();
    } catch (LockObtainFailedException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
      if(isearcher != null){
        try {
          isearcher.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if(directory != null){
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.IndexSearcher

Copyright © 2018 www.massapicom. 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.