Search in sources :

Example 1 with FieldMaskingReader

use of org.elasticsearch.test.FieldMaskingReader in project elasticsearch by elastic.

the class FieldDataCacheTests method testLoadGlobal_neverCacheIfFieldIsMissing.

public void testLoadGlobal_neverCacheIfFieldIsMissing() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    iwc.setMergePolicy(NoMergePolicy.INSTANCE);
    IndexWriter iw = new IndexWriter(dir, iwc);
    long numDocs = scaledRandomIntBetween(32, 128);
    for (int i = 1; i <= numDocs; i++) {
        Document doc = new Document();
        doc.add(new SortedSetDocValuesField("field1", new BytesRef(String.valueOf(i))));
        doc.add(new StringField("field2", String.valueOf(i), Field.Store.NO));
        iw.addDocument(doc);
        if (i % 24 == 0) {
            iw.commit();
        }
    }
    iw.close();
    DirectoryReader ir = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(dir), new ShardId("_index", "_na_", 0));
    DummyAccountingFieldDataCache fieldDataCache = new DummyAccountingFieldDataCache();
    // Testing SortedSetDVOrdinalsIndexFieldData:
    SortedSetDVOrdinalsIndexFieldData sortedSetDVOrdinalsIndexFieldData = createSortedDV("field1", fieldDataCache);
    sortedSetDVOrdinalsIndexFieldData.loadGlobal(ir);
    assertThat(fieldDataCache.cachedGlobally, equalTo(1));
    sortedSetDVOrdinalsIndexFieldData.loadGlobal(new FieldMaskingReader("field1", ir));
    assertThat(fieldDataCache.cachedGlobally, equalTo(1));
    // Testing PagedBytesIndexFieldData
    PagedBytesIndexFieldData pagedBytesIndexFieldData = createPagedBytes("field2", fieldDataCache);
    pagedBytesIndexFieldData.loadGlobal(ir);
    assertThat(fieldDataCache.cachedGlobally, equalTo(2));
    pagedBytesIndexFieldData.loadGlobal(new FieldMaskingReader("field2", ir));
    assertThat(fieldDataCache.cachedGlobally, equalTo(2));
    ir.close();
    dir.close();
}
Also used : PagedBytesIndexFieldData(org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) FieldMaskingReader(org.elasticsearch.test.FieldMaskingReader) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) SortedSetDVOrdinalsIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedSetDVOrdinalsIndexFieldData) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with FieldMaskingReader

use of org.elasticsearch.test.FieldMaskingReader in project elasticsearch by elastic.

the class IndexShardTests method testSearcherWrapperIsUsed.

public void testSearcherWrapperIsUsed() throws IOException {
    IndexShard shard = newStartedShard(true);
    indexDoc(shard, "test", "0", "{\"foo\" : \"bar\"}");
    indexDoc(shard, "test", "1", "{\"foobar\" : \"bar\"}");
    shard.refresh("test");
    Engine.GetResult getResult = shard.get(new Engine.Get(false, new Term(UidFieldMapper.NAME, Uid.createUid("test", "1"))));
    assertTrue(getResult.exists());
    assertNotNull(getResult.searcher());
    getResult.release();
    try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
        TopDocs search = searcher.searcher().search(new TermQuery(new Term("foo", "bar")), 10);
        assertEquals(search.totalHits, 1);
        search = searcher.searcher().search(new TermQuery(new Term("foobar", "bar")), 10);
        assertEquals(search.totalHits, 1);
    }
    IndexSearcherWrapper wrapper = new IndexSearcherWrapper() {

        @Override
        public DirectoryReader wrap(DirectoryReader reader) throws IOException {
            return new FieldMaskingReader("foo", reader);
        }

        @Override
        public IndexSearcher wrap(IndexSearcher searcher) throws EngineException {
            return searcher;
        }
    };
    closeShards(shard);
    IndexShard newShard = newShard(ShardRoutingHelper.reinitPrimary(shard.routingEntry()), shard.shardPath(), shard.indexSettings().getIndexMetaData(), wrapper, () -> {
    }, null);
    recoveryShardFromStore(newShard);
    try (Engine.Searcher searcher = newShard.acquireSearcher("test")) {
        TopDocs search = searcher.searcher().search(new TermQuery(new Term("foo", "bar")), 10);
        assertEquals(search.totalHits, 0);
        search = searcher.searcher().search(new TermQuery(new Term("foobar", "bar")), 10);
        assertEquals(search.totalHits, 1);
    }
    getResult = newShard.get(new Engine.Get(false, new Term(UidFieldMapper.NAME, Uid.createUid("test", "1"))));
    assertTrue(getResult.exists());
    // make sure get uses the wrapped reader
    assertNotNull(getResult.searcher());
    assertTrue(getResult.searcher().reader() instanceof FieldMaskingReader);
    getResult.release();
    closeShards(newShard);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) FieldMaskingReader(org.elasticsearch.test.FieldMaskingReader) Term(org.apache.lucene.index.Term) TopDocs(org.apache.lucene.search.TopDocs) Engine(org.elasticsearch.index.engine.Engine)

Example 3 with FieldMaskingReader

use of org.elasticsearch.test.FieldMaskingReader in project elasticsearch by elastic.

the class IndexShardTests method testSearcherWrapperWorksWithGlobalOrdinals.

public void testSearcherWrapperWorksWithGlobalOrdinals() throws IOException {
    IndexSearcherWrapper wrapper = new IndexSearcherWrapper() {

        @Override
        public DirectoryReader wrap(DirectoryReader reader) throws IOException {
            return new FieldMaskingReader("foo", reader);
        }

        @Override
        public IndexSearcher wrap(IndexSearcher searcher) throws EngineException {
            return searcher;
        }
    };
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).build();
    IndexMetaData metaData = IndexMetaData.builder("test").putMapping("test", "{ \"properties\": { \"foo\":  { \"type\": \"text\", \"fielddata\": true }}}").settings(settings).primaryTerm(0, 1).build();
    IndexShard shard = newShard(new ShardId(metaData.getIndex(), 0), true, "n1", metaData, wrapper);
    recoveryShardFromStore(shard);
    indexDoc(shard, "test", "0", "{\"foo\" : \"bar\"}");
    shard.refresh("created segment 1");
    indexDoc(shard, "test", "1", "{\"foobar\" : \"bar\"}");
    shard.refresh("created segment 2");
    // test global ordinals are evicted
    MappedFieldType foo = shard.mapperService().fullName("foo");
    IndexFieldData.Global ifd = shard.indexFieldDataService().getForField(foo);
    FieldDataStats before = shard.fieldData().stats("foo");
    assertThat(before.getMemorySizeInBytes(), equalTo(0L));
    FieldDataStats after = null;
    try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
        assertThat("we have to have more than one segment", searcher.getDirectoryReader().leaves().size(), greaterThan(1));
        ifd.loadGlobal(searcher.getDirectoryReader());
        after = shard.fieldData().stats("foo");
        assertEquals(after.getEvictions(), before.getEvictions());
        // If a field doesn't exist an empty IndexFieldData is returned and that isn't cached:
        assertThat(after.getMemorySizeInBytes(), equalTo(0L));
    }
    assertEquals(shard.fieldData().stats("foo").getEvictions(), before.getEvictions());
    assertEquals(shard.fieldData().stats("foo").getMemorySizeInBytes(), after.getMemorySizeInBytes());
    shard.flush(new FlushRequest().force(true).waitIfOngoing(true));
    shard.refresh("test");
    assertEquals(shard.fieldData().stats("foo").getMemorySizeInBytes(), before.getMemorySizeInBytes());
    assertEquals(shard.fieldData().stats("foo").getEvictions(), before.getEvictions());
    closeShards(shard);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DirectoryReader(org.apache.lucene.index.DirectoryReader) FieldMaskingReader(org.elasticsearch.test.FieldMaskingReader) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) Settings(org.elasticsearch.common.settings.Settings) Engine(org.elasticsearch.index.engine.Engine) FieldDataStats(org.elasticsearch.index.fielddata.FieldDataStats)

Aggregations

DirectoryReader (org.apache.lucene.index.DirectoryReader)3 FieldMaskingReader (org.elasticsearch.test.FieldMaskingReader)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Engine (org.elasticsearch.index.engine.Engine)2 Document (org.apache.lucene.document.Document)1 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)1 StringField (org.apache.lucene.document.StringField)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Term (org.apache.lucene.index.Term)1 TermQuery (org.apache.lucene.search.TermQuery)1 TopDocs (org.apache.lucene.search.TopDocs)1 Directory (org.apache.lucene.store.Directory)1 BytesRef (org.apache.lucene.util.BytesRef)1 FlushRequest (org.elasticsearch.action.admin.indices.flush.FlushRequest)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)1 Settings (org.elasticsearch.common.settings.Settings)1 FieldDataStats (org.elasticsearch.index.fielddata.FieldDataStats)1 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)1