Search in sources :

Example 1 with DocumentFieldMappers

use of org.elasticsearch.index.mapper.DocumentFieldMappers in project elasticsearch by elastic.

the class DocumentMapperMergeTests method testConcurrentMergeTest.

public void testConcurrentMergeTest() throws Throwable {
    final MapperService mapperService = createIndex("test").mapperService();
    mapperService.merge("test", new CompressedXContent("{\"test\":{}}"), MapperService.MergeReason.MAPPING_UPDATE, false);
    final DocumentMapper documentMapper = mapperService.documentMapper("test");
    DocumentFieldMappers dfm = documentMapper.mappers();
    try {
        assertNotNull(dfm.indexAnalyzer().tokenStream("non_existing_field", "foo"));
        fail();
    } catch (IllegalArgumentException e) {
    // ok that's expected
    }
    final AtomicBoolean stopped = new AtomicBoolean(false);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final AtomicReference<String> lastIntroducedFieldName = new AtomicReference<>();
    final AtomicReference<Exception> error = new AtomicReference<>();
    final Thread updater = new Thread() {

        @Override
        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < 200 && stopped.get() == false; i++) {
                    final String fieldName = Integer.toString(i);
                    ParsedDocument doc = documentMapper.parse("test", "test", fieldName, new BytesArray("{ \"" + fieldName + "\" : \"test\" }"));
                    Mapping update = doc.dynamicMappingsUpdate();
                    assert update != null;
                    lastIntroducedFieldName.set(fieldName);
                    mapperService.merge("test", new CompressedXContent(update.toString()), MapperService.MergeReason.MAPPING_UPDATE, false);
                }
            } catch (Exception e) {
                error.set(e);
            } finally {
                stopped.set(true);
            }
        }
    };
    updater.start();
    try {
        barrier.await();
        while (stopped.get() == false) {
            final String fieldName = lastIntroducedFieldName.get();
            final BytesReference source = new BytesArray("{ \"" + fieldName + "\" : \"test\" }");
            ParsedDocument parsedDoc = documentMapper.parse("test", "test", "random", source);
            if (parsedDoc.dynamicMappingsUpdate() != null) {
                // not in the mapping yet, try again
                continue;
            }
            dfm = documentMapper.mappers();
            assertNotNull(dfm.indexAnalyzer().tokenStream(fieldName, "foo"));
        }
    } finally {
        stopped.set(true);
        updater.join();
    }
    if (error.get() != null) {
        throw error.get();
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) DocumentFieldMappers(org.elasticsearch.index.mapper.DocumentFieldMappers) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) AtomicReference(java.util.concurrent.atomic.AtomicReference) Mapping(org.elasticsearch.index.mapper.Mapping) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 2 with DocumentFieldMappers

use of org.elasticsearch.index.mapper.DocumentFieldMappers in project elasticsearch by elastic.

the class DynamicTemplatesTests method testMatchTypeOnly.

public void testMatchTypeOnly() throws Exception {
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject().startObject("person").startArray("dynamic_templates").startObject().startObject("test").field("match_mapping_type", "string").startObject("mapping").field("index", false).endObject().endObject().endObject().endArray().endObject().endObject();
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(builder).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    builder = JsonXContent.contentBuilder();
    builder.startObject().field("s", "hello").field("l", 1).endObject();
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", builder.bytes());
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    DocumentFieldMappers mappers = docMapper.mappers();
    assertThat(mappers.smartNameFieldMapper("s"), Matchers.notNullValue());
    assertEquals(IndexOptions.NONE, mappers.smartNameFieldMapper("s").fieldType().indexOptions());
    assertThat(mappers.smartNameFieldMapper("l"), Matchers.notNullValue());
    assertNotSame(IndexOptions.NONE, mappers.smartNameFieldMapper("l").fieldType().indexOptions());
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentFieldMappers(org.elasticsearch.index.mapper.DocumentFieldMappers) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 3 with DocumentFieldMappers

use of org.elasticsearch.index.mapper.DocumentFieldMappers in project elasticsearch by elastic.

the class TransportGetFieldMappingsIndexAction method findFieldMappingsByType.

private Map<String, FieldMappingMetaData> findFieldMappingsByType(DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) {
    MapBuilder<String, FieldMappingMetaData> fieldMappings = new MapBuilder<>();
    final DocumentFieldMappers allFieldMappers = documentMapper.mappers();
    for (String field : request.fields()) {
        if (Regex.isMatchAllPattern(field)) {
            for (FieldMapper fieldMapper : allFieldMappers) {
                addFieldMapper(fieldMapper.fieldType().name(), fieldMapper, fieldMappings, request.includeDefaults());
            }
        } else if (Regex.isSimpleMatchPattern(field)) {
            for (FieldMapper fieldMapper : allFieldMappers) {
                if (Regex.simpleMatch(field, fieldMapper.fieldType().name())) {
                    addFieldMapper(fieldMapper.fieldType().name(), fieldMapper, fieldMappings, request.includeDefaults());
                }
            }
        } else {
            // not a pattern
            FieldMapper fieldMapper = allFieldMappers.smartNameFieldMapper(field);
            if (fieldMapper != null) {
                addFieldMapper(field, fieldMapper, fieldMappings, request.includeDefaults());
            } else if (request.probablySingleFieldRequest()) {
                fieldMappings.put(field, FieldMappingMetaData.NULL);
            }
        }
    }
    return fieldMappings.immutableMap();
}
Also used : FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) DocumentFieldMappers(org.elasticsearch.index.mapper.DocumentFieldMappers) MapBuilder(org.elasticsearch.common.collect.MapBuilder) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Aggregations

DocumentFieldMappers (org.elasticsearch.index.mapper.DocumentFieldMappers)3 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)2 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)2 IOException (java.io.IOException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FieldMappingMetaData (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 MapBuilder (org.elasticsearch.common.collect.MapBuilder)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexService (org.elasticsearch.index.IndexService)1 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)1 MapperService (org.elasticsearch.index.mapper.MapperService)1 Mapping (org.elasticsearch.index.mapper.Mapping)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1