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

Commit

Permalink
Resolves #60 - This issue was caused by the remove-all-whitespace dir…
Browse files Browse the repository at this point in the history
…ective in getJson method of Bulk action class. Previously I added the said directive to prevent broken Bulk api calls, because if the source string in an added bulkable-action contained line breaks it would break the elasticsearch bulk api format and the bulk operation would fail. I have now removed that directive and provided a warning in the class' JavaDoc accordingly.
  • Loading branch information
Cihat Keser committed Aug 1, 2013
1 parent 1874903 commit fd5e1ff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/main/java/io/searchbox/core/Bulk.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
import java.util.*;

/**
* The bulk API makes it possible to perform many index/delete operations in a
* single API call. This can greatly increase the indexing speed.
* <br/>
* <br/>
* Make sure that your source data (provided in Action instances) <b> does NOT
* have unescaped line-breaks</b> (e.g.: <code>&quot;\n&quot;</code> or <code>&quot;\r\n&quot;</code>)
* as doing so will break up the elasticsearch's bulk api format and bulk operation
* will fail.
*
* @author Dogukan Sonmez
* @author cihat keser
*/
Expand Down Expand Up @@ -100,7 +109,7 @@ protected Object generateBulkPayload(List<BulkableAction> actions) {

private Object getJson(Object source) {
if (source instanceof String) {
return StringUtils.deleteWhitespace((String) source);
return source;
} else {
return gson.toJson(source);
}
Expand Down
61 changes: 55 additions & 6 deletions src/test/java/io/searchbox/core/BulkIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchClient;
import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchNode;
import com.github.tlrx.elasticsearch.test.support.junit.runners.ElasticsearchRunner;
import com.google.gson.Gson;
import io.searchbox.Action;
import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import io.searchbox.params.Parameters;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
Expand Down Expand Up @@ -46,6 +48,53 @@ public void bulkOperationWithIndex() {
}
}

@Test
public void bulkOperationWithIndexWithSourceIncludingWhitespace() {
try {
Map<String, String> source1 = new HashMap<String, String>();
source1.put("user name", "kimchy olga john doe");

String source2 = "{\"k e y\" : \" val v a l \" }";

Bulk bulk = new Bulk.Builder()
.addAction(new Index.Builder(source1).index("twitter").type("tweet").id("1").build())
.addAction(new Index.Builder(source2).index("twitter").type("tweet").id("2").build())
.build();
executeTestCase(bulk);

GetResponse getResponse = directClient.get(new GetRequest("twitter", "tweet", "1")).actionGet();
assertNotNull(getResponse);
assertEquals(new Gson().toJson(source1), getResponse.getSourceAsString());

getResponse = directClient.get(new GetRequest("twitter", "tweet", "2")).actionGet();
assertNotNull(getResponse);
assertEquals(source2, getResponse.getSourceAsString());
} catch (IOException e) {
fail("Failed during the bulk operation Exception:" + e.getMessage());
}
}

@Test
public void bulkOperationWithIndexWithSourceIncludingLineBreak() {
try {
Map<String, String> source1 = new HashMap<String, String>();
source1.put("user name", "kimchy\nolga\njohn doe");

String source2 = "{\"k e y\" : \" val\nv a\r\nl \" }";

Bulk bulk = new Bulk.Builder()
.addAction(new Index.Builder(source1).index("twitter").type("tweet").id("1").build())
.addAction(new Index.Builder(source2).index("twitter").type("tweet").id("2").build())
.build();

JestResult result = client.execute(bulk);
assertNotNull(result);
assertFalse(result.isSucceeded());
} catch (IOException e) {
fail("Failed during the bulk operation Exception:" + e.getMessage());
}
}

@Test
public void bulkOperationWithIndexWithParam() {
try {
Expand All @@ -72,18 +121,18 @@ public void bulkOperationWithIndexWithParam() {
@Test
public void bulkOperationWithIndexAndUpdate() {
try {
String script = "{\n" +
" \"script\" : \"ctx._source.user += tag\",\n" +
" \"params\" : {\n" +
" \"tag\" : \"_osman\"\n" +
" }\n" +
String script = "{" +
" \"script\" : \"ctx._source.user += tag\"," +
" \"params\" : {" +
" \"tag\" : \"_osman\"" +
" }" +
"}";

Map<String, String> source = new HashMap<String, String>();
source.put("user", "kimchy");
Bulk bulk = new Bulk.Builder()
.addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build())
.addAction(new Update.Builder(script).index("twitter").type("tweet").id("1").build())
.addAction(new Update.Builder(StringUtils.chomp(script)).index("twitter").type("tweet").id("1").build())
.build();
executeTestCase(bulk);

Expand Down

0 comments on commit fd5e1ff

Please sign in to comment.