Skip to content

Commit

Permalink
Merge pull request searchbox-io#336 from logzio/feature/handle_index_…
Browse files Browse the repository at this point in the history
…failure_bulk_item

Fix NPE when bulk item result has "_id":null
  • Loading branch information
Cihat Keser committed Apr 21, 2016
2 parents 6483126 + 7c4f8ce commit 8fe6975
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public BulkResultItem(String operation, JsonObject values) {
this.operation = operation;
this.index = values.get("_index").getAsString();
this.type = values.get("_type").getAsString();
this.id = values.get("_id").getAsString();
this.id = values.has("_id") && !values.get("_id").isJsonNull() ? values.get("_id").getAsString() : null;
this.status = values.get("status").getAsInt();
this.error = values.has("error") ? values.get("error").toString() : null;
}
Expand Down
40 changes: 40 additions & 0 deletions jest-common/src/test/java/io/searchbox/core/BulkResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.searchbox.core;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;

public class BulkResultTest {

static String indexFailedResult = "{\n" +
" \"took\": 10,\n" +
" \"errors\": true,\n" +
" \"items\": [\n" +
" {\n" +
" \"index\": {\n" +
" \"_index\": \"index-name\",\n" +
" \"_type\": \"type-name\",\n" +
" \"_id\": null,\n" +
" \"status\": 400,\n" +
" \"error\": \"MapperParsingException[mapping [type-name]]; nested: MapperParsingException[No type specified for property [field-name]]; \"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";

@Test
public void bulkResultWithFailures() {
BulkResult bulkResult = new BulkResult(new GsonBuilder().serializeNulls().create());
bulkResult.setJsonString(indexFailedResult);
bulkResult.setJsonMap(new Gson().fromJson(indexFailedResult, Map.class));
bulkResult.setSucceeded(false);

assertEquals(1, bulkResult.getItems().size());
assertEquals(1, bulkResult.getFailedItems().size());
}
}

0 comments on commit 8fe6975

Please sign in to comment.