Search in sources :

Example 6 with BuilderContext

use of org.opensearch.index.mapper.Mapper.BuilderContext in project OpenSearch by opensearch-project.

the class IndexFieldDataServiceTests method testGetForFieldDefaults.

public void testGetForFieldDefaults() {
    final IndexService indexService = createIndex("test");
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    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 stringMapper = new KeywordFieldMapper.Builder("string").build(ctx).fieldType();
    ifdService.clear();
    IndexFieldData<?> fd = ifdService.getForField(stringMapper, "test", () -> {
        throw new UnsupportedOperationException();
    });
    assertTrue(fd instanceof SortedSetOrdinalsIndexFieldData);
    for (MappedFieldType mapper : Arrays.asList(new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.BYTE, false, true).build(ctx).fieldType(), new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.SHORT, false, true).build(ctx).fieldType(), new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.INTEGER, false, true).build(ctx).fieldType(), new NumberFieldMapper.Builder("long", NumberFieldMapper.NumberType.LONG, false, true).build(ctx).fieldType())) {
        ifdService.clear();
        fd = ifdService.getForField(mapper, "test", () -> {
            throw new UnsupportedOperationException();
        });
        assertTrue(fd instanceof SortedNumericIndexFieldData);
    }
    final MappedFieldType floatMapper = new NumberFieldMapper.Builder("float", NumberFieldMapper.NumberType.FLOAT, false, true).build(ctx).fieldType();
    ifdService.clear();
    fd = ifdService.getForField(floatMapper, "test", () -> {
        throw new UnsupportedOperationException();
    });
    assertTrue(fd instanceof SortedNumericIndexFieldData);
    final MappedFieldType doubleMapper = new NumberFieldMapper.Builder("double", NumberFieldMapper.NumberType.DOUBLE, false, true).build(ctx).fieldType();
    ifdService.clear();
    fd = ifdService.getForField(doubleMapper, "test", () -> {
        throw new UnsupportedOperationException();
    });
    assertTrue(fd instanceof SortedNumericIndexFieldData);
}
Also used : NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) IndexService(org.opensearch.index.IndexService) IndicesService(org.opensearch.indices.IndicesService) SortedSetOrdinalsIndexFieldData(org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData) ContentPath(org.opensearch.index.mapper.ContentPath) SortedNumericIndexFieldData(org.opensearch.index.fielddata.plain.SortedNumericIndexFieldData) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext)

Example 7 with BuilderContext

use of org.opensearch.index.mapper.Mapper.BuilderContext in project OpenSearch by opensearch-project.

the class MapperService method unmappedFieldType.

/**
 * Given a type (eg. long, string, ...), return an anonymous field mapper that can be used for search operations.
 */
public MappedFieldType unmappedFieldType(String type) {
    if (type.equals("string")) {
        deprecationLogger.deprecate("unmapped_type_string", "[unmapped_type:string] should be replaced with [unmapped_type:keyword]");
        type = "keyword";
    }
    MappedFieldType fieldType = unmappedFieldTypes.get(type);
    if (fieldType == null) {
        final Mapper.TypeParser.ParserContext parserContext = documentMapperParser().parserContext();
        Mapper.TypeParser typeParser = parserContext.typeParser(type);
        if (typeParser == null) {
            throw new IllegalArgumentException("No mapper found for type [" + type + "]");
        }
        final Mapper.Builder<?> builder = typeParser.parse("__anonymous_" + type, emptyMap(), parserContext);
        final BuilderContext builderContext = new BuilderContext(indexSettings.getSettings(), new ContentPath(1));
        fieldType = ((FieldMapper) builder.build(builderContext)).fieldType();
        // There is no need to synchronize writes here. In the case of concurrent access, we could just
        // compute some mappers several times, which is not a big deal
        Map<String, MappedFieldType> newUnmappedFieldTypes = new HashMap<>(unmappedFieldTypes);
        newUnmappedFieldTypes.put(type, fieldType);
        unmappedFieldTypes = unmodifiableMap(newUnmappedFieldTypes);
    }
    return fieldType;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext)

Example 8 with BuilderContext

use of org.opensearch.index.mapper.Mapper.BuilderContext in project OpenSearch by opensearch-project.

the class AbstractSortTestCase method createMockShardContext.

protected final QueryShardContext createMockShardContext(IndexSearcher searcher) {
    Index index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_");
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build());
    BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, Mockito.mock(BitsetFilterCache.Listener.class));
    TriFunction<MappedFieldType, String, Supplier<SearchLookup>, IndexFieldData<?>> indexFieldDataLookup = (fieldType, fieldIndexName, searchLookup) -> {
        IndexFieldData.Builder builder = fieldType.fielddataBuilder(fieldIndexName, searchLookup);
        return builder.build(new IndexFieldDataCache.None(), null);
    };
    return new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, bitsetFilterCache, indexFieldDataLookup, null, null, scriptService, xContentRegistry(), namedWriteableRegistry, null, searcher, () -> randomNonNegativeLong(), null, null, () -> true, null) {

        @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()).build(context);
        }
    };
}
Also used : ScriptModule(org.opensearch.script.ScriptModule) ToXContent(org.opensearch.common.xcontent.ToXContent) ScriptEngine(org.opensearch.script.ScriptEngine) ContentPath(org.opensearch.index.mapper.ContentPath) Version(org.opensearch.Version) XContentParser(org.opensearch.common.xcontent.XContentParser) IndexFieldDataCache(org.opensearch.index.fielddata.IndexFieldDataCache) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) SortField(org.apache.lucene.search.SortField) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) AfterClass(org.junit.AfterClass) Index(org.opensearch.index.Index) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) SearchLookup(org.opensearch.search.lookup.SearchLookup) QueryBuilder(org.opensearch.index.query.QueryBuilder) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) XContentType(org.opensearch.common.xcontent.XContentType) BigArrays(org.opensearch.common.util.BigArrays) IndexSearcher(org.apache.lucene.search.IndexSearcher) TriFunction(org.opensearch.common.TriFunction) IndexSettingsModule(org.opensearch.test.IndexSettingsModule) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DocValueFormat(org.opensearch.search.DocValueFormat) ObjectMapper(org.opensearch.index.mapper.ObjectMapper) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Nested(org.opensearch.index.mapper.ObjectMapper.Nested) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) Environment(org.opensearch.env.Environment) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) EqualsHashCodeTestUtils.checkEqualsAndHashCode(org.opensearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode) Rewriteable(org.opensearch.index.query.Rewriteable) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) IOException(java.io.IOException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Mockito(org.mockito.Mockito) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Collections(java.util.Collections) SearchModule(org.opensearch.search.SearchModule) IndexSettings(org.opensearch.index.IndexSettings) QueryBuilder(org.opensearch.index.query.QueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Index(org.opensearch.index.Index) ContentPath(org.opensearch.index.mapper.ContentPath) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) QueryShardContext(org.opensearch.index.query.QueryShardContext) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) Supplier(java.util.function.Supplier) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache)

Example 9 with BuilderContext

use of org.opensearch.index.mapper.Mapper.BuilderContext in project OpenSearch by opensearch-project.

the class AggregatorTestCase method createSearchContext.

protected SearchContext createSearchContext(IndexSearcher indexSearcher, IndexSettings indexSettings, Query query, MultiBucketConsumer bucketConsumer, CircuitBreakerService circuitBreakerService, MappedFieldType... fieldTypes) throws IOException {
    QueryCache queryCache = new DisabledQueryCache(indexSettings);
    QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {

        @Override
        public void onUse(Query query) {
        }

        @Override
        public boolean shouldCache(Query query) {
            // never cache a query
            return false;
        }
    };
    ContextIndexSearcher contextIndexSearcher = new ContextIndexSearcher(indexSearcher.getIndexReader(), indexSearcher.getSimilarity(), queryCache, queryCachingPolicy, false);
    SearchContext searchContext = mock(SearchContext.class);
    when(searchContext.numberOfShards()).thenReturn(1);
    when(searchContext.searcher()).thenReturn(contextIndexSearcher);
    when(searchContext.fetchPhase()).thenReturn(new FetchPhase(Arrays.asList(new FetchSourcePhase(), new FetchDocValuesPhase())));
    when(searchContext.bitsetFilterCache()).thenReturn(new BitsetFilterCache(indexSettings, mock(Listener.class)));
    IndexShard indexShard = mock(IndexShard.class);
    when(indexShard.shardId()).thenReturn(new ShardId("test", "test", 0));
    when(searchContext.indexShard()).thenReturn(indexShard);
    when(searchContext.aggregations()).thenReturn(new SearchContextAggregations(AggregatorFactories.EMPTY, bucketConsumer));
    when(searchContext.query()).thenReturn(query);
    /*
         * Always use the circuit breaking big arrays instance so that the CircuitBreakerService
         * we're passed gets a chance to break.
         */
    BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), circuitBreakerService).withCircuitBreaking();
    when(searchContext.bigArrays()).thenReturn(bigArrays);
    // TODO: now just needed for top_hits, this will need to be revised for other agg unit tests:
    MapperService mapperService = mapperServiceMock();
    when(mapperService.getIndexSettings()).thenReturn(indexSettings);
    when(mapperService.hasNested()).thenReturn(false);
    when(searchContext.mapperService()).thenReturn(mapperService);
    IndexFieldDataService ifds = new IndexFieldDataService(indexSettings, new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() {
    }), circuitBreakerService, mapperService);
    QueryShardContext queryShardContext = queryShardContextMock(contextIndexSearcher, mapperService, indexSettings, circuitBreakerService, bigArrays);
    when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
    when(queryShardContext.getObjectMapper(anyString())).thenAnswer(invocation -> {
        String fieldName = (String) invocation.getArguments()[0];
        if (fieldName.startsWith(NESTEDFIELD_PREFIX)) {
            BuilderContext context = new BuilderContext(indexSettings.getSettings(), new ContentPath());
            return new ObjectMapper.Builder<>(fieldName).nested(Nested.newNested()).build(context);
        }
        return null;
    });
    Map<String, MappedFieldType> fieldNameToType = new HashMap<>();
    fieldNameToType.putAll(Arrays.stream(fieldTypes).filter(Objects::nonNull).collect(Collectors.toMap(MappedFieldType::name, Function.identity())));
    fieldNameToType.putAll(getFieldAliases(fieldTypes));
    registerFieldTypes(searchContext, mapperService, fieldNameToType);
    doAnswer(invocation -> {
        /* Store the release-ables so we can release them at the end of the test case. This is important because aggregations don't
             * close their sub-aggregations. This is fairly similar to what the production code does. */
        releasables.add((Releasable) invocation.getArguments()[0]);
        return null;
    }).when(searchContext).addReleasable(any());
    return searchContext;
}
Also used : QueryCachingPolicy(org.apache.lucene.search.QueryCachingPolicy) QueryCache(org.apache.lucene.search.QueryCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) Listener(org.opensearch.index.cache.bitset.BitsetFilterCache.Listener) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Builder(org.opensearch.search.aggregations.AggregatorFactories.Builder) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) ContextIndexSearcher(org.opensearch.search.internal.ContextIndexSearcher) SearchContext(org.opensearch.search.internal.SearchContext) MockBigArrays(org.opensearch.common.util.MockBigArrays) Mockito.anyString(org.mockito.Mockito.anyString) ShardId(org.opensearch.index.shard.ShardId) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexShard(org.opensearch.index.shard.IndexShard) FetchSourcePhase(org.opensearch.search.fetch.subphase.FetchSourcePhase) FetchDocValuesPhase(org.opensearch.search.fetch.subphase.FetchDocValuesPhase) ContentPath(org.opensearch.index.mapper.ContentPath) IndicesFieldDataCache(org.opensearch.indices.fielddata.cache.IndicesFieldDataCache) MockBigArrays(org.opensearch.common.util.MockBigArrays) BigArrays(org.opensearch.common.util.BigArrays) IndexFieldDataService(org.opensearch.index.fielddata.IndexFieldDataService) FetchPhase(org.opensearch.search.fetch.FetchPhase) Objects(java.util.Objects) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) MapperService(org.opensearch.index.mapper.MapperService)

Example 10 with BuilderContext

use of org.opensearch.index.mapper.Mapper.BuilderContext in project OpenSearch by opensearch-project.

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", createDefaultIndexAnalyzers()).fielddata(true).build(ctx).fieldType();
    final IndexWriter writer = new IndexWriter(new ByteBuffersDirectory(), 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 ? OpenSearchDirectoryReader.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, "test", () -> {
        throw new UnsupportedOperationException();
    });
    LeafReaderContext leafReaderContext = reader.getContext().leaves().get(0);
    LeafFieldData 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.opensearch.index.IndexService) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.apache.lucene.document.Document) ShardId(org.opensearch.index.shard.ShardId) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) OpenSearchDirectoryReader(org.opensearch.common.lucene.index.OpenSearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Accountable(org.apache.lucene.util.Accountable) IndicesService(org.opensearch.indices.IndicesService) ContentPath(org.opensearch.index.mapper.ContentPath) 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.opensearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

BuilderContext (org.opensearch.index.mapper.Mapper.BuilderContext)10 ContentPath (org.opensearch.index.mapper.ContentPath)9 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)8 HashMap (java.util.HashMap)3 Document (org.apache.lucene.document.Document)3 StringField (org.apache.lucene.document.StringField)3 IndexReader (org.apache.lucene.index.IndexReader)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 NumberFieldMapper (org.opensearch.index.mapper.NumberFieldMapper)3 TextFieldMapper (org.opensearch.index.mapper.TextFieldMapper)3 IOException (java.io.IOException)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 ByteBuffersDirectory (org.apache.lucene.store.ByteBuffersDirectory)2 Accountable (org.apache.lucene.util.Accountable)2