Examples of IndexSearcher


Examples of org.apache.lucene.search.IndexSearcher

    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

Examples of org.apache.lucene.search.IndexSearcher

    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

Examples of org.apache.lucene.search.IndexSearcher

    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

Examples of org.apache.lucene.search.IndexSearcher

    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

Examples of org.apache.lucene.search.IndexSearcher

    iw.close();
  }

  public void testSearch() {
    try {
      IndexSearcher searcher = new IndexSearcher(dir);
      QueryParser qp = new QueryParser(Version.LUCENE_29, "txt", analyzer);
      Query q = qp.parse("西伯利亚")//2008年底
      System.out.println(q);
      TopDocs tds = searcher.search(q, 10);
      System.out.println("======size:"+tds.totalHits+"========");
      for(ScoreDoc sd : tds.scoreDocs) {
        System.out.println(sd.score);
        System.out.println(searcher.doc(sd.doc).get("txt"));
      }
    } catch (CorruptIndexException e) {
     
      e.printStackTrace();
    } catch (IOException e) {
View Full Code Here

Examples of org.apache.lucene.search.IndexSearcher

        Field.Index.NOT_ANALYZED));
    modifier.addDocument(doc);
  }

  private int getHitCount(Directory dir, Term term) throws IOException {
    IndexSearcher searcher = new IndexSearcher(dir, true);
    int hitCount = searcher.search(new TermQuery(term), null, 1000).totalHits;
    searcher.close();
    return hitCount;
  }
View Full Code Here

Examples of org.apache.lucene.search.IndexSearcher

          fail(testName
               + ":exception when creating IndexReader after disk full during close: "
               + e);
        }

        IndexSearcher searcher = newSearcher(newReader);
        ScoreDoc[] hits = null;
        try {
          hits = searcher.search(new TermQuery(searchTerm), null, 1000).scoreDocs;
        }
        catch (IOException e) {
          e.printStackTrace();
          fail(testName + ": exception when searching: " + e);
        }
        int result2 = hits.length;
        if (success) {
          if (x == 0 && result2 != END_COUNT) {
            fail(testName
                 + ": method did not throw exception but hits.length for search on term 'aaa' is "
                 + result2 + " instead of expected " + END_COUNT);
          } else if (x == 1 && result2 != START_COUNT && result2 != END_COUNT) {
            // It's possible that the first exception was
            // "recoverable" wrt pending deletes, in which
            // case the pending deletes are retained and
            // then re-flushing (with plenty of disk
            // space) will succeed in flushing the
            // deletes:
            fail(testName
                 + ": method did not throw exception but hits.length for search on term 'aaa' is "
                 + result2 + " instead of expected " + START_COUNT + " or " + END_COUNT);
          }
        } else {
          // On hitting exception we still may have added
          // all docs:
          if (result2 != START_COUNT && result2 != END_COUNT) {
            err.printStackTrace();
            fail(testName
                 + ": method did throw exception but hits.length for search on term 'aaa' is "
                 + result2 + " instead of expected " + START_COUNT + " or " + END_COUNT);
          }
        }

        searcher.close();
        newReader.close();
      }

      modifier.close();
      dir.close();
View Full Code Here

Examples of org.apache.lucene.search.IndexSearcher

    doTestRank(FLOAT_FIELD,FieldScoreQuery.Type.FLOAT);
  }

  // Test that FieldScoreQuery returns docs in expected order.
  private void doTestRank (String field, FieldScoreQuery.Type tp) throws Exception {
    IndexSearcher s = new IndexSearcher(dir, true);
    Query q = new FieldScoreQuery(field,tp);
    log("test: "+q);
    QueryUtils.check(random, q,s);
    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
    assertEquals("All docs should be matched!",N_DOCS,h.length);
    String prevID = "ID"+(N_DOCS+1); // greater than all ids of docs in this test
    for (int i=0; i<h.length; i++) {
      String resID = s.doc(h[i].doc).get(ID_FIELD);
      log(i+".   score="+h[i].score+"  -  "+resID);
      log(s.explain(q,h[i].doc));
      assertTrue("res id "+resID+" should be < prev res id "+prevID, resID.compareTo(prevID)<0);
      prevID = resID;
    }
    s.close();
  }
View Full Code Here

Examples of org.apache.lucene.search.IndexSearcher

    doTestExactScore(FLOAT_FIELD,FieldScoreQuery.Type.FLOAT);
  }

  // Test that FieldScoreQuery returns docs with expected score.
  private void doTestExactScore (String field, FieldScoreQuery.Type tp) throws Exception {
    IndexSearcher s = new IndexSearcher(dir, true);
    Query q = new FieldScoreQuery(field,tp);
    TopDocs td = s.search(q,null,1000);
    assertEquals("All docs should be matched!",N_DOCS,td.totalHits);
    ScoreDoc sd[] = td.scoreDocs;
    for (ScoreDoc aSd : sd) {
      float score = aSd.score;
      log(s.explain(q, aSd.doc));
      String id = s.getIndexReader().document(aSd.doc).get(ID_FIELD);
      float expectedScore = expectedFieldScore(id); // "ID7" --> 7.0
      assertEquals("score of " + id + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
    }
    s.close();
  }
View Full Code Here

Examples of org.apache.lucene.search.IndexSearcher

    expectedArrayTypes.put(FieldScoreQuery.Type.BYTE, new byte[0]);
    expectedArrayTypes.put(FieldScoreQuery.Type.SHORT, new short[0]);
    expectedArrayTypes.put(FieldScoreQuery.Type.INT, new int[0]);
    expectedArrayTypes.put(FieldScoreQuery.Type.FLOAT, new float[0]);
   
    IndexSearcher s = new IndexSearcher(dir, true);
    Object[] innerArray = new Object[s.getIndexReader().getSequentialSubReaders().length];

    boolean warned = false; // print warning once.
    for (int i=0; i<10; i++) {
      FieldScoreQuery q = new FieldScoreQuery(field,tp);
      ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
      assertEquals("All docs should be matched!",N_DOCS,h.length);
      IndexReader[] readers = s.getIndexReader().getSequentialSubReaders();
      for (int j = 0; j < readers.length; j++) {
        IndexReader reader = readers[j];
        try {
          if (i == 0) {
            innerArray[j] = q.valSrc.getValues(reader).getInnerArray();
            log(i + ".  compare: " + innerArray[j].getClass() + " to "
                + expectedArrayTypes.get(tp).getClass());
            assertEquals(
                "field values should be cached in the correct array type!",
                innerArray[j].getClass(), expectedArrayTypes.get(tp).getClass());
          } else {
            log(i + ".  compare: " + innerArray[j] + " to "
                + q.valSrc.getValues(reader).getInnerArray());
            assertSame("field values should be cached and reused!", innerArray[j],
                q.valSrc.getValues(reader).getInnerArray());
          }
        } catch (UnsupportedOperationException e) {
          if (!warned) {
            System.err.println("WARNING: " + testName()
                + " cannot fully test values of " + q);
            warned = true;
          }
        }
      }
    }
    s.close();
    // verify new values are reloaded (not reused) for a new reader
    s = new IndexSearcher(dir, true);
    FieldScoreQuery q = new FieldScoreQuery(field,tp);
    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
    assertEquals("All docs should be matched!",N_DOCS,h.length);
    IndexReader[] readers = s.getIndexReader().getSequentialSubReaders();
    for (int j = 0; j < readers.length; j++) {
      IndexReader reader = readers[j];
      try {
        log("compare: " + innerArray + " to "
            + q.valSrc.getValues(reader).getInnerArray());
        assertNotSame(
            "cached field values should not be reused if reader as changed!",
            innerArray, q.valSrc.getValues(reader).getInnerArray());
      } catch (UnsupportedOperationException e) {
        if (!warned) {
          System.err.println("WARNING: " + testName()
              + " cannot fully test values of " + q);
          warned = true;
        }
      }
    }
    s.close();
  }
View Full Code Here
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.