org.elasticsearch.common.compress.CompressedXContent Java Examples

The following examples show how to use org.elasticsearch.common.compress.CompressedXContent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IndexTemplateMetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
public static IndexTemplateMetaData readFrom(StreamInput in) throws IOException {
    Builder builder = new Builder(in.readString());
    builder.order(in.readInt());
    builder.patterns(in.readList(StreamInput::readString));
    builder.settings(Settings.readSettingsFromStream(in));
    int mappingsSize = in.readVInt();
    for (int i = 0; i < mappingsSize; i++) {
        builder.putMapping(in.readString(), CompressedXContent.readCompressedString(in));
    }
    int aliasesSize = in.readVInt();
    for (int i = 0; i < aliasesSize; i++) {
        AliasMetaData aliasMd = new AliasMetaData(in);
        builder.putAlias(aliasMd);
    }
    builder.version(in.readOptionalVInt());
    return builder.build();
}
 
Example #2
Source File: MappingMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public MappingMetaData(CompressedXContent mapping) throws IOException {
    Map<String, Object> mappingMap;
    try (XContentParser parser = XContentHelper.createParser(mapping.compressedReference())) {
        mappingMap = parser.mapOrdered();
    }
    if (mappingMap.containsKey(MAPPING_VERSION)) {
        this.mappingVersion = (int)mappingMap.get(MAPPING_VERSION);
        mappingMap.remove(MAPPING_VERSION);
    } else {
        this.mappingVersion = 1;
    }
    XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mappingMap);
    this.source = new CompressedXContent(mappingBuilder.bytes());
    if (mappingMap.size() != 1) {
        throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
    }
    this.type = mappingMap.keySet().iterator().next();
    initMappers((Map<String, Object>) mappingMap.get(this.type));
}
 
Example #3
Source File: Templates.java    From crate with Apache License 2.0 6 votes vote down vote up
public static IndexTemplateMetaData.Builder copyWithNewName(IndexTemplateMetaData source, RelationName newName) {
    String targetTemplateName = PartitionName.templateName(newName.schema(), newName.name());
    String targetAlias = newName.indexNameOrAlias();
    IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData
        .builder(targetTemplateName)
        .patterns(Collections.singletonList(PartitionName.templatePrefix(newName.schema(), newName.name())))
        .settings(source.settings())
        .order(source.order())
        .putAlias(AliasMetaData.builder(targetAlias).build())
        .version(source.version());

    for (ObjectObjectCursor<String, CompressedXContent> mapping : source.mappings()) {
        try {
            templateBuilder.putMapping(mapping.key, mapping.value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return templateBuilder;
}
 
Example #4
Source File: ArrayMapperTest.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * create index with type and mapping and validate DocumentMapper serialization
 */
private DocumentMapper mapper(String indexName, String mapping) throws IOException {
    IndicesModule indicesModule = new IndicesModule(Collections.singletonList(new MapperPlugin() {
        @Override
        public Map<String, Mapper.TypeParser> getMappers() {
            return Collections.singletonMap(ArrayMapper.CONTENT_TYPE, new ArrayTypeParser());
        }
    }));
    MapperService mapperService = MapperTestUtils.newMapperService(
        NamedXContentRegistry.EMPTY,
        createTempDir(),
        Settings.EMPTY,
        indicesModule,
        indexName
    );
    DocumentMapperParser parser = mapperService.documentMapperParser();

    DocumentMapper defaultMapper = parser.parse(TYPE, new CompressedXContent(mapping));
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
    builder.startObject();
    defaultMapper.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    String rebuildMapping = Strings.toString(builder);
    return parser.parse(TYPE, new CompressedXContent(rebuildMapping));
}
 
Example #5
Source File: MetaDataIndexUpgraderTest.java    From crate with Apache License 2.0 6 votes vote down vote up
private static CompressedXContent createDynamicStringMappingTemplate() throws IOException {
    // @formatter:off
    XContentBuilder builder = XContentFactory.jsonBuilder()
        .startObject()
        .startObject(Constants.DEFAULT_MAPPING_TYPE)
            .startArray("dynamic_templates")
                .startObject()
                    .startObject("strings")
                        .field("match_mapping_type", "string")
                        .startObject("mapping")
                            .field("type", "keyword")
                            .field("doc_values", true)
                            .field("store", false)
                        .endObject()
                    .endObject()
                .endObject()
            .endArray()
        .endObject()
        .endObject();
    // @formatter:on

    return new CompressedXContent(BytesReference.bytes(builder));
}
 
Example #6
Source File: IndexTemplateMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public IndexTemplateMetaData readFrom(StreamInput in) throws IOException {
    Builder builder = new Builder(in.readString());
    builder.order(in.readInt());
    builder.templateOwnerTenantId(in.readLong());
    builder.template(in.readString());
    
    builder.settings(Settings.readSettingsFromStream(in));
    int mappingsSize = in.readVInt();
    for (int i = 0; i < mappingsSize; i++) {
        builder.putMapping(in.readString(), CompressedXContent.readCompressedString(in));
    }
    int aliasesSize = in.readVInt();
    for (int i = 0; i < aliasesSize; i++) {
        AliasMetaData aliasMd = AliasMetaData.Builder.readFrom(in);
        builder.putAlias(aliasMd);
    }
    int customSize = in.readVInt();
    for (int i = 0; i < customSize; i++) {
        String type = in.readString();
        IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
        builder.putCustom(type, customIndexMetaData);
    }
    return builder.build();
}
 
Example #7
Source File: IndexTemplateUpgraderTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void test__all_is_removed_from_template_mapping() throws Throwable {
    String templateName = PartitionName.templateName("doc", "events");
    var template = IndexTemplateMetaData.builder(templateName)
        .patterns(List.of("*"))
        .putMapping(
            Constants.DEFAULT_MAPPING_TYPE,
            "{" +
            "   \"default\": {" +
            "       \"_all\": {\"enabled\": false}," +
            "       \"properties\": {" +
            "           \"name\": {" +
            "               \"type\": \"keyword\"" +
            "           }" +
            "       }" +
            "   }" +
            "}")
        .build();

    IndexTemplateUpgrader upgrader = new IndexTemplateUpgrader();
    Map<String, IndexTemplateMetaData> result = upgrader.apply(Map.of(templateName, template));
    IndexTemplateMetaData updatedTemplate = result.get(templateName);

    CompressedXContent compressedXContent = updatedTemplate.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(compressedXContent.string(), is("{\"default\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}"));
}
 
Example #8
Source File: TransportSchemaUpdateAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry,
                                   ClusterState currentState,
                                   String templateName,
                                   Map<String, Object> newMapping) throws Exception {
    IndexTemplateMetaData template = currentState.metaData().templates().get(templateName);
    if (template == null) {
        throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
    }

    IndexTemplateMetaData.Builder templateBuilder = new IndexTemplateMetaData.Builder(template);
    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
        Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
        mergeIntoSource(source, newMapping);
        try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
            templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
        }
    }
    MetaData.Builder builder = MetaData.builder(currentState.metaData()).put(templateBuilder);
    return ClusterState.builder(currentState).metaData(builder).build();
}
 
Example #9
Source File: AliasMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AliasMetaData readFrom(StreamInput in) throws IOException {
    String alias = in.readString();
    CompressedXContent filter = null;
    if (in.readBoolean()) {
        filter = CompressedXContent.readCompressedString(in);
    }
    String indexRouting = null;
    if (in.readBoolean()) {
        indexRouting = in.readString();
    }
    String searchRouting = null;
    if (in.readBoolean()) {
        searchRouting = in.readString();
    }
    return new AliasMetaData(alias, filter, indexRouting, searchRouting);
}
 
Example #10
Source File: StandardnumberMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testNonStandardnumber() throws Exception {
    String mapping = copyToStringFromClasspath("mapping.json");
    DocumentMapper docMapper = createIndex("some_index")
            .mapperService().documentMapperParser()
            .parse("someType", new CompressedXContent(mapping));
    String sampleText = "Hello world";
    BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder()
            .startObject().field("someField", sampleText).endObject());
    SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON);
    ParseContext.Document doc = docMapper.parse(sourceToParse).rootDoc();
    assertEquals(0, doc.getFields("someField").length);
    // re-parse it
    String builtMapping = docMapper.mappingSource().string();
    logger.warn("testNonStandardnumber: built mapping =" + builtMapping);
    DocumentMapper docMapper2 = createIndex("some_index2")
            .mapperService().documentMapperParser()
            .parse("someType", new CompressedXContent(builtMapping));
    json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject()
            .field("someField", sampleText).endObject());
    sourceToParse = SourceToParse.source("some_index2", "someType", "1", json, XContentType.JSON);
    doc = docMapper2.parse(sourceToParse).rootDoc();
    assertEquals(0, doc.getFields("someField").length);
}
 
Example #11
Source File: IndexAdmin.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 装载索引数据结构
 *
 * @param index
 * @param type
 * @return
 */
private String loadIndexStruct(String index, String type) {
    ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
    ImmutableOpenMap<String, IndexMetaData> immutableOpenMap = response.getState().getMetaData().getIndices();
    if (immutableOpenMap != null) {
        IndexMetaData metaData = immutableOpenMap.get(index);
        if (metaData != null) {
            ImmutableOpenMap<String, MappingMetaData> mappings = metaData.getMappings();
            if (mappings != null) {
                MappingMetaData mappingMetaData = mappings.get(type);
                if (mappingMetaData != null) {
                    CompressedXContent content = mappingMetaData.source();
                    if (content != null) {
                        return content.toString();
                    }
                }
            }
        }
    }
    LOGGER.error("获取ES数据结构失败 index:" + index + "|type:" + type);
    return null;
}
 
Example #12
Source File: MapperService.java    From crate with Apache License 2.0 6 votes vote down vote up
private synchronized Map<String, DocumentMapper> internalMerge(IndexMetaData indexMetaData, MergeReason reason, boolean updateAllTypes,
                                                               boolean onlyUpdateIfNeeded) {
    Map<String, CompressedXContent> map = new LinkedHashMap<>();
    for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) {
        MappingMetaData mappingMetaData = cursor.value;
        if (onlyUpdateIfNeeded) {
            DocumentMapper existingMapper = documentMapper(mappingMetaData.type());
            if (existingMapper == null || mappingMetaData.source().equals(existingMapper.mappingSource()) == false) {
                map.put(mappingMetaData.type(), mappingMetaData.source());
            }
        } else {
            map.put(mappingMetaData.type(), mappingMetaData.source());
        }
    }
    return internalMerge(map, reason, updateAllTypes);
}
 
Example #13
Source File: MapperService.java    From crate with Apache License 2.0 6 votes vote down vote up
private synchronized Map<String, DocumentMapper> internalMerge(Map<String, CompressedXContent> mappings, MergeReason reason, boolean updateAllTypes) {
    List<DocumentMapper> documentMappers = new ArrayList<>();
    for (Map.Entry<String, CompressedXContent> entry : mappings.entrySet()) {
        String type = entry.getKey();
        if (type.equals(DEFAULT_MAPPING)) {
            continue;
        }


        try {
            DocumentMapper documentMapper = documentParser.parse(type, entry.getValue());
            documentMappers.add(documentMapper);
        } catch (Exception e) {
            throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
        }
    }

    return internalMerge(documentMappers, reason, updateAllTypes);
}
 
Example #14
Source File: AliasMetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
public AliasMetaData(StreamInput in) throws IOException {
    alias = in.readString();
    if (in.readBoolean()) {
        filter = CompressedXContent.readCompressedString(in);
    } else {
        filter = null;
    }
    if (in.readBoolean()) {
        indexRouting = in.readString();
    } else {
        indexRouting = null;
    }
    if (in.readBoolean()) {
        searchRouting = in.readString();
        searchRoutingValues = Collections.unmodifiableSet(Sets.newHashSet(Strings.splitStringByCommaToArray(searchRouting)));
    } else {
        searchRouting = null;
        searchRoutingValues = emptySet();
    }
    writeIndex = in.readOptionalBoolean();
}
 
Example #15
Source File: IndexTemplateMetaData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public IndexTemplateMetaData(String name, int order, String template, Settings settings, ImmutableOpenMap<String, CompressedXContent> mappings,
                             ImmutableOpenMap<String, AliasMetaData> aliases, ImmutableOpenMap<String, IndexMetaData.Custom> customs, long templateOwnerTenantId) {
    this.name = name;
    this.order = order;
    this.template = template;
    this.settings = settings;
    this.mappings = mappings;
    this.aliases = aliases;
    this.customs = customs;
    this.templateOwnerTenantId = templateOwnerTenantId;
}
 
Example #16
Source File: AliasMetaData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static AliasMetaData fromXContent(XContentParser parser) throws IOException {
    Builder builder = new Builder(parser.currentName());

    String currentFieldName = null;
    XContentParser.Token token = parser.nextToken();
    if (token == null) {
        // no data...
        return builder.build();
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("filter".equals(currentFieldName)) {
                Map<String, Object> filter = parser.mapOrdered();
                builder.filter(filter);
            }
        } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
            if ("filter".equals(currentFieldName)) {
                builder.filter(new CompressedXContent(parser.binaryValue()));
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("routing".equals(currentFieldName)) {
                builder.routing(parser.text());
            } else if ("index_routing".equals(currentFieldName) || "indexRouting".equals(currentFieldName)) {
                builder.indexRouting(parser.text());
            } else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName)) {
                builder.searchRouting(parser.text());
            }
        }
    }
    return builder.build();
}
 
Example #17
Source File: StandardnumberMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSimpleStandardNumber() throws Exception {
    String mapping = copyToStringFromClasspath("mapping.json");
    DocumentMapper docMapper = createIndex("some_index")
            .mapperService().documentMapperParser()
            .parse("someType", new CompressedXContent(mapping));
    String sampleText = "978-3-551-75213-0";
    BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject()
            .field("someField", sampleText).endObject());
    SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON);
    ParseContext.Document doc = docMapper.parse(sourceToParse).rootDoc();
    assertEquals(2, doc.getFields("someField").length);
    assertEquals("978-3-551-75213-0", doc.getFields("someField")[0].stringValue());
    assertEquals("9783551752130", doc.getFields("someField")[1].stringValue());
}
 
Example #18
Source File: ColumnPolicyIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictPartitionedTableInsert() throws Exception {
    execute("create table numbers (" +
            "  num int, " +
            "  odd boolean," +
            "  prime boolean" +
            ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();

    GetIndexTemplatesResponse response = client().admin().indices()
        .prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers"))
        .execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap =
        XContentHelper.convertToMap(mappingStr.compressedReference(), false, XContentType.JSON);
    @SuppressWarnings("unchecked")
    Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));

    execute("insert into numbers (num, odd, prime) values (?, ?, ?)",
        new Object[]{6, true, false});
    execute("refresh table numbers");

    Map<String, Object> sourceMap = getSourceMap(
        new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));

    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)",
        new Object[]{28, true, false, true});
}
 
Example #19
Source File: DocumentMapperParser.java    From crate with Apache License 2.0 5 votes vote down vote up
public DocumentMapper parse(@Nullable String type, CompressedXContent source) throws MapperParsingException {
    Map<String, Object> mapping = null;
    if (source != null) {
        Map<String, Object> root = XContentHelper.convertToMap(source.compressedReference(), true, XContentType.JSON).v2();
        Tuple<String, Map<String, Object>> t = extractMapping(type, root);
        type = t.v1();
        mapping = t.v2();
    }
    if (mapping == null) {
        mapping = new HashMap<>();
    }
    return parse(type, mapping);
}
 
Example #20
Source File: KeywordFieldMapperTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void test_keyword_field_parser_on_mapping_with_length_limit() throws Exception {
    String expectedMapping = Strings.toString(
        XContentFactory.jsonBuilder()
            .startObject()
                .startObject(TYPE)
                    .startObject("properties")
                        .startObject("text_col")
                            .field("type", "keyword")
                            .field("index", false)
                            .field("length_limit", 1)
                        .endObject()
                    .endObject()
                .endObject()
            .endObject());

    var parser = parser(new KeywordFieldMapper.TypeParser());
    // string -> doXContentBody -> parse -> string
    var compressedXContent = new CompressedXContent(expectedMapping);
    var mapper = parser.parse(TYPE, compressedXContent);
    var actualMapping = mapper.mappingSource().toString();

    assertThat(expectedMapping, is(actualMapping));
    assertThat(actualMapping, is(
        "{\"default\":{\"properties\":" +
        "{\"text_col\":{\"type\":\"keyword\",\"index\":false,\"length_limit\":1}}}}"));
}
 
Example #21
Source File: MapperService.java    From crate with Apache License 2.0 5 votes vote down vote up
public void merge(Map<String, Map<String, Object>> mappings, MergeReason reason, boolean updateAllTypes) {
    Map<String, CompressedXContent> mappingSourcesCompressed = new LinkedHashMap<>(mappings.size());
    for (Map.Entry<String, Map<String, Object>> entry : mappings.entrySet()) {
        try {
            mappingSourcesCompressed.put(entry.getKey(), new CompressedXContent(Strings.toString(XContentFactory.jsonBuilder().map(entry.getValue()))));
        } catch (Exception e) {
            throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
        }
    }

    internalMerge(mappingSourcesCompressed, reason, updateAllTypes);
}
 
Example #22
Source File: DocumentMapperParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public DocumentMapper parse(@Nullable String type, CompressedXContent source, String defaultSource) throws MapperParsingException {
    Map<String, Object> mapping = null;
    if (source != null) {
        Map<String, Object> root = XContentHelper.convertToMap(source.compressedReference(), true).v2();
        Tuple<String, Map<String, Object>> t = extractMapping(type, root);
        type = t.v1();
        mapping = t.v2();
    }
    if (mapping == null) {
        mapping = Maps.newHashMap();
    }
    return parse(type, mapping, defaultSource);
}
 
Example #23
Source File: ColumnPolicyIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictPartitionedTableUpdate() throws Exception {
    execute("create table numbers (" +
            "  num int, " +
            "  odd boolean," +
            "  prime boolean" +
            ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();

    GetIndexTemplatesResponse response = client().admin().indices()
        .prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers"))
        .execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked")
    Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));

    execute("insert into numbers (num, odd, prime) values (?, ?, ?)",
        new Object[]{6, true, false});
    execute("refresh table numbers");

    Map<String, Object> sourceMap = getSourceMap(
        new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));

    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("update numbers set num=?, perfect=? where num=6",
        new Object[]{28, true});
}
 
Example #24
Source File: MapperService.java    From crate with Apache License 2.0 5 votes vote down vote up
private boolean assertSerialization(DocumentMapper mapper) {
    // capture the source now, it may change due to concurrent parsing
    final CompressedXContent mappingSource = mapper.mappingSource();
    DocumentMapper newMapper = parse(mapper.type(), mappingSource);

    if (newMapper.mappingSource().equals(mappingSource) == false) {
        throw new IllegalStateException("DocumentMapper serialization result is different from source. \n--> Source ["
            + mappingSource + "]\n--> Result ["
            + newMapper.mappingSource() + "]");
    }
    return true;
}
 
Example #25
Source File: IndicesClusterStateService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private boolean processMapping(String index, MapperService mapperService, String mappingType, CompressedXContent mappingSource, long mappingVersion) throws Throwable {
    // refresh mapping can happen when the parsing/merging of the mapping from the metadata doesn't result in the same
    // mapping, in this case, we send to the master to refresh its own version of the mappings (to conform with the
    // merge version of it, which it does when refreshing the mappings), and warn log it.
    boolean requiresRefresh = false;
    try {
        DocumentMapper existingMapper = mapperService.documentMapper(mappingType);

        if (existingMapper == null || existingMapper.mappingVersion() != mappingVersion || mappingSource.equals(existingMapper.mappingSource()) == false) {
            String op = existingMapper == null ? "adding" : "updating";
            if (logger.isDebugEnabled() && mappingSource.compressed().length < 512) {
                logger.debug("[{}] {} mapping [{}], source [{}]", index, op, mappingType, mappingSource.string());
            } else if (logger.isTraceEnabled()) {
                logger.trace("[{}] {} mapping [{}], source [{}]", index, op, mappingType, mappingSource.string());
            } else {
                logger.debug("[{}] {} mapping [{}] (source suppressed due to length, use TRACE level if needed)", index, op, mappingType);
            }
            mapperService.merge(mappingType, mappingSource, MapperService.MergeReason.MAPPING_RECOVERY, true, true, mappingVersion);
            if (!mapperService.documentMapper(mappingType).mappingSource().equals(mappingSource)) {
                logger.debug("[{}] parsed mapping [{}], and got different sources\noriginal:\n{}\nparsed:\n{}", index, mappingType, mappingSource, mapperService.documentMapper(mappingType).mappingSource());
                requiresRefresh = true;
            }
        }
    } catch (Throwable e) {
        logger.warn("[{}] failed to add mapping [{}], source [{}]", e, index, mappingType, mappingSource);
        throw e;
    }
    return requiresRefresh;
}
 
Example #26
Source File: MappingMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
public MappingMetaData(CompressedXContent mapping) throws IOException {
    this.source = mapping;
    Map<String, Object> mappingMap = XContentHelper.convertToMap(mapping.compressedReference(), true).v2();
    if (mappingMap.size() != 1) {
        throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
    }
    this.type = mappingMap.keySet().iterator().next();
    initMappers((Map<String, Object>) mappingMap.get(this.type));
}
 
Example #27
Source File: MappingMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
public MappingMetaData(String type, Map<String, Object> mapping) throws IOException {
    this.type = type;
    this.source = new CompressedXContent(
        (builder, params) -> builder.mapContents(mapping), XContentType.JSON, ToXContent.EMPTY_PARAMS);
    Map<String, Object> withoutType = mapping;
    if (mapping.size() == 1 && mapping.containsKey(type)) {
        withoutType = (Map<String, Object>) mapping.get(type);
    }
    initMappers(withoutType);
}
 
Example #28
Source File: AliasMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
private AliasMetaData(String alias, CompressedXContent filter, String indexRouting, String searchRouting, Boolean writeIndex) {
    this.alias = alias;
    this.filter = filter;
    this.indexRouting = indexRouting;
    this.searchRouting = searchRouting;
    if (searchRouting != null) {
        searchRoutingValues = Collections.unmodifiableSet(Sets.newHashSet(Strings.splitStringByCommaToArray(searchRouting)));
    } else {
        searchRoutingValues = emptySet();
    }
    this.writeIndex = writeIndex;
}
 
Example #29
Source File: IndexTemplateMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
public IndexTemplateMetaData(String name, int order, Integer version,
                             List<String> patterns, Settings settings,
                             ImmutableOpenMap<String, CompressedXContent> mappings,
                             ImmutableOpenMap<String, AliasMetaData> aliases) {
    if (patterns == null || patterns.isEmpty()) {
        throw new IllegalArgumentException("Index patterns must not be null or empty; got " + patterns);
    }
    this.name = name;
    this.order = order;
    this.version = version;
    this.patterns = patterns;
    this.settings = settings;
    this.mappings = mappings;
    this.aliases = aliases;
}
 
Example #30
Source File: MappingMetaData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private MappingMetaData() {
    this.type = "";
    this.mappingVersion = 1;
    try {
        this.source = new CompressedXContent("{}");
    } catch (IOException ex) {
        throw new IllegalStateException("Cannot create MappingMetaData prototype", ex);
    }
}