Skip to content

Commit bd4ab06

Browse files
authored
Merge pull request #323 from metafacture/allowOmittingIdInElasticsearchBulk
Allow to omit id in JsonToElasticsearchBulk
2 parents 7871f35 + 19def0d commit bd4ab06

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

metafacture-elasticsearch/src/main/java/org/metafacture/elasticsearch/JsonToElasticsearchBulk.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public Object put(String key, Object value) {
7878
private String type;
7979
private String index;
8080

81+
/**
82+
* As an id is not required it can be omitted.
83+
*
84+
* @param type The Elasticsearch index type
85+
* @param index The Elasticsearch index name
86+
*/
87+
public JsonToElasticsearchBulk(String type, String index) {
88+
this(new String[] { }, type, index);
89+
}
90+
8191
/**
8292
* @param idPath The key path of the JSON value to be used as the ID for the record
8393
* @param type The Elasticsearch index type
@@ -116,7 +126,7 @@ public void process(String obj) {
116126
Map<String, Object> detailsMap = new HashMap<String, Object>();
117127
Map<String, Object> indexMap = new HashMap<String, Object>();
118128
indexMap.put("index", detailsMap);
119-
detailsMap.put("_id", findId(json));
129+
if (idPath.length > 0) detailsMap.put("_id", findId(json));
120130
detailsMap.put("_type", type);
121131
detailsMap.put("_index", index);
122132
mapper.writeValue(stringWriter, indexMap);
@@ -129,10 +139,6 @@ public void process(String obj) {
129139
}
130140

131141
private Object findId(Object value) {
132-
if (idPath.length < 1) {
133-
return null;
134-
}
135-
136142
for (final String key : idPath) {
137143
if (value instanceof Map) {
138144
@SuppressWarnings("unchecked")

metafacture-elasticsearch/src/test/java/org/metafacture/elasticsearch/JsonToElasticsearchBulkTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public final class JsonToElasticsearchBulkTest {
4545
private static final String TYPE1 = "T1";
4646
private static final String INDEX1 = "I1";
4747

48-
private static final String METADATA = "{'index':{'_index':'I1','_type':'T1','_id':%s}}";
48+
private static final String METADATA = "{'index':{'_index':'I1','_type':'T1'%s}}";
4949

5050
private static final String ENTITY_SEPARATOR1 = ".";
5151
private static final String ENTITY_SEPARATOR2 = ":";
@@ -122,7 +122,13 @@ public void testShouldNotExtractEmptyIdKey() {
122122
@Test
123123
public void testShouldNotExtractEmptyIdPath() {
124124
setBulk(new String[0]);
125-
shouldNotExtractId("{'L1':'V1','L2':'V2','L3':'V3'}");
125+
shouldExtractId("{'L1':'V1','L2':'V2','L3':'V3'}", null);
126+
}
127+
128+
@Test
129+
public void testShouldNotExtractOmittedIdPath() {
130+
bulk = new JsonToElasticsearchBulk(TYPE1, INDEX1);
131+
shouldExtractId("{'L1':'V1','L2':'V2','L3':'V3'}", null);
126132
}
127133

128134
@Test
@@ -220,7 +226,9 @@ private void shouldExtractId(final String obj, final String idValue, final Strin
220226
bulk.setReceiver(receiver);
221227
bulk.process(fixQuotes(obj));
222228

223-
verify(receiver).process(fixQuotes(String.format(METADATA, idValue) + "\n" + resultObj));
229+
final String metadata = String.format(METADATA, idValue != null ? ",'_id':" + idValue : "");
230+
231+
verify(receiver).process(fixQuotes(metadata + "\n" + resultObj));
224232
verifyNoMoreInteractions(receiver);
225233
}
226234

0 commit comments

Comments
 (0)