Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge ES index and mapping creation in Indices#create() #1563

Merged
merged 1 commit into from Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,21 +18,13 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.Client;
import org.graylog2.indexer.ranges.IndexRange;
import org.graylog2.plugin.Tools;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Representing the message type mapping in ElasticSearch. This is giving ES more
* information about what the fields look like and how it should analyze them.
Expand All @@ -41,24 +33,6 @@
public class IndexMapping {
public static final String TYPE_MESSAGE = "message";

private final Client client;

@Inject
public IndexMapping(Client client) {
this.client = checkNotNull(client);
}

public ActionFuture<PutMappingResponse> createMapping(final String index, final String type, final Map<String, Object> mapping) {
return client.admin().indices().putMapping(mappingRequest(index, type, mapping));
}

private PutMappingRequest mappingRequest(final String index, final String type, final Map<String, Object> mapping) {
return client.admin().indices().preparePutMapping(index)
.setType(type)
.setSource(ImmutableMap.of(type, mapping))
.request();
}

public Map<String, Object> messageMapping(final String analyzer) {
return ImmutableMap.of(
"properties", partFieldProperties(analyzer),
Expand Down
Expand Up @@ -37,7 +37,6 @@
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.optimize.OptimizeRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
Expand Down Expand Up @@ -220,17 +219,14 @@ public boolean create(String indexName) {
"number_of_shards", configuration.getShards(),
"number_of_replicas", configuration.getReplicas(),
"index.analysis.analyzer.analyzer_keyword", keywordLowercase);

final CreateIndexRequest cir = c.admin().indices().prepareCreate(indexName).setSettings(settings).request();
if (!c.admin().indices().create(cir).actionGet().isAcknowledged()) {
return false;
}

final Map<String, Object> messageMapping = indexMapping.messageMapping(configuration.getAnalyzer());
final PutMappingResponse messageMappingResponse =
indexMapping.createMapping(indexName, IndexMapping.TYPE_MESSAGE, messageMapping).actionGet();

return messageMappingResponse.isAcknowledged();
final CreateIndexRequest cir = c.admin().indices().prepareCreate(indexName)
.setSettings(settings)
.addMapping(IndexMapping.TYPE_MESSAGE, messageMapping)
.request();

return c.admin().indices().create(cir).actionGet().isAcknowledged();
}

public Set<String> getAllMessageFields() {
Expand Down
Expand Up @@ -78,7 +78,7 @@ public IndicesTest() {

@Before
public void setUp() throws Exception {
indices = new Indices(client, CONFIG, new IndexMapping(client));
indices = new Indices(client, CONFIG, new IndexMapping());
}

@Test
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void insert(InputStream dataScript) {
client.admin().indices().prepareDelete(index).execute().actionGet();
}

Indices indices = new Indices(client, new ElasticsearchConfiguration(), new IndexMapping(client));
Indices indices = new Indices(client, new ElasticsearchConfiguration(), new IndexMapping());
if (!indices.create(index)) {
throw new IllegalStateException("Couldn't create index " + index);
}
Expand Down
Expand Up @@ -85,7 +85,7 @@ public EsIndexRangeServiceTest() {

@Before
public void setUp() throws Exception {
indices = new Indices(client, ELASTICSEARCH_CONFIGURATION, new IndexMapping(client));
indices = new Indices(client, ELASTICSEARCH_CONFIGURATION, new IndexMapping());
final Deflector deflector = new Deflector(null, ELASTICSEARCH_CONFIGURATION, new NullActivityWriter(), null, null, indices);
indexRangeService = new EsIndexRangeService(client, deflector, indices, localEventBus, clusterEventBus, new MetricRegistry());
}
Expand Down