Java Code Examples for org.apache.lucene.util.LuceneTestCase#newSearcher()

The following examples show how to use org.apache.lucene.util.LuceneTestCase#newSearcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PayloadHelper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up a RAM-resident Directory, and adds documents (using English.intToEnglish()) with two fields: field and multiField
 * and analyzes them using the PayloadAnalyzer
 * @param similarity The Similarity class to use in the Searcher
 * @param numDocs The num docs to add
 * @return An IndexSearcher
 */
// TODO: randomize
public IndexSearcher setUp(Random random, Similarity similarity, int numDocs) throws IOException {
  Directory directory = new MockDirectoryWrapper(random, new ByteBuffersDirectory());
  PayloadAnalyzer analyzer = new PayloadAnalyzer();

  // TODO randomize this
  IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
      analyzer).setSimilarity(similarity));
  // writer.infoStream = System.out;
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    doc.add(new TextField(FIELD, English.intToEnglish(i), Field.Store.YES));
    doc.add(new TextField(MULTI_FIELD, English.intToEnglish(i) + "  " + English.intToEnglish(i), Field.Store.YES));
    doc.add(new TextField(NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES));
    writer.addDocument(doc);
  }
  writer.forceMerge(1);
  reader = DirectoryReader.open(writer);
  writer.close();

  IndexSearcher searcher = LuceneTestCase.newSearcher(LuceneTestCase.getOnlyLeafReader(reader));
  searcher.setSimilarity(similarity);
  return searcher;
}
 
Example 2
Source File: QueryUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Given an IndexSearcher, returns a new IndexSearcher whose IndexReader
 * is a MultiReader containing the Reader of the original IndexSearcher,
 * as well as several "empty" IndexReaders -- some of which will have
 * deleted documents in them.  This new IndexSearcher should
 * behave exactly the same as the original IndexSearcher.
 * @param s the searcher to wrap
 * @param edge if negative, s will be the first sub; if 0, s will be in the middle, if positive s will be the last sub
 */
public static IndexSearcher wrapUnderlyingReader(Random random, final IndexSearcher s, final int edge)
  throws IOException {

  IndexReader r = s.getIndexReader();

  // we can't put deleted docs before the nested reader, because
  // it will throw off the docIds
  IndexReader[] readers = new IndexReader[] {
    edge < 0 ? r : new MultiReader(),
    new MultiReader(),
    new MultiReader(edge < 0 ? emptyReader(4) : new MultiReader(),
        new MultiReader(),
        0 == edge ? r : new MultiReader()),
    0 < edge ? new MultiReader() : emptyReader(7),
    new MultiReader(),
    new MultiReader(0 < edge ? new MultiReader() : emptyReader(5),
        new MultiReader(),
        0 < edge ? r : new MultiReader())
  };

  IndexSearcher out = LuceneTestCase.newSearcher(new MultiReader(readers));
  out.setSimilarity(s.getSimilarity());
  return out;
}
 
Example 3
Source File: CoreParserTestIndexData.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
CoreParserTestIndexData(Analyzer analyzer) throws Exception {
  BufferedReader d = new BufferedReader(new InputStreamReader(
      TestCoreParser.class.getResourceAsStream("reuters21578.txt"), StandardCharsets.US_ASCII));
  dir = LuceneTestCase.newDirectory();
  IndexWriter writer = new IndexWriter(dir, LuceneTestCase.newIndexWriterConfig(analyzer));
  String line = d.readLine();
  while (line != null) {
    int endOfDate = line.indexOf('\t');
    String date = line.substring(0, endOfDate).trim();
    String content = line.substring(endOfDate).trim();
    Document doc = new Document();
    doc.add(LuceneTestCase.newTextField("date", date, Field.Store.YES));
    doc.add(LuceneTestCase.newTextField("contents", content, Field.Store.YES));
    doc.add(new IntPoint("date3", Integer.parseInt(date)));
    writer.addDocument(doc);
    line = d.readLine();
  }
  d.close();
  writer.close();
  reader = DirectoryReader.open(dir);
  searcher = LuceneTestCase.newSearcher(reader, false);
}
 
Example 4
Source File: TestSearcherManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEvilSearcherFactory() throws Exception {
  final Random random = random();
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random, dir);
  w.commit();

  final IndexReader other = DirectoryReader.open(dir);

  final SearcherFactory theEvilOne = new SearcherFactory() {
    @Override
    public IndexSearcher newSearcher(IndexReader ignored, IndexReader previous) {
      return LuceneTestCase.newSearcher(other);
    }
    };

  expectThrows(IllegalStateException.class, () -> {
    new SearcherManager(dir, theEvilOne);
  });
  expectThrows(IllegalStateException.class, () -> {
    new SearcherManager(w.w, random.nextBoolean(), false, theEvilOne);
  });
  w.close();
  other.close();
  dir.close();
}
 
Example 5
Source File: TestControlledRealTimeReopenThread.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEvilSearcherFactory() throws Exception {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  w.commit();

  final IndexReader other = DirectoryReader.open(dir);

  final SearcherFactory theEvilOne = new SearcherFactory() {
    @Override
    public IndexSearcher newSearcher(IndexReader ignored, IndexReader previous) {
      return LuceneTestCase.newSearcher(other);
    }
    };

  expectThrows(IllegalStateException.class, () -> {
    new SearcherManager(w.w, false, false, theEvilOne);
  });

  w.close();
  other.close();
  dir.close();
}