Search in sources :

Example 16 with LeafReaderContext

use of org.apache.lucene.index.LeafReaderContext in project elasticsearch by elastic.

the class IndexFieldDataServiceTests method testFieldDataCacheListener.

public void testFieldDataCacheListener() throws Exception {
    final IndexService indexService = createIndex("test");
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    // copy the ifdService since we can set the listener only once.
    final IndexFieldDataService ifdService = new IndexFieldDataService(indexService.getIndexSettings(), indicesService.getIndicesFieldDataCache(), indicesService.getCircuitBreakerService(), indexService.mapperService());
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType mapper1 = new TextFieldMapper.Builder("s").fielddata(true).build(ctx).fieldType();
    final IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new KeywordAnalyzer()));
    Document doc = new Document();
    doc.add(new StringField("s", "thisisastring", Store.NO));
    writer.addDocument(doc);
    DirectoryReader open = DirectoryReader.open(writer);
    final boolean wrap = randomBoolean();
    final IndexReader reader = wrap ? ElasticsearchDirectoryReader.wrap(open, new ShardId("test", "_na_", 1)) : open;
    final AtomicInteger onCacheCalled = new AtomicInteger();
    final AtomicInteger onRemovalCalled = new AtomicInteger();
    ifdService.setListener(new IndexFieldDataCache.Listener() {

        @Override
        public void onCache(ShardId shardId, String fieldName, Accountable ramUsage) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onCacheCalled.incrementAndGet();
        }

        @Override
        public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, long sizeInBytes) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onRemovalCalled.incrementAndGet();
        }
    });
    IndexFieldData<?> ifd = ifdService.getForField(mapper1);
    LeafReaderContext leafReaderContext = reader.getContext().leaves().get(0);
    AtomicFieldData load = ifd.load(leafReaderContext);
    assertEquals(1, onCacheCalled.get());
    assertEquals(0, onRemovalCalled.get());
    reader.close();
    load.close();
    writer.close();
    assertEquals(1, onCacheCalled.get());
    assertEquals(1, onRemovalCalled.get());
    ifdService.clear();
}
Also used : IndexService(org.elasticsearch.index.IndexService) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Accountable(org.apache.lucene.util.Accountable) IndicesService(org.elasticsearch.indices.IndicesService) ContentPath(org.elasticsearch.index.mapper.ContentPath) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 17 with LeafReaderContext

use of org.apache.lucene.index.LeafReaderContext in project elasticsearch by elastic.

the class AbstractFieldDataImplTestCase method testMultiValueWithMissing.

public void testMultiValueWithMissing() throws Exception {
    fillMultiValueWithMissing();
    IndexFieldData indexFieldData = getForField("value");
    List<LeafReaderContext> readerContexts = refreshReader();
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData fieldData = indexFieldData.load(readerContext);
        assertThat(fieldData.ramBytesUsed(), greaterThanOrEqualTo(minRamBytesUsed()));
        SortedBinaryDocValues bytesValues = fieldData.getBytesValues();
        assertValues(bytesValues, 0, two(), four());
        assertValues(bytesValues, 1, Strings.EMPTY_ARRAY);
        assertValues(bytesValues, 2, three());
    }
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 18 with LeafReaderContext

use of org.apache.lucene.index.LeafReaderContext in project elasticsearch by elastic.

the class AbstractFieldDataImplTestCase method testSingleValueWithMissing.

public void testSingleValueWithMissing() throws Exception {
    fillSingleValueWithMissing();
    IndexFieldData indexFieldData = getForField("value");
    List<LeafReaderContext> readerContexts = refreshReader();
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData fieldData = indexFieldData.load(readerContext);
        assertThat(fieldData.ramBytesUsed(), greaterThanOrEqualTo(minRamBytesUsed()));
        SortedBinaryDocValues bytesValues = fieldData.getBytesValues();
        assertValues(bytesValues, 0, two());
        assertValues(bytesValues, 1, Strings.EMPTY_ARRAY);
        assertValues(bytesValues, 2, three());
    }
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 19 with LeafReaderContext

use of org.apache.lucene.index.LeafReaderContext in project elasticsearch by elastic.

the class AbstractFieldDataImplTestCase method testMissingValueForAll.

public void testMissingValueForAll() throws Exception {
    fillAllMissing();
    IndexFieldData indexFieldData = getForField("value");
    List<LeafReaderContext> readerContexts = refreshReader();
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData fieldData = indexFieldData.load(readerContext);
        // Some impls (FST) return size 0 and some (PagedBytes) do take size in the case no actual data is loaded
        assertThat(fieldData.ramBytesUsed(), greaterThanOrEqualTo(0L));
        SortedBinaryDocValues bytesValues = fieldData.getBytesValues();
        assertValues(bytesValues, 0, Strings.EMPTY_ARRAY);
        assertValues(bytesValues, 1, Strings.EMPTY_ARRAY);
        assertValues(bytesValues, 2, Strings.EMPTY_ARRAY);
        SortedBinaryDocValues hashedBytesValues = fieldData.getBytesValues();
        assertValues(hashedBytesValues, 0, Strings.EMPTY_ARRAY);
        assertValues(hashedBytesValues, 1, Strings.EMPTY_ARRAY);
        assertValues(hashedBytesValues, 2, Strings.EMPTY_ARRAY);
    }
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 20 with LeafReaderContext

use of org.apache.lucene.index.LeafReaderContext in project elasticsearch by elastic.

the class AbstractFieldDataTestCase method testEmpty.

public void testEmpty() throws Exception {
    Document d = new Document();
    d.add(new StringField("field", "value", Field.Store.NO));
    writer.addDocument(d);
    refreshReader();
    IndexFieldData fieldData = getForField("non_existing_field");
    int max = randomInt(7);
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData previous = null;
        for (int i = 0; i < max; i++) {
            AtomicFieldData current = fieldData.load(readerContext);
            assertThat(current.ramBytesUsed(), equalTo(0L));
            if (previous != null) {
                assertThat(current, not(sameInstance(previous)));
            }
            previous = current;
        }
    }
}
Also used : StringField(org.apache.lucene.document.StringField) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Document(org.apache.lucene.document.Document)

Aggregations

LeafReaderContext (org.apache.lucene.index.LeafReaderContext)335 LeafReader (org.apache.lucene.index.LeafReader)73 Document (org.apache.lucene.document.Document)71 IOException (java.io.IOException)69 BytesRef (org.apache.lucene.util.BytesRef)67 Directory (org.apache.lucene.store.Directory)61 Term (org.apache.lucene.index.Term)52 IndexSearcher (org.apache.lucene.search.IndexSearcher)49 IndexReader (org.apache.lucene.index.IndexReader)48 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)45 DirectoryReader (org.apache.lucene.index.DirectoryReader)44 Bits (org.apache.lucene.util.Bits)44 NumericDocValues (org.apache.lucene.index.NumericDocValues)43 ArrayList (java.util.ArrayList)41 Weight (org.apache.lucene.search.Weight)37 Terms (org.apache.lucene.index.Terms)36 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)36 Scorer (org.apache.lucene.search.Scorer)36 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)34 Query (org.apache.lucene.search.Query)34