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

Commit

Permalink
Modified Action interface method getData to take a Gson instance as p…
Browse files Browse the repository at this point in the history
…arameter, which will be provided by JestClient (which is provided to client through ClientConfig).
  • Loading branch information
Cihat Keser committed Oct 16, 2013
1 parent 41eb3c2 commit 7f7dedc
Show file tree
Hide file tree
Showing 33 changed files with 263 additions and 165 deletions.
15 changes: 6 additions & 9 deletions src/main/java/io/searchbox/AbstractAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public abstract class AbstractAction implements Action {
protected String indexName;
protected String typeName;
protected String nodes;
private Object data;
private String URI;
private boolean isBulkOperation;
private String pathToResult;
Expand Down Expand Up @@ -87,10 +86,12 @@ public Object getHeader(String header) {
return headerMap.get(header);
}

@Override
public Map<String, Object> getHeaders() {
return headerMap;
}

@Override
public String getURI() {
String finalUri = URI;
if (parameterMap.size() > 0) {
Expand All @@ -109,14 +110,12 @@ protected void setURI(String URI) {
this.URI = URI;
}

public Object getData() {
return data;
}

protected void setData(Object data) {
this.data = data;
@Override
public Object getData(Gson gson) {
return null;
}

@Override
public String getPathToResult() {
return pathToResult;
}
Expand Down Expand Up @@ -199,7 +198,6 @@ public int hashCode() {
.append(getURI())
.append(getRestMethodName())
.append(getHeaders())
.append(getData())
.toHashCode();
}

Expand All @@ -216,7 +214,6 @@ public boolean equals(Object obj) {
.append(getURI(), rhs.getURI())
.append(getRestMethodName(), rhs.getRestMethodName())
.append(getHeaders(), rhs.getHeaders())
.append(getData(), rhs.getData())
.isEquals();
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/searchbox/Action.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.searchbox;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.Map;
Expand All @@ -13,7 +14,7 @@ public interface Action {

String getRestMethodName();

Object getData();
Object getData(Gson gson);

String getPathToResult();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/searchbox/client/http/JestHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public JestResult execute(Action clientRequest) throws IOException {

String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI());

HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData());
HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData(gson));

// add headers added to action
if (!clientRequest.getHeaders().isEmpty()) {
Expand Down Expand Up @@ -82,7 +82,7 @@ public void executeAsync(final Action clientRequest, final JestResultHandler<Jes

String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI());

final HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData());
final HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData(gson));

// add headers added to action
if (!clientRequest.getHeaders().isEmpty()) {
Expand Down
58 changes: 16 additions & 42 deletions src/main/java/io/searchbox/core/Bulk.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,39 @@
public class Bulk extends AbstractAction {

final static Logger log = LoggerFactory.getLogger(AbstractAction.class);
private Gson gson;
protected Collection<BulkableAction> bulkableActions;

public Bulk(Builder builder) {
super(builder);
gson = builder.gson;
indexName = builder.defaultIndex;
typeName = builder.defaultType;
bulkableActions = builder.actions;

setData(generateBulkPayload(builder.actions));
setURI(buildURI());
}

public Gson getGson() {
return gson;
private Object getJson(Gson gson, Object source) {
if (source instanceof String) {
return source;
} else {
return gson.toJson(source);
}
}

/**
* Make sure to always provide a non-pretty-printing Gson instance!
* This restriction is due to the way Elasticsearch's Bulk REST API uses line separators.
*
* @param gson
*/
public void setGson(Gson gson) {
this.gson = gson;
@Override
public String getRestMethodName() {
return "POST";
}

protected Object generateBulkPayload(List<BulkableAction> actions) {
@Override
public Object getData(Gson gson) {
/*
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
*/
StringBuilder sb = new StringBuilder();
for (BulkableAction action : actions) {
for (BulkableAction action : bulkableActions) {
// write out the action-meta-data line
// e.g.: { "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
Map<String, Map<String, String>> opMap = new HashMap<String, Map<String, String>>(1);
Expand Down Expand Up @@ -98,29 +97,16 @@ protected Object generateBulkPayload(List<BulkableAction> actions) {

// write out the action source/document line
// e.g.: { "field1" : "value1" }
Object source = action.getData();
Object source = action.getData(gson);
if (source != null) {
sb.append(getJson(source));
sb.append(getJson(gson, source));
sb.append("\n");
}

}
return sb.toString();
}

private Object getJson(Object source) {
if (source instanceof String) {
return source;
} else {
return gson.toJson(source);
}
}

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

@Override
public String getPathToResult() {
return "ok";
Expand All @@ -137,18 +123,6 @@ public static class Builder extends AbstractAction.Builder<Bulk, Builder> {
private List<BulkableAction> actions = new LinkedList<BulkableAction>();
private String defaultIndex;
private String defaultType;
private Gson gson = new Gson();

/**
* Make sure to always provide a non-pretty-printing Gson instance!
* This restriction is due to the way Elasticsearch's Bulk REST API uses line separators.
*
* @param gson
*/
public Builder gson(Gson gson) {
this.gson = gson;
return this;
}

public Builder defaultIndex(String defaultIndex) {
this.defaultIndex = defaultIndex;
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/searchbox/core/Count.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.searchbox.core;

import com.google.gson.Gson;
import io.searchbox.AbstractAction;
import io.searchbox.AbstractMultiTypeActionBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Dogukan Sonmez
* @author cihat keser
*/
public class Count extends AbstractAction {

final static Logger log = LoggerFactory.getLogger(Count.class);
private String query;

public Count(Builder builder) {
super(builder);
setData(builder.query);

this.query = builder.query;
setURI(buildURI());
}

Expand All @@ -36,6 +36,11 @@ public String getRestMethodName() {
return "POST";
}

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

public static class Builder extends AbstractMultiTypeActionBuilder<Count, Builder> {
private String query;

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/io/searchbox/core/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
import com.google.gson.JsonObject;
import io.searchbox.AbstractDocumentTargetedAction;
import io.searchbox.BulkableAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Dogukan Sonmez
* @author cihat keser
*/
public class Delete extends AbstractDocumentTargetedAction implements BulkableAction {

final static Logger log = LoggerFactory.getLogger(Delete.class);

private Delete(Builder builder) {
super(builder);
setURI(buildURI());
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/io/searchbox/core/DeleteByQuery.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package io.searchbox.core;

import com.google.gson.Gson;
import io.searchbox.AbstractAction;
import io.searchbox.AbstractMultiTypeActionBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashSet;

/**
* @author Dogukan Sonmez
* @author cihat keser
*/
public class DeleteByQuery extends AbstractAction {

final static Logger log = LoggerFactory.getLogger(DeleteByQuery.class);
private String query;

public DeleteByQuery(Builder builder) {
super(builder);
setData(builder.query);

this.query = builder.query;
setURI(buildURI());
}

Expand All @@ -29,17 +30,6 @@ public String buildURI() {
return sb.toString();
}

protected String createQueryString(LinkedHashSet<String> set) {
StringBuilder sb = new StringBuilder();
String tmp = "";
for (String index : set) {
sb.append(tmp);
sb.append(index);
tmp = ",";
}
return sb.toString();
}

@Override
public String getPathToResult() {
return "ok";
Expand All @@ -50,6 +40,11 @@ public String getRestMethodName() {
return "DELETE";
}

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

public static class Builder extends AbstractMultiTypeActionBuilder<DeleteByQuery, Builder> {

private String query;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/io/searchbox/core/Explain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.searchbox.core;

import com.google.gson.Gson;
import io.searchbox.AbstractDocumentTargetedAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -11,18 +12,24 @@
public class Explain extends AbstractDocumentTargetedAction {

final static Logger log = LoggerFactory.getLogger(Explain.class);
private Object query;

private Explain(Builder builder) {
super(builder);
setURI(buildURI());
setData(builder.query);
this.query = builder.query;
}

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

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

@Override
protected String buildURI() {
StringBuilder sb = new StringBuilder(super.buildURI());
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/searchbox/core/Index.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.searchbox.core;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.searchbox.AbstractDocumentTargetedAction;
import io.searchbox.BulkableAction;
import io.searchbox.params.Parameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

Expand All @@ -15,11 +14,12 @@
*/
public class Index extends AbstractDocumentTargetedAction implements BulkableAction {

final static Logger log = LoggerFactory.getLogger(Index.class);
private Object source;

private Index(Builder builder) {
super(builder);
setData(builder.source);

this.source = builder.source;
setURI(buildURI());
}

Expand All @@ -38,6 +38,11 @@ public String getRestMethodName() {
return (id != null) ? "PUT" : "POST";
}

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

@Override
public String getBulkMethodName() {
Collection<Object> opType = getParameter(Parameters.OP_TYPE);
Expand Down
Loading

0 comments on commit 7f7dedc

Please sign in to comment.