Package org.apache.lucene.search

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


    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

    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

        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

          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

    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

    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

    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

        }
        writer.close();
        IndexReader reader = IndexReader.open(dir, policy, false);
        reader.deleteDocument(3*i+1);
        reader.setNorm(4*i+1, "content", 2.0F);
        IndexSearcher searcher = newSearcher(reader);
        ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
        assertEquals(16*(1+i), hits.length);
        // this is a commit
        if (VERBOSE) {
          System.out.println("TEST: close reader numOnCommit=" + policy.numOnCommit);
        }
        reader.close();
        searcher.close();
      }
      conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
          .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy);
      mp = conf.getMergePolicy();
      if (mp instanceof LogMergePolicy) {
        setUseCompoundFile(mp, useCompoundFile);
      }
      IndexReader r = IndexReader.open(dir);
      final boolean wasOptimized = r.isOptimized();
      r.close();
      writer = new IndexWriter(dir, conf);
      writer.optimize();
      // this is a commit
      writer.close();

      assertEquals(2*(N+1)+1, policy.numOnInit);
      assertEquals(2*(N+2) - (wasOptimized ? 1:0), policy.numOnCommit);

      IndexSearcher searcher = new IndexSearcher(dir, false);
      ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
      assertEquals(176, hits.length);

      // Simplistic check: just verify only the past N segments_N's still
      // exist, and, I can open a reader on each:
      long gen = SegmentInfos.getCurrentSegmentGeneration(dir);

      dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
      int expectedCount = 176;
      searcher.close();
      for(int i=0;i<N+1;i++) {
        try {
          IndexReader reader = IndexReader.open(dir, true);

          // Work backwards in commits on what the expected
          // count should be.
          searcher = newSearcher(reader);
          hits = searcher.search(query, null, 1000).scoreDocs;
          if (i > 1) {
            if (i % 2 == 0) {
              expectedCount += 1;
            } else {
              expectedCount -= 17;
            }
          }
          assertEquals(expectedCount, hits.length);
          searcher.close();
          reader.close();
          if (i == N) {
            fail("should have failed on commits before last 5");
          }
        } catch (IOException e) {
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.