Search in sources :

Example 1 with Mapper

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

the class TransportGetFieldMappingsIndexAction method findFieldMappingsByType.

private static Map<String, FieldMappingMetadata> findFieldMappingsByType(Predicate<String> fieldPredicate, DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) {
    Map<String, FieldMappingMetadata> fieldMappings = new HashMap<>();
    final MappingLookup allFieldMappers = documentMapper.mappers();
    for (String field : request.fields()) {
        if (Regex.isMatchAllPattern(field)) {
            for (Mapper fieldMapper : allFieldMappers) {
                addFieldMapper(fieldPredicate, fieldMapper.name(), fieldMapper, fieldMappings, request.includeDefaults());
            }
        } else if (Regex.isSimpleMatchPattern(field)) {
            for (Mapper fieldMapper : allFieldMappers) {
                if (Regex.simpleMatch(field, fieldMapper.name())) {
                    addFieldMapper(fieldPredicate, fieldMapper.name(), fieldMapper, fieldMappings, request.includeDefaults());
                }
            }
        } else {
            // not a pattern
            Mapper fieldMapper = allFieldMappers.getMapper(field);
            if (fieldMapper != null) {
                addFieldMapper(fieldPredicate, field, fieldMapper, fieldMappings, request.includeDefaults());
            } else if (request.probablySingleFieldRequest()) {
                fieldMappings.put(field, FieldMappingMetadata.NULL);
            }
        }
    }
    return Collections.unmodifiableMap(fieldMappings);
}
Also used : Mapper(org.opensearch.index.mapper.Mapper) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) HashMap(java.util.HashMap) MappingLookup(org.opensearch.index.mapper.MappingLookup) FieldMappingMetadata(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata)

Example 2 with Mapper

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

the class CategoryContextMappingTests method testIndexingWithContextList.

public void testIndexingWithContextList() throws Exception {
    String mapping = Strings.toString(jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "category").endObject().endArray().endObject().endObject().endObject().endObject());
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    Mapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    ParsedDocument parsedDocument = defaultMapper.parse(new SourceToParse("test", "type1", "1", BytesReference.bytes(jsonBuilder().startObject().startObject("completion").array("input", "suggestion5", "suggestion6", "suggestion7").startObject("contexts").array("ctx", "ctx1", "ctx2", "ctx3").endObject().field("weight", 5).endObject().endObject()), XContentType.JSON));
    IndexableField[] fields = parsedDocument.rootDoc().getFields(fieldMapper.name());
    assertContextSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with Mapper

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

the class CategoryContextMappingTests method testIndexingWithSimpleContexts.

public void testIndexingWithSimpleContexts() throws Exception {
    String mapping = Strings.toString(jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "category").endObject().endArray().endObject().endObject().endObject().endObject());
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    Mapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    ParsedDocument parsedDocument = defaultMapper.parse(new SourceToParse("test", "type1", "1", BytesReference.bytes(jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion5", "suggestion6", "suggestion7").startObject("contexts").field("ctx", "ctx1").endObject().field("weight", 5).endObject().endArray().endObject()), XContentType.JSON));
    IndexableField[] fields = parsedDocument.rootDoc().getFields(fieldMapper.name());
    assertContextSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with Mapper

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

the class AggregatorTestCase method testSupportedFieldTypes.

/**
 * This test will validate that an aggregator succeeds or fails to run against all the field types
 * that are registered in {@link IndicesModule} (e.g. all the core field types).  An aggregator
 * is provided by the implementor class, and it is executed against each field type in turn.  If
 * an exception is thrown when the field is supported, that will fail the test.  Similarly, if
 * an exception _is not_ thrown when a field is unsupported, that will also fail the test.
 *
 * Exception types/messages are not currently checked, just presence/absence of an exception.
 */
public void testSupportedFieldTypes() throws IOException {
    MapperRegistry mapperRegistry = new IndicesModule(Collections.emptyList()).getMapperRegistry();
    Settings settings = Settings.builder().put("index.version.created", Version.CURRENT.id).build();
    String fieldName = "typeTestFieldName";
    List<ValuesSourceType> supportedVSTypes = getSupportedValuesSourceTypes();
    List<String> unsupportedMappedFieldTypes = unsupportedMappedFieldTypes();
    if (supportedVSTypes.isEmpty()) {
        // If the test says it doesn't support any VStypes, it has not been converted yet so skip
        return;
    }
    for (Map.Entry<String, Mapper.TypeParser> mappedType : mapperRegistry.getMapperParsers().entrySet()) {
        // Some field types should not be tested, or require more work and are not ready yet
        if (TYPE_TEST_DENYLIST.contains(mappedType.getKey())) {
            continue;
        }
        Map<String, Object> source = new HashMap<>();
        source.put("type", mappedType.getKey());
        // Text is the only field that doesn't support DVs, instead FD
        if (mappedType.getKey().equals(TextFieldMapper.CONTENT_TYPE) == false) {
            source.put("doc_values", "true");
        }
        Mapper.Builder builder = mappedType.getValue().parse(fieldName, source, new MockParserContext());
        FieldMapper mapper = (FieldMapper) builder.build(new BuilderContext(settings, new ContentPath()));
        MappedFieldType fieldType = mapper.fieldType();
        // Non-aggregatable fields are not testable (they will throw an error on all aggs anyway), so skip
        if (fieldType.isAggregatable() == false) {
            continue;
        }
        try (Directory directory = newDirectory()) {
            RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
            writeTestDoc(fieldType, fieldName, indexWriter);
            indexWriter.close();
            try (IndexReader indexReader = DirectoryReader.open(directory)) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                AggregationBuilder aggregationBuilder = createAggBuilderForTypeTest(fieldType, fieldName);
                ValuesSourceType vst = fieldToVST(fieldType);
                // TODO in the future we can make this more explicit with expectThrows(), when the exceptions are standardized
                AssertionError failure = null;
                try {
                    searchAndReduce(indexSearcher, new MatchAllDocsQuery(), aggregationBuilder, fieldType);
                    if (supportedVSTypes.contains(vst) == false || unsupportedMappedFieldTypes.contains(fieldType.typeName())) {
                        failure = new AssertionError("Aggregator [" + aggregationBuilder.getType() + "] should not support field type [" + fieldType.typeName() + "] but executing against the field did not throw an exception");
                    }
                } catch (Exception | AssertionError e) {
                    if (supportedVSTypes.contains(vst) && unsupportedMappedFieldTypes.contains(fieldType.typeName()) == false) {
                        failure = new AssertionError("Aggregator [" + aggregationBuilder.getType() + "] supports field type [" + fieldType.typeName() + "] but executing against the field threw an exception: [" + e.getMessage() + "]", e);
                    }
                }
                if (failure != null) {
                    throw failure;
                }
            }
        }
    }
}
Also used : AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher) ContextIndexSearcher(org.opensearch.search.internal.ContextIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndicesModule(org.opensearch.indices.IndicesModule) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) HashMap(java.util.HashMap) Mockito.anyString(org.mockito.Mockito.anyString) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) FieldAliasMapper(org.opensearch.index.mapper.FieldAliasMapper) GeoPointFieldMapper(org.opensearch.index.mapper.GeoPointFieldMapper) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) CompletionFieldMapper(org.opensearch.index.mapper.CompletionFieldMapper) ObjectMapper(org.opensearch.index.mapper.ObjectMapper) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) FieldMapper(org.opensearch.index.mapper.FieldMapper) Mapper(org.opensearch.index.mapper.Mapper) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) Directory(org.apache.lucene.store.Directory) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) ContentPath(org.opensearch.index.mapper.ContentPath) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IOException(java.io.IOException) MapperRegistry(org.opensearch.indices.mapper.MapperRegistry) IndexReader(org.apache.lucene.index.IndexReader) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) GeoPointFieldMapper(org.opensearch.index.mapper.GeoPointFieldMapper) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) CompletionFieldMapper(org.opensearch.index.mapper.CompletionFieldMapper) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) FieldMapper(org.opensearch.index.mapper.FieldMapper) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 5 with Mapper

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

the class TransportGetFieldMappingsIndexAction method shardOperation.

@Override
protected GetFieldMappingsResponse shardOperation(final GetFieldMappingsIndexRequest request, ShardId shardId) {
    assert shardId != null;
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    Version indexCreatedVersion = indexService.mapperService().getIndexSettings().getIndexVersionCreated();
    Predicate<String> metadataFieldPredicate = (f) -> indicesService.isMetadataField(indexCreatedVersion, f);
    Predicate<String> fieldPredicate = metadataFieldPredicate.or(indicesService.getFieldFilter().apply(shardId.getIndexName()));
    DocumentMapper mapper = indexService.mapperService().documentMapper();
    Collection<String> typeIntersection;
    if (request.types().length == 0) {
        typeIntersection = mapper == null ? Collections.emptySet() : Collections.singleton(mapper.type());
    } else {
        typeIntersection = mapper != null && Regex.simpleMatch(request.types(), mapper.type()) ? Collections.singleton(mapper.type()) : Collections.emptySet();
        if (typeIntersection.isEmpty()) {
            throw new TypeMissingException(shardId.getIndex(), request.types());
        }
    }
    Map<String, Map<String, FieldMappingMetadata>> typeMappings = new HashMap<>();
    for (String type : typeIntersection) {
        DocumentMapper documentMapper = indexService.mapperService().documentMapper(type);
        Map<String, FieldMappingMetadata> fieldMapping = findFieldMappingsByType(fieldPredicate, documentMapper, request);
        if (!fieldMapping.isEmpty()) {
            typeMappings.put(type, fieldMapping);
        }
    }
    return new GetFieldMappingsResponse(singletonMap(shardId.getIndexName(), Collections.unmodifiableMap(typeMappings)));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) ThreadPool(org.opensearch.threadpool.ThreadPool) Version(org.opensearch.Version) HashMap(java.util.HashMap) FieldMappingMetadata(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata) OpenSearchException(org.opensearch.OpenSearchException) Writeable(org.opensearch.common.io.stream.Writeable) Regex(org.opensearch.common.regex.Regex) ClusterState(org.opensearch.cluster.ClusterState) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) Collections.singletonMap(java.util.Collections.singletonMap) Predicate(java.util.function.Predicate) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) Collection(java.util.Collection) IndicesService(org.opensearch.indices.IndicesService) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) Mapper(org.opensearch.index.mapper.Mapper) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) IndexService(org.opensearch.index.IndexService) TransportService(org.opensearch.transport.TransportService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) XContentHelper(org.opensearch.common.xcontent.XContentHelper) MappingLookup(org.opensearch.index.mapper.MappingLookup) ShardId(org.opensearch.index.shard.ShardId) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) XContentType(org.opensearch.common.xcontent.XContentType) TypeMissingException(org.opensearch.indices.TypeMissingException) Collections(java.util.Collections) TransportSingleShardAction(org.opensearch.action.support.single.shard.TransportSingleShardAction) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexService(org.opensearch.index.IndexService) HashMap(java.util.HashMap) TypeMissingException(org.opensearch.indices.TypeMissingException) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) Version(org.opensearch.Version) FieldMappingMetadata(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

Mapper (org.opensearch.index.mapper.Mapper)11 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)10 IndexableField (org.apache.lucene.index.IndexableField)8 KeywordFieldMapper (org.opensearch.index.mapper.KeywordFieldMapper)8 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)8 SourceToParse (org.opensearch.index.mapper.SourceToParse)8 TextFieldMapper (org.opensearch.index.mapper.TextFieldMapper)8 Matchers.containsString (org.hamcrest.Matchers.containsString)7 CompressedXContent (org.opensearch.common.compress.CompressedXContent)7 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Map (java.util.Map)3 Collections (java.util.Collections)2 Collections.singletonMap (java.util.Collections.singletonMap)2 OpenSearchException (org.opensearch.OpenSearchException)2 FieldMappingMetadata (org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata)2 BytesReference (org.opensearch.common.bytes.BytesReference)2 XContentHelper (org.opensearch.common.xcontent.XContentHelper)2 XContentType (org.opensearch.common.xcontent.XContentType)2 Collection (java.util.Collection)1