Search in sources :

Example 1 with PutMappingRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest in project graylog2-server by Graylog2.

the class IndicesAdapterES7 method updateIndexMapping.

@Override
public void updateIndexMapping(@Nonnull String indexName, @Nonnull String mappingType, @Nonnull Map<String, Object> mapping) {
    final PutMappingRequest request = new PutMappingRequest(indexName).source(mapping);
    client.execute((c, requestOptions) -> c.indices().putMapping(request, requestOptions), "Unable to update index mapping " + indexName);
}
Also used : PutMappingRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest)

Example 2 with PutMappingRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest in project graylog2-server by Graylog2.

the class V20200730000000_AddGl2MessageIdFieldAliasForEventsES7 method addGl2MessageIdFieldAlias.

@Override
public void addGl2MessageIdFieldAlias(Set<String> indexPrefixes) {
    final String[] prefixesWithWildcard = indexPrefixes.stream().map(p -> p + "*").toArray(String[]::new);
    final PutMappingRequest putMappingRequest = new PutMappingRequest(prefixesWithWildcard).indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED).source(ImmutableMap.of("properties", ImmutableMap.of(FIELD_GL2_MESSAGE_ID, aliasMapping())));
    try {
        final AcknowledgedResponse acknowledgedResponse = client.execute((c, requestOptions) -> c.indices().putMapping(putMappingRequest, requestOptions));
        if (!acknowledgedResponse.isAcknowledged()) {
            throw new ElasticsearchException(errorMsgFor(prefixesWithWildcard) + " Elasticsearch failed to acknowledge.");
        }
    } catch (ElasticsearchException e) {
        throw new ElasticsearchException(errorMsgFor(prefixesWithWildcard), e);
    }
}
Also used : Inject(javax.inject.Inject) LinkedHashMap(java.util.LinkedHashMap) V20200730000000_AddGl2MessageIdFieldAliasForEvents(org.graylog.plugins.views.migrations.V20200730000000_AddGl2MessageIdFieldAliasForEvents) PutMappingRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest) ElasticsearchClient(org.graylog.storage.elasticsearch7.ElasticsearchClient) Arrays(java.util.Arrays) ImmutableMap(com.google.common.collect.ImmutableMap) IndicesOptions(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.IndicesOptions) AcknowledgedResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.master.AcknowledgedResponse) Set(java.util.Set) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) FIELD_GL2_MESSAGE_ID(org.graylog2.plugin.Message.FIELD_GL2_MESSAGE_ID) PutMappingRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.master.AcknowledgedResponse) ElasticsearchException(org.graylog2.indexer.ElasticsearchException)

Example 3 with PutMappingRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest in project graylog2-server by Graylog2.

the class V20200730000000_AddGl2MessageIdFieldAliasForEventsES7IT method createIndicesWithIdMapping.

private void createIndicesWithIdMapping(String... indices) {
    for (String index : indices) {
        client().createIndex(index);
    }
    final PutMappingRequest putMappingRequest = new PutMappingRequest(indices).source(idMapping());
    final AcknowledgedResponse acknowledgedResponse = elasticsearch.elasticsearchClient().execute((c, opt) -> c.indices().putMapping(putMappingRequest, opt));
    if (!acknowledgedResponse.isAcknowledged()) {
        throw new RuntimeException("Failed to add 'id' mapping for indices " + Arrays.toString(indices));
    }
}
Also used : PutMappingRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.master.AcknowledgedResponse)

Example 4 with PutMappingRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest in project snow-owl by b2ihealthcare.

the class EsIndexAdmin method create.

@Override
public void create() {
    log.info("Preparing '{}' indexes...", name);
    // register any type that requires a refresh at the end of the index create/open
    Set<DocumentMapping> mappingsToRefresh = Sets.newHashSet();
    // create number of indexes based on number of types
    for (DocumentMapping mapping : mappings.getMappings()) {
        final String index = getTypeIndex(mapping);
        final Map<String, Object> typeMapping = ImmutableMap.<String, Object>builder().put("date_detection", false).put("numeric_detection", false).put("dynamic_templates", List.of(stringsAsKeywords())).putAll(toProperties(mapping)).build();
        if (exists(mapping)) {
            // update mapping if required
            final MappingMetadata currentIndexMapping;
            try {
                final GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices(index);
                currentIndexMapping = client.indices().getMapping(getMappingsRequest).mappings().get(index);
            } catch (Exception e) {
                throw new IndexException(String.format("Failed to get mapping of '%s' for type '%s'", name, mapping.typeAsString()), e);
            }
            try {
                final ObjectNode newTypeMapping = mapper.valueToTree(typeMapping);
                final ObjectNode currentTypeMapping = mapper.valueToTree(currentIndexMapping.getSourceAsMap());
                SortedSet<String> compatibleChanges = Sets.newTreeSet();
                SortedSet<String> incompatibleChanges = Sets.newTreeSet();
                JsonDiff.diff(currentTypeMapping, newTypeMapping).forEach(change -> {
                    if (change.isAdd()) {
                        compatibleChanges.add(change.getFieldPath());
                    } else if (change.isMove() || change.isReplace()) {
                        incompatibleChanges.add(change.getFieldPath());
                    }
                });
                if (!incompatibleChanges.isEmpty()) {
                    log.warn("Cannot migrate index '{}' to new mapping with breaking changes on properties '{}'. Run repository reindex to migrate to new mapping schema or drop that index manually using the Elasticsearch API.", index, incompatibleChanges);
                } else if (!compatibleChanges.isEmpty()) {
                    log.info("Applying mapping changes {} in index {}", compatibleChanges, index);
                    PutMappingRequest putMappingRequest = new PutMappingRequest(index).source(typeMapping);
                    AcknowledgedResponse response = client.indices().updateMapping(putMappingRequest);
                    checkState(response.isAcknowledged(), "Failed to update mapping '%s' for type '%s'", name, mapping.typeAsString());
                    // new fields do not require reindex, they will be added to new documents, existing documents don't have any data that needs reindex
                    if (hasFieldAliasChange(compatibleChanges)) {
                        if (bulkIndexByScroll(client, mapping, Expressions.matchAll(), "update", null, /*no script, in place update of docs to pick up mapping changes*/
                        "mapping migration")) {
                            mappingsToRefresh.add(mapping);
                        }
                        log.info("Migrated documents to new mapping in index '{}'", index);
                    }
                }
            } catch (IOException e) {
                throw new IndexException(String.format("Failed to update mapping '%s' for type '%s'", name, mapping.typeAsString()), e);
            }
        } else {
            // create index
            final Map<String, Object> indexSettings;
            try {
                indexSettings = createIndexSettings();
                log.info("Configuring '{}' index with settings: {}", index, indexSettings);
            } catch (IOException e) {
                throw new IndexException("Couldn't prepare settings for index " + index, e);
            }
            final CreateIndexRequest createIndexRequest = new CreateIndexRequest(index).mapping(typeMapping).settings(indexSettings);
            try {
                final CreateIndexResponse response = client.indices().create(createIndexRequest);
                checkState(response.isAcknowledged(), "Failed to create index '%s' for type '%s'", name, mapping.typeAsString());
            } catch (Exception e) {
                throw new IndexException(String.format("Failed to create index '%s' for type '%s'", name, mapping.typeAsString()), e);
            }
        }
    }
    // wait until the cluster processes each index create request
    waitForYellowHealth(indices());
    if (!mappingsToRefresh.isEmpty()) {
        refresh(mappingsToRefresh);
    }
    log.info("'{}' indexes are ready.", name);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PutMappingRequest(org.elasticsearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) IOException(java.io.IOException) GetMappingsRequest(org.elasticsearch.client.indices.GetMappingsRequest) DocumentMapping(com.b2international.index.mapping.DocumentMapping) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata)

Aggregations

PutMappingRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest)3 AcknowledgedResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.master.AcknowledgedResponse)2 DocumentMapping (com.b2international.index.mapping.DocumentMapping)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 LinkedHashMap (java.util.LinkedHashMap)1 Set (java.util.Set)1 Inject (javax.inject.Inject)1 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)1 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)1 CreateIndexResponse (org.elasticsearch.client.indices.CreateIndexResponse)1 GetMappingsRequest (org.elasticsearch.client.indices.GetMappingsRequest)1 PutMappingRequest (org.elasticsearch.client.indices.PutMappingRequest)1 MappingMetadata (org.elasticsearch.cluster.metadata.MappingMetadata)1 V20200730000000_AddGl2MessageIdFieldAliasForEvents (org.graylog.plugins.views.migrations.V20200730000000_AddGl2MessageIdFieldAliasForEvents)1 IndicesOptions (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.IndicesOptions)1 ElasticsearchClient (org.graylog.storage.elasticsearch7.ElasticsearchClient)1 ElasticsearchException (org.graylog2.indexer.ElasticsearchException)1