Java Code Examples for org.apache.lucene.index.FilterDirectoryReader

The following examples show how to use org.apache.lucene.index.FilterDirectoryReader. These examples are extracted from open source projects. 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 Project: crate   Source File: MockEngineSupport.java    License: Apache License 2.0 6 votes vote down vote up
public MockEngineSupport(EngineConfig config, Class<? extends FilterDirectoryReader> wrapper) {
    Settings settings = config.getIndexSettings().getSettings();
    shardId = config.getShardId();
    filterCache = config.getQueryCache();
    filterCachingPolicy = config.getQueryCachingPolicy();
    final long seed =  config.getIndexSettings().getValue(ESIntegTestCase.INDEX_TEST_SEED_SETTING);
    Random random = new Random(seed);
    final double ratio = WRAP_READER_RATIO.get(settings);
    boolean wrapReader = random.nextDouble() < ratio;
    if (logger.isTraceEnabled()) {
        logger.trace("Using [{}] for shard [{}] seed: [{}] wrapReader: [{}]", this.getClass().getName(), shardId, seed, wrapReader);
    }
    mockContext = new MockContext(random, wrapReader, wrapper, settings);
    this.inFlightSearchers = new InFlightSearchers();
    LuceneTestCase.closeAfterSuite(inFlightSearchers); // only one suite closeable per Engine
    this.disableFlushOnClose = DISABLE_FLUSH_ON_CLOSE.get(settings);
}
 
Example 2
Source Project: lucene-solr   Source File: TestSearcherManager.java    License: Apache License 2.0 5 votes vote down vote up
public MyFilterDirectoryReader(DirectoryReader in) throws IOException {
  super(in, 
        new FilterDirectoryReader.SubReaderWrapper() {
          @Override
          public LeafReader wrap(LeafReader reader) {
            FilterLeafReader wrapped = new MyFilterLeafReader(reader);
            assertEquals(reader, wrapped.getDelegate());
            return wrapped;
          }
        });
}
 
Example 3
Source Project: lucene-solr   Source File: TestSearcherManager.java    License: Apache License 2.0 5 votes vote down vote up
public void testCustomDirectoryReader() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  DirectoryReader nrtReader = w.getReader();

  FilterDirectoryReader reader = new MyFilterDirectoryReader(nrtReader);
  assertEquals(nrtReader, reader.getDelegate());
  assertEquals(FilterDirectoryReader.unwrap(nrtReader), FilterDirectoryReader.unwrap(reader));

  SearcherManager mgr = new SearcherManager(reader, null);
  for(int i=0;i<10;i++) {
    w.addDocument(new Document());
    mgr.maybeRefresh();
    IndexSearcher s = mgr.acquire();
    try {
      assertTrue(s.getIndexReader() instanceof MyFilterDirectoryReader);
      for (LeafReaderContext ctx : s.getIndexReader().leaves()) {
        assertTrue(ctx.reader() instanceof MyFilterLeafReader);
      }
    } finally {
      mgr.release(s);
    }
  }
  mgr.close();
  w.close();
  dir.close();
}
 
Example 4
Source Project: lucene-solr   Source File: UninvertingReader.java    License: Apache License 2.0 5 votes vote down vote up
public UninvertingDirectoryReader(DirectoryReader in, final Function<String, Type> mapper) throws IOException {
  super(in, new FilterDirectoryReader.SubReaderWrapper() {
    @Override
    public LeafReader wrap(LeafReader reader) {
      return UninvertingReader.wrap(reader, mapper);
    }
  });
  this.mapper = mapper;
}
 
Example 5
Source Project: crate   Source File: ElasticsearchDirectoryReader.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tries to unwrap the given reader until the first
 * {@link ElasticsearchDirectoryReader} instance is found or {@code null}
 * if no instance is found.
 */
public static ElasticsearchDirectoryReader getElasticsearchDirectoryReader(DirectoryReader reader) {
    if (reader instanceof FilterDirectoryReader) {
        if (reader instanceof ElasticsearchDirectoryReader) {
            return (ElasticsearchDirectoryReader) reader;
        } else {
            // We need to use FilterDirectoryReader#getDelegate and not FilterDirectoryReader#unwrap, because
            // If there are multiple levels of filtered leaf readers then with the unwrap() method it immediately
            // returns the most inner leaf reader and thus skipping of over any other filtered leaf reader that
            // may be instance of ElasticsearchLeafReader. This can cause us to miss the shardId.
            return getElasticsearchDirectoryReader(((FilterDirectoryReader) reader).getDelegate());
        }
    }
    return null;
}
 
Example 6
Source Project: crate   Source File: FieldMaskingReader.java    License: Apache License 2.0 5 votes vote down vote up
public FieldMaskingReader(String field, DirectoryReader in) throws IOException {
    super(in, new FilterDirectoryReader.SubReaderWrapper() {
        @Override
        public LeafReader wrap(LeafReader reader) {
            return new FilterLeafReader(new FieldFilterLeafReader(reader, Collections.singleton(field), true)) {

                // FieldFilterLeafReader does not forward cache helpers
                // since it considers it is illegal because of the fact
                // that it changes the content of the index. However we
                // want this behavior for tests, and security plugins
                // are careful to only use the cache when it's valid

                @Override
                public CacheHelper getReaderCacheHelper() {
                    return reader.getReaderCacheHelper();
                }

                @Override
                public CacheHelper getCoreCacheHelper() {
                    return reader.getCoreCacheHelper();
                }
            };
        }
    });
    this.field = field;

}
 
Example 7
Source Project: crate   Source File: ElasticsearchDirectoryReader.java    License: Apache License 2.0 4 votes vote down vote up
private ElasticsearchDirectoryReader(DirectoryReader in, FilterDirectoryReader.SubReaderWrapper wrapper,
        ShardId shardId) throws IOException {
    super(in, wrapper);
    this.wrapper = wrapper;
    this.shardId = shardId;
}
 
Example 8
Source Project: crate   Source File: MockEngineFactoryPlugin.java    License: Apache License 2.0 4 votes vote down vote up
protected Class<? extends FilterDirectoryReader> getReaderWrapperClass() {
    return AssertingDirectoryReader.class;
}
 
Example 9
Source Project: crate   Source File: MockInternalEngine.java    License: Apache License 2.0 4 votes vote down vote up
MockInternalEngine(EngineConfig config,  Class<? extends FilterDirectoryReader> wrapper) throws EngineException {
    super(config);
    wrapperClass = wrapper;

}
 
Example 10
Source Project: crate   Source File: MockEngineFactory.java    License: Apache License 2.0 4 votes vote down vote up
public MockEngineFactory(Class<? extends FilterDirectoryReader> wrapper) {
    this.wrapper = wrapper;
}
 
Example 11
Source Project: crate   Source File: MockEngineSupport.java    License: Apache License 2.0 4 votes vote down vote up
public MockContext(Random random, boolean wrapReader, Class<? extends FilterDirectoryReader> wrapper, Settings indexSettings) {
    this.random = random;
    this.wrapReader = wrapReader;
    this.wrapper = wrapper;
    this.indexSettings = indexSettings;
}