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

Settings api #155

Merged
merged 3 commits into from
Oct 21, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
package io.searchbox.indices.settings;

import io.searchbox.action.AbstractMultiIndexActionBuilder;
import io.searchbox.action.GenericResultAbstractAction;

/**
* The get settings API allows to retrieve settings of index/indices.
*
* @author Dogukan Sonmez
* @author cihat keser
*/
public class GetSettings extends IndicesSettingsAbstractAction {

private GetSettings(Builder builder) {
super(builder);
}

@Override
public String getRestMethodName() {
return "GET";
}

public static class Builder extends AbstractMultiIndexActionBuilder<GetSettings, Builder> {

@Override
public GetSettings build() {
return new GetSettings(this);
}

/**
* Prefix Query Option allows to include only settings (whose keys) matches the specified prefix.
*/
public Builder prefixQuery(String prefixQuery) {
return setParameter("prefix", prefixQuery);
}

}

public class GetSettings {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.searchbox.indices.settings;

import io.searchbox.action.GenericResultAbstractAction;

/**
* @author cihat keser
*/
public abstract class IndicesSettingsAbstractAction extends GenericResultAbstractAction {

protected IndicesSettingsAbstractAction(Builder builder) {
super(builder);
setURI(buildURI());
}

@Override
protected String buildURI() {
StringBuilder sb = new StringBuilder();
sb.append(super.buildURI()).append("/_settings");
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
package io.searchbox.indices.settings;

import com.google.gson.Gson;
import io.searchbox.action.AbstractMultiIndexActionBuilder;

/**
* Change specific index level settings in real time.
*
* @author Dogukan Sonmez
* @author cihat keser
*/
public class UpdateSettings extends IndicesSettingsAbstractAction {

private Object source;

private UpdateSettings(Builder builder) {
super(builder);
this.source = builder.source;
}

@Override
public String getRestMethodName() {
return "PUT";
}

@Override
public Object getData(Gson gson) {
return source;
}

public static class Builder extends AbstractMultiIndexActionBuilder<UpdateSettings, Builder> {
private final Object source;

/**
* Please see the <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html#indices-update-settings">related page on Elasticsearch guide</a>
* for the list of settings that can be changed using this action/API.
*
* @param source body of request that includes updated settings
*/
public Builder(Object source) {
this.source = source;
}

@Override
public UpdateSettings build() {
return new UpdateSettings(this);
}
}

public class UpdateSettings {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.searchbox.indices.settings;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class GetSettingsTest {

@Test
public void testDefaultUriGeneration() {
String expectedUri = "_all/_settings";

GetSettings getSettings = new GetSettings.Builder().build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testDefaultUriGenerationWithPrefix() {
String expectedUri = "_all/_settings?prefix=index.routing.allocation.";

GetSettings getSettings = new GetSettings.Builder().prefixQuery("index.routing.allocation.").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testDefaultUriGenerationWithEmptyPrefix() {
String expectedUri = "_all/_settings?prefix=";

GetSettings getSettings = new GetSettings.Builder().prefixQuery("").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testSingleIndexUriGeneration() {
String expectedUri = "books/_settings";

GetSettings getSettings = new GetSettings.Builder().addIndex("books").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testSingleIndexUriGenerationWithPrefix() {
String expectedUri = "books/_settings?prefix=index.routing.allocation.";

GetSettings getSettings = new GetSettings.Builder().addIndex("books").prefixQuery("index.routing.allocation.").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testMultipleIndicesUriGeneration() {
String expectedUri = "books%2Carticles/_settings";

GetSettings getSettings = new GetSettings.Builder().addIndex("books").addIndex("articles").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testMultipleIndicesUriGenerationWithPrefix() {
String expectedUri = "books%2Carticles/_settings?prefix=index.routing.allocation.";

GetSettings getSettings = new GetSettings.Builder()
.addIndex("books").addIndex("articles").prefixQuery("index.routing.allocation.").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testWildcardUriGeneration() {
String expectedUri = "2013-*/_settings";

GetSettings getSettings = new GetSettings.Builder().addIndex("2013-*").build();
assertEquals(expectedUri, getSettings.getURI());
}

@Test
public void testWildcardUriGenerationWithPrefix() {
String expectedUri = "2013-*/_settings?prefix=index.routing.allocation.";

GetSettings getSettings = new GetSettings.Builder().addIndex("2013-*").prefixQuery("index.routing.allocation.").build();
assertEquals(expectedUri, getSettings.getURI());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.searchbox.indices.settings;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class UpdateSettingsTest {

@Test
public void testDefaultBehaviour() {
String expectedUri = "_all/_settings";

UpdateSettings updateSettings = new UpdateSettings.Builder("").build();
assertEquals(expectedUri, updateSettings.getURI());
assertEquals("", updateSettings.getData(null));
assertEquals("PUT", updateSettings.getRestMethodName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.searchbox.client.http.apache.HttpDeleteWithEntity;
import io.searchbox.client.http.apache.HttpGetWithEntity;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
Expand Down Expand Up @@ -158,10 +159,9 @@ protected HttpUriRequest constructHttpMethod(String methodName, String url, Obje
}

protected String createJsonStringEntity(Object data) {

String entity;

if (data instanceof String && isJson(data.toString())) {
if (data instanceof String && (StringUtils.isEmpty(data.toString()) || isJson(data.toString()))) {
entity = data.toString();
} else {
entity = gson.toJson(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.searchbox.indices.settings;

import com.google.gson.JsonObject;
import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;

import java.io.IOException;

/**
* @author cihat keser
*/
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numDataNodes = 1)
public class GetSettingsIntegrationTest extends AbstractIntegrationTest {

@Test
public void testBasicFlow() throws IOException {
String index = "test";

createIndex(index);
ensureGreen(index);

GetSettings getSettings = new GetSettings.Builder().build();
JestResult result = client.execute(getSettings);
assertNotNull(result);

assertTrue(result.isSucceeded());
System.out.println("result.getJsonString() = " + result.getJsonString());
JsonObject json = result.getJsonObject();
assertNotNull(json.getAsJsonObject(index));
assertNotNull(json.getAsJsonObject(index).getAsJsonObject("settings"));
}

@Test
public void testForNonexistentIndex() throws IOException {
String index = "test";

createIndex(index);
ensureGreen(index);

GetSettings getSettings = new GetSettings.Builder().addIndex("nonExisting").build();
JestResult result = client.execute(getSettings);
assertNotNull(result);

assertFalse(result.isSucceeded());
}

}
Loading