Search in sources :

Example 6 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext in project elasticsearch by elastic.

the class AbstractSortTestCase method createMockShardContext.

protected QueryShardContext createMockShardContext() {
    Index index = new Index(randomAsciiOfLengthBetween(1, 10), "_na_");
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build());
    IndicesFieldDataCache cache = new IndicesFieldDataCache(Settings.EMPTY, null);
    IndexFieldDataService ifds = new IndexFieldDataService(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), cache, null, null);
    BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, new BitsetFilterCache.Listener() {

        @Override
        public void onRemoval(ShardId shardId, Accountable accountable) {
        }

        @Override
        public void onCache(ShardId shardId, Accountable accountable) {
        }
    });
    long nowInMillis = randomNonNegativeLong();
    return new QueryShardContext(0, idxSettings, bitsetFilterCache, ifds, null, null, scriptService, xContentRegistry(), null, null, () -> nowInMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            return provideMappedFieldType(name);
        }

        @Override
        public ObjectMapper getObjectMapper(String name) {
            BuilderContext context = new BuilderContext(this.getIndexSettings().getSettings(), new ContentPath());
            return new ObjectMapper.Builder<>(name).nested(Nested.newNested(false, false)).build(context);
        }
    };
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Accountable(org.apache.lucene.util.Accountable) Index(org.elasticsearch.index.Index) ContentPath(org.elasticsearch.index.mapper.ContentPath) ShardId(org.elasticsearch.index.shard.ShardId) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) BitsetFilterCache(org.elasticsearch.index.cache.bitset.BitsetFilterCache)

Example 7 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext in project elasticsearch by elastic.

the class FilterFieldDataTests method testFilterByFrequency.

public void testFilterByFrequency() throws Exception {
    Random random = random();
    for (int i = 0; i < 1000; i++) {
        Document d = new Document();
        d.add(new StringField("id", "" + i, Field.Store.NO));
        if (i % 100 == 0) {
            d.add(new StringField("high_freq", "100", Field.Store.NO));
            d.add(new StringField("low_freq", "100", Field.Store.NO));
            d.add(new StringField("med_freq", "100", Field.Store.NO));
        }
        if (i % 10 == 0) {
            d.add(new StringField("high_freq", "10", Field.Store.NO));
            d.add(new StringField("med_freq", "10", Field.Store.NO));
        }
        if (i % 5 == 0) {
            d.add(new StringField("high_freq", "5", Field.Store.NO));
        }
        writer.addDocument(d);
    }
    writer.forceMerge(1, true);
    List<LeafReaderContext> contexts = refreshReader();
    final BuilderContext builderCtx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    {
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("high_freq").fielddata(true).fielddataFrequencyFilter(0, random.nextBoolean() ? 100 : 0.5d, 0).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(2L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("10"));
            assertThat(bytesValues.lookupOrd(1).utf8ToString(), equalTo("100"));
        }
    }
    {
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("high_freq").fielddata(true).fielddataFrequencyFilter(random.nextBoolean() ? 101 : 101d / 200.0d, 201, 100).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(1L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("5"));
        }
    }
    {
        // test # docs with value
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("med_freq").fielddata(true).fielddataFrequencyFilter(random.nextBoolean() ? 101 : 101d / 200.0d, Integer.MAX_VALUE, 101).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(2L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("10"));
            assertThat(bytesValues.lookupOrd(1).utf8ToString(), equalTo("100"));
        }
    }
    {
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("med_freq").fielddata(true).fielddataFrequencyFilter(random.nextBoolean() ? 101 : 101d / 200.0d, Integer.MAX_VALUE, 101).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(2L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("10"));
            assertThat(bytesValues.lookupOrd(1).utf8ToString(), equalTo("100"));
        }
    }
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) ContentPath(org.elasticsearch.index.mapper.ContentPath) Document(org.apache.lucene.document.Document) Random(java.util.Random) StringField(org.apache.lucene.document.StringField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Aggregations

BuilderContext (org.elasticsearch.index.mapper.Mapper.BuilderContext)7 ContentPath (org.elasticsearch.index.mapper.ContentPath)6 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)4 Document (org.apache.lucene.document.Document)2 StringField (org.apache.lucene.document.StringField)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 Accountable (org.apache.lucene.util.Accountable)2 IndexService (org.elasticsearch.index.IndexService)2 NumberFieldMapper (org.elasticsearch.index.mapper.NumberFieldMapper)2 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)2 ShardId (org.elasticsearch.index.shard.ShardId)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1