Skip to content

Commit

Permalink
Introducing a method to support index mapping update operations (#11212)
Browse files Browse the repository at this point in the history
[graylog-plugin-enterprise/issue-2626]
  • Loading branch information
devdanylo authored Aug 27, 2021
1 parent b238ac2 commit e3e5f20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import io.searchbox.indices.aliases.GetAliases;
import io.searchbox.indices.aliases.ModifyAliases;
import io.searchbox.indices.aliases.RemoveAliasMapping;
import io.searchbox.indices.mapping.PutMapping;
import io.searchbox.indices.settings.GetSettings;
import io.searchbox.indices.settings.UpdateSettings;
import io.searchbox.indices.template.DeleteTemplate;
Expand Down Expand Up @@ -85,6 +86,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -216,6 +218,25 @@ public void create(String indexName, IndexSettings indexSettings, String templat
}
}

@Override
public void updateIndexMapping(@Nonnull String indexName,
@Nonnull String mappingType,
@Nonnull Map<String, Object> mapping) {

final PutMapping request = new PutMapping.Builder(indexName, mappingType, mapping).build();

final JestResult jestResult;
try {
jestResult = jestClient.execute(request);
} catch (IOException e) {
throw new ElasticsearchException("Couldn't update index mapping " + indexName + "/" + mappingType, e);
}

if (!jestResult.isSucceeded()){
throw new ElasticsearchException(jestResult.getErrorMessage());
}
}

@Override
public boolean ensureIndexTemplate(String templateName, Map<String, Object> template) {
final PutTemplate request = new PutTemplate.Builder(uncheckedURLEncode(templateName), template).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.DeleteAliasRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutMappingRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.cluster.metadata.AliasMetadata;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilders;
Expand Down Expand Up @@ -72,6 +73,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -152,6 +154,18 @@ public void create(String index, IndexSettings indexSettings, String templateNam
"Unable to create index " + index);
}

@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);
}

@Override
public boolean ensureIndexTemplate(String templateName, Map<String, Object> template) {
final PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.graylog2.indexer.searches.IndexRangeStats;
import org.joda.time.DateTime;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
Expand All @@ -39,6 +40,14 @@ public interface IndicesAdapter {

void create(String indexName, IndexSettings indexSettings, String templateName, Map<String, Object> template);

/**
* Add fields to an existing index or to change search only settings of existing fields
* @param indexName existing index name
* @param mappingType target mapping type (e.g. message). Not relevant for ES7+ (will be simply ignored).
* @param mapping field mappings
*/
void updateIndexMapping(@Nonnull String indexName, @Nonnull String mappingType, @Nonnull Map<String, Object> mapping);

boolean ensureIndexTemplate(String templateName, Map<String, Object> template);

Optional<DateTime> indexCreationDate(String index);
Expand Down

0 comments on commit e3e5f20

Please sign in to comment.