Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
CreateIndex action now accepts any Object as settings source (also im…
Browse files Browse the repository at this point in the history
…proved test cases for this action).

Fixes #40 and #118.
  • Loading branch information
Cihat Keser committed Jul 12, 2014
1 parent 5e7a02c commit 1b27a0c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.google.gson.JsonObject;
import io.searchbox.action.GenericResultAbstractAction;

import java.util.HashMap;
import java.util.Map;

/**
* @author Dogukan Sonmez
* @author cihat keser
Expand All @@ -20,7 +17,7 @@ public CreateIndex(Builder builder) {
super(builder);

this.indexName = builder.index;
if (builder.settings.size() > 0) {
if (builder.settings != null) {
this.settings = builder.settings;
} else {
this.settings = new JsonObject();
Expand All @@ -40,14 +37,14 @@ public String getRestMethodName() {
}

public static class Builder extends GenericResultAbstractAction.Builder<CreateIndex, Builder> {
private Map<String, String> settings = new HashMap<String, String>();
private Object settings = null;
private String index;

public Builder(String index) {
this.index = index;
}

public Builder settings(Map<String, String> settings) {
public Builder settings(Object settings) {
this.settings = settings;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

/**
* @author Dogukan Sonmez
Expand All @@ -17,28 +23,67 @@ public class CreateIndexIntegrationTest extends AbstractIntegrationTest {
@Test
public void createIndexWithDefaultSettings() throws IOException {
CreateIndex createIndex = new CreateIndex.Builder("newindex").build();
executeTestCase(createIndex);

JestResult result = client.execute(createIndex);
assertNotNull(result);
assertTrue(result.isSucceeded());
}

@Test
public void createIndexWithSettings() {
public void createIndexWithMapSettings() throws IOException {
String index = "anothernewindex";
final ImmutableSettings.Builder indexerSettings = ImmutableSettings.settingsBuilder();
indexerSettings.put("analysis.analyzer.events.type", "custom");
indexerSettings.put("analysis.analyzer.events.tokenizer", "standard");
indexerSettings.put("analysis.analyzer.events.filter", "snowball, standard, lowercase");
CreateIndex createIndex = new CreateIndex.Builder("anothernewindex")
.settings(indexerSettings.build().getAsMap())
Map<String, String> expectedSettingsMap = indexerSettings.build().getAsMap();
CreateIndex createIndex = new CreateIndex.Builder(index)
.settings(expectedSettingsMap)
.build();
try {
executeTestCase(createIndex);
} catch (IOException e) {
fail("Test failed while executing creating index with default settings");

JestResult result = client.execute(createIndex);
assertNotNull(result);
assertTrue(result.isSucceeded());

GetSettingsResponse settingsResponse =
client().admin().indices().getSettings(new GetSettingsRequest().indices(index)).actionGet();
assertNotNull(settingsResponse);
Settings actualSettingsMap = settingsResponse.getIndexToSettings().get(index);
for (Map.Entry<String, String> entry : expectedSettingsMap.entrySet()) {
String key = "index." + entry.getKey();
assertEquals(entry.getValue(), actualSettingsMap.get(key));
}
}

private void executeTestCase(CreateIndex createIndex) throws RuntimeException, IOException {
@Test
public void createIndexWithStringSettingsAndMapping() throws IOException {
String index = "stringyone";
String expectedType1Maping =
"{\"type1\":{\"_source\":{\"enabled\":false},\"properties\":{\"field1\":{\"type\":\"string\",\"index\":\"not_analyzed\"}}}}";
String settingsJson = "{\n" +
" \"settings\" : {\n" +
" \"number_of_shards\" : 8\n" +
" },\n" +
" \"mappings\" : " + expectedType1Maping +
"}";
CreateIndex createIndex = new CreateIndex.Builder(index)
.settings(settingsJson)
.build();

JestResult result = client.execute(createIndex);
assertNotNull(result);
assertTrue(result.isSucceeded());

GetSettingsResponse settingsResponse =
client().admin().indices().getSettings(new GetSettingsRequest().indices(index)).actionGet();
assertNotNull(settingsResponse);
assertEquals("8", settingsResponse.getSetting(index, "index.number_of_shards"));

GetMappingsResponse mappingsResponse =
client().admin().indices().getMappings(new GetMappingsRequest().indices(index)).actionGet();
assertNotNull(mappingsResponse);
String actualType1Mapping = mappingsResponse.getMappings().get(index).get("type1").source().string();
assertEquals(expectedType1Maping, actualType1Mapping);
}

}

0 comments on commit 1b27a0c

Please sign in to comment.