Skip to content

Commit

Permalink
removed default index mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatsb committed Sep 25, 2012
1 parent 5bede92 commit 48fabf8
Show file tree
Hide file tree
Showing 28 changed files with 16 additions and 435 deletions.
23 changes: 0 additions & 23 deletions src/main/java/io/searchbox/AbstractAction.java
Expand Up @@ -127,14 +127,10 @@ protected String buildURI(String index, String type, String id) {

if (StringUtils.isNotBlank(index)) {
sb.append(index);
} else {
sb.append("<jesttempindex>");
}

if (StringUtils.isNotBlank(type)) {
sb.append("/").append(type);
} else {
sb.append("/<jesttemptype>");
}

if (StringUtils.isNotBlank(id)) sb.append("/").append(id);
Expand All @@ -143,25 +139,6 @@ protected String buildURI(String index, String type, String id) {

return sb.toString();
}

protected String buildURIWithoutDefaults(String index, String type, String id) {
StringBuilder sb = new StringBuilder();

if (StringUtils.isNotBlank(index)) {
sb.append(index);
}

if (StringUtils.isNotBlank(type)) {
sb.append("/").append(type);
}

if (StringUtils.isNotBlank(id)) sb.append("/").append(id);

log.debug("Created uri: " + sb.toString());

return sb.toString();
}

protected String buildQueryString() {
StringBuilder queryString = new StringBuilder("");
for (Map.Entry<?, ?> entry : parameterMap.entrySet()) {
Expand Down
67 changes: 6 additions & 61 deletions src/main/java/io/searchbox/client/AbstractJestClient.java
Expand Up @@ -25,21 +25,15 @@ public abstract class AbstractJestClient implements JestClient {

public LinkedHashSet<String> servers;

private String defaultIndex;

private String defaultType;

private Boolean useDefaults = true;

private Iterator<String> roundRobinIterator;
private Iterator<String> roundRobinIterator;

public LinkedHashSet<String> getServers() {
return servers;
}

public void setServers(LinkedHashSet<String> servers) {
this.servers = servers;
this.roundRobinIterator = Iterators.cycle(servers);
this.roundRobinIterator = Iterators.cycle(servers);
}

public <T> T executeAsync(Action clientRequest) {
Expand All @@ -50,8 +44,8 @@ public void shutdownClient() {
}

protected String getElasticSearchServer() {
if(roundRobinIterator.hasNext())
return roundRobinIterator.next();
if (roundRobinIterator.hasNext())
return roundRobinIterator.next();
throw new RuntimeException("No Server is assigned to client to connect");
}

Expand Down Expand Up @@ -108,57 +102,8 @@ protected String getRequestURL(String elasticSearchServer, String uri) {
elasticSearchServer.substring(0, elasticSearchServer.length() - 1) : elasticSearchServer;

StringBuilder sb = new StringBuilder(serverUrl);
String modifiedURI = modifyData(uri);

if (useDefaults) {
if (StringUtils.isNotBlank(defaultIndex)) {
sb.append("/").append(defaultIndex);
}
if (StringUtils.isNotBlank(defaultType)) {
sb.append("/").append(defaultType);
}
}

sb.append(modifiedURI.startsWith("/") ? modifiedURI : "/" + modifiedURI);
sb.append(uri.startsWith("/") ? uri : "/" + uri);
return sb.toString();
}

protected String modifyData(Object data) {
String originalDataString = (String) data;
originalDataString = originalDataString.replaceAll("<jesttempindex>", defaultIndex);
originalDataString = originalDataString.replaceAll("<jesttemptype>", defaultType);
return originalDataString;
}

public void removeDefaultIndex() {
defaultIndex = null;
}

public void removeDefaultType() {
defaultType = null;
}

public void registerDefaultIndex(String indexName) {
defaultIndex = indexName;
}

public void registerDefaultType(String typeName) {
defaultType = typeName;
}

public String getDefaultIndex() {
return defaultIndex;
}

public String getDefaultType() {
return defaultType;
}

public Boolean useDefaults() {
return useDefaults;
}

public void useDefaults(Boolean useDefaults) {
this.useDefaults = useDefaults;
}
}
}
9 changes: 0 additions & 9 deletions src/main/java/io/searchbox/client/JestClient.java
Expand Up @@ -18,13 +18,4 @@ public interface JestClient {
<T> T executeAsync(Action clientRequest);

void shutdownClient();

void registerDefaultIndex(String indexName);

void registerDefaultType(String typeName);

void removeDefaultIndex();

void removeDefaultType();

}
6 changes: 0 additions & 6 deletions src/main/java/io/searchbox/client/JestClientFactory.java
Expand Up @@ -38,12 +38,6 @@ public JestClient getObject() {
httpclient = new DefaultHttpClient();
log.debug("Default http client is created without multi threaded option");
}
if (clientConfig.getClientFeature(ClientConstants.DEFAULT_INDEX) != null) {
client.registerDefaultIndex((String) clientConfig.getClientFeature(ClientConstants.DEFAULT_INDEX));
if (clientConfig.getClientFeature(ClientConstants.DEFAULT_TYPE) != null) {
client.registerDefaultType((String) clientConfig.getClientFeature(ClientConstants.DEFAULT_TYPE));
}
}
} else {
log.debug("There is no configuration to create http client. Going to create simple client with default values");
httpclient = new DefaultHttpClient();
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/searchbox/client/config/ClientConstants.java
Expand Up @@ -12,9 +12,4 @@ private ClientConstants() {}
public static final String SERVER_LIST = "serverList";

public static final String IS_MULTI_THREADED = "isMultiThreaded";

public static final String DEFAULT_INDEX = "defaultIndex";

public static final String DEFAULT_TYPE = "defaultType";

}
12 changes: 3 additions & 9 deletions src/main/java/io/searchbox/client/http/JestHttpClient.java
Expand Up @@ -6,7 +6,6 @@
import io.searchbox.client.AbstractJestClient;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
Expand Down Expand Up @@ -39,10 +38,6 @@ public class JestHttpClient extends AbstractJestClient implements JestClient {

public JestResult execute(Action clientRequest) throws IOException {

if (StringUtils.isNotBlank(this.getDefaultIndex())) {
useDefaults(false);
}

String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI());
String methodName = clientRequest.getRestMethodName();
HttpResponse response = null;
Expand Down Expand Up @@ -83,11 +78,10 @@ private String createJsonStringEntity(Object data) {
if (data instanceof byte[]) {
return Unicode.fromBytes((byte[]) data);
} else if (data instanceof String) {
Object modifiedData = modifyData(data);
if (isJson(modifiedData.toString())) {
return modifiedData.toString();
if (isJson(data.toString())) {
return data.toString();
} else {
return new Gson().toJson(modifiedData);
return new Gson().toJson(data);
}
} else {
return new Gson().toJson(data);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/searchbox/core/Bulk.java
Expand Up @@ -14,8 +14,6 @@
* @author Dogukan Sonmez
*/


// TODO Bulk api does not using default index and type yet
public class Bulk extends AbstractAction implements Action {

private final Set<Index> indexSet = new LinkedHashSet<Index>();
Expand All @@ -27,11 +25,11 @@ public Bulk() {
}

public Bulk(String indexName) {
setURI(buildURIWithoutDefaults(indexName, null, null) + "/_bulk");
setURI(buildURI(indexName, null, null) + "/_bulk");
}

public Bulk(String indexName, String typeName) {
setURI(buildURIWithoutDefaults(indexName, typeName, null) + "/_bulk");
setURI(buildURI(indexName, typeName, null) + "/_bulk");
}

public void addIndex(Index index) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/searchbox/indices/CreateIndex.java
Expand Up @@ -19,19 +19,19 @@
public class CreateIndex extends AbstractAction implements Action {

public CreateIndex(String indexName) {
setURI(buildURIWithoutDefaults(indexName, null, null));
setURI(buildURI(indexName, null, null));
setRestMethodName("PUT");
setData(ImmutableSettings.Builder.EMPTY_SETTINGS.getAsMap());
}

public CreateIndex(String indexName, Settings settings) {
setURI(buildURIWithoutDefaults(indexName, null, null));
setURI(buildURI(indexName, null, null));
setData(settings.getAsMap());
setRestMethodName("POST");
}

public CreateIndex(String indexName, String settingsSource) throws FileNotFoundException {
setURI(buildURIWithoutDefaults(indexName, null, null));
setURI(buildURI(indexName, null, null));
setData(readSettingsFromSource(settingsSource).getAsMap());
setRestMethodName("POST");
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/searchbox/indices/DeleteIndex.java
Expand Up @@ -15,14 +15,14 @@ public class DeleteIndex extends AbstractAction implements Action {

public DeleteIndex(String indexName) {
super.indexName = indexName;
setURI(buildURIWithoutDefaults(indexName, null, null));
setURI(buildURI(indexName, null, null));
setRestMethodName("DELETE");
}

public DeleteIndex(String indexName, String type) {
super.indexName = indexName;
super.typeName = type;
setURI(buildURIWithoutDefaults(indexName, type, null));
setURI(buildURI(indexName, type, null));
setRestMethodName("DELETE");
}

Expand Down
31 changes: 0 additions & 31 deletions src/test/java/io/searchbox/client/AbstractJestClientTest.java
Expand Up @@ -223,44 +223,13 @@ public void extractDocumentsFromResponseForSearchRequest() {
"}";
}

@Test
public void getRequestURLWithDefaultIndex() {
String requestURI = "/tweet/1";
String elasticSearchServer = "http://localhost:9200";
client.registerDefaultIndex("twitter");
assertEquals("http://localhost:9200/twitter/tweet/1", client.getRequestURL(elasticSearchServer, requestURI));
}

@Test
public void getRequestURLWithDefaultTypeAndIndex() {
String requestURI = "/1";
String elasticSearchServer = "http://localhost:9200";
client.registerDefaultIndex("twitter");
client.registerDefaultType("tweet");
assertEquals("http://localhost:9200/twitter/tweet/1", client.getRequestURL(elasticSearchServer, requestURI));
}

@Test
public void getRequestURL() {
String requestURI = "twitter/tweet/1";
String elasticSearchServer = "http://localhost:9200";
assertEquals("http://localhost:9200/twitter/tweet/1", client.getRequestURL(elasticSearchServer, requestURI));
}

@Test
public void modifyData() {
client.registerDefaultIndex("twitter");
client.registerDefaultType("tweet");
String data = "{ \"delete\" : { \"_index\" : \"<jesttempindex>\", \"_type\" : \"<jesttemptype>\", \"_id\" : \"1\" } }\n" +
"{ \"delete\" : { \"_index\" : \"<jesttempindex>\", \"_type\" : \"<jesttemptype>\", \"_id\" : \"2\" } }\n" +
"{ \"delete\" : { \"_index\" : \"<jesttempindex>\", \"_type\" : \"<jesttemptype>\", \"_id\" : \"3\" } }\n";
String expected = "{ \"delete\" : { \"_index\" : \"twitter\", \"_type\" : \"tweet\", \"_id\" : \"1\" } }\n" +
"{ \"delete\" : { \"_index\" : \"twitter\", \"_type\" : \"tweet\", \"_id\" : \"2\" } }\n" +
"{ \"delete\" : { \"_index\" : \"twitter\", \"_type\" : \"tweet\", \"_id\" : \"3\" } }\n";
String actual = client.modifyData(data);
assertEquals(expected, actual);
}

@Test
public void testGetElasticSearchServer() throws Exception {
JestHttpClient client = new JestHttpClient();
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/io/searchbox/core/AbstractIntegrationTest.java
Expand Up @@ -26,8 +26,6 @@ public void setUp() throws Exception {
servers.add("http://localhost:9200");
clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers);
clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, true);
clientConfig.getClientFeatures().put(ClientConstants.DEFAULT_INDEX, "cvbank");
clientConfig.getClientFeatures().put(ClientConstants.DEFAULT_TYPE, "candidate");

factory.setClientConfig(clientConfig);

Expand Down
26 changes: 0 additions & 26 deletions src/test/java/io/searchbox/core/DeleteIntegrationTest.java
Expand Up @@ -34,32 +34,6 @@ public void deleteDocument() {
}
}

@Test
@ElasticsearchIndex(indexName = "twitter")
public void deleteDocumentFromDefaultIndex() {
client.registerDefaultIndex("twitter");
try {
executeTestCase(new Delete.Builder("1").type("tweet").build());
log.info("Successfully finished document delete operation");
} catch (Exception e) {
fail("Failed during the delete index with valid parameters. Exception:" + e.getMessage());
}
}

@Test
@ElasticsearchIndex(indexName = "twitter")
public void deleteDocumentFromDefaultIndexAndType() {
client.registerDefaultIndex("twitter");
client.registerDefaultType("tweet");
try {
executeTestCase(new Delete.Builder("1").build());
log.info("Successfully finished document delete operation");
} catch (Exception e) {
fail("Failed during the delete index with valid parameters. Exception:" + e.getMessage());
}
}


@Test
@ElasticsearchIndex(indexName = "cvbank")
public void deleteRealDocument() {
Expand Down
15 changes: 0 additions & 15 deletions src/test/java/io/searchbox/core/DeleteTest.java
Expand Up @@ -18,21 +18,6 @@ public void deleteDocument() {
assertEquals("twitter/tweet/1", delete.getURI());
}

@Test
public void deleteDocumentFromDefaultIndex() {
Delete delete = new Delete.Builder("1").type("tweet").build();
assertEquals("DELETE", delete.getRestMethodName());
assertEquals("<jesttempindex>/tweet/1", delete.getURI());

}

@Test
public void deleteDocumentFromDefaultIndexAndType() {
Delete delete = new Delete.Builder("1").build();
assertEquals("DELETE", delete.getRestMethodName());
assertEquals("<jesttempindex>/<jesttemptype>/1", delete.getURI());
}

@Test
public void deleteDocumentWithVersion() {
Delete delete = new Delete.Builder("1").index("twitter").type("tweet").build();
Expand Down

0 comments on commit 48fabf8

Please sign in to comment.