Bulk extends AbstractAction<BulkResult> { @Override public String getData(Gson gson) { StringBuilder sb = new StringBuilder(); for (BulkableAction action : bulkableActions) { Map<String, Map<String, String>> opMap = new LinkedHashMap<String, Map<String, String>>(1); Map<String, String> opDetails = new LinkedHashMap<String, String>(3); if (StringUtils.isNotBlank(action.getId())) { opDetails.put("_id", action.getId()); } if (StringUtils.isNotBlank(action.getIndex())) { opDetails.put("_index", action.getIndex()); } if (StringUtils.isNotBlank(action.getType())) { opDetails.put("_type", action.getType()); } for (String parameter : Parameters.ACCEPTED_IN_BULK) { try { Collection<Object> values = action.getParameter(parameter); if (values != null) { if (values.size() == 1) { opDetails.put(parameter, values.iterator().next().toString()); } else if (values.size() > 1) { throw new IllegalArgumentException("Expecting a single value for '" + parameter + "' parameter, you provided: " + values.size()); } } } catch (NullPointerException e) { log.debug("Could not retrieve '" + parameter + "' parameter from action.", e); } } opMap.put(action.getBulkMethodName(), opDetails); sb.append(gson.toJson(opMap, new TypeToken<Map<String, Map<String, String>>>() { }.getType())); sb.append("\n"); Object source = action.getData(gson); if (source != null) { sb.append(getJson(gson, source)); sb.append("\n"); } } return sb.toString(); } protected  Bulk(Builder builder); @Override String getRestMethodName(); @Override String getData(Gson gson); @Override String getPathToResult(); @Override BulkResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void bulkOperationWithIndex() throws JSONException { Map source = new HashMap(); source.put("field", "value"); Bulk bulk = new Bulk.Builder() .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) .build(); executeAsserts(bulk); String expectedData = "{\"index\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" + "{\"field\":\"value\"}"; JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false); }
@Test public void bulkOperationWithSingleDelete() throws JSONException { Bulk bulk = new Bulk.Builder() .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) .build(); executeAsserts(bulk); String expectedData = "{\"delete\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n"; JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false); }
@Test public void bulkOperationWithMultipleIndex() throws JSONException { Map source = new HashMap(); source.put("field", "value"); Bulk bulk = new Bulk.Builder() .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) .addAction(new Index.Builder(source).index("elasticsearch").type("jest").id("2").build()) .build(); executeAsserts(bulk); String expectedData = "{\"index\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" + "{\"field\":\"value\"}\n" + "{\"index\":{\"_id\":\"2\",\"_index\":\"elasticsearch\",\"_type\":\"jest\"}}\n" + "{\"field\":\"value\"}"; JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false); }
@Test public void bulkOperationWithMultipleDelete() throws JSONException { Bulk bulk = new Bulk.Builder() .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) .addAction(new Delete.Builder("2").index("twitter").type("tweet").build()) .build(); executeAsserts(bulk); String expectedData = "{\"delete\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" + "{\"delete\":{\"_id\":\"2\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}"; JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()).toString(), false); }
@Test public void bulkOperationWithMultipleIndexAndDelete() throws JSONException { Map source = new HashMap(); source.put("field", "value"); Bulk bulk = new Bulk.Builder() .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) .addAction(new Index.Builder(source).index("elasticsearch").type("jest").id("2").build()) .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) .addAction(new Delete.Builder("2").index("twitter").type("tweet").build()) .build(); executeAsserts(bulk); String expectedData = "{\"index\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" + "{\"field\":\"value\"}\n" + "{\"index\":{\"_id\":\"2\",\"_index\":\"elasticsearch\",\"_type\":\"jest\"}}\n" + "{\"field\":\"value\"}\n" + "{\"delete\":{\"_id\":\"1\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}\n" + "{\"delete\":{\"_id\":\"2\",\"_index\":\"twitter\",\"_type\":\"tweet\"}}"; JSONAssert.assertEquals(expectedData, bulk.getData(new Gson()), false); }