Skip to content

Commit 35ce879

Browse files
committed
Allow to omit id in JsonToElasticsearchBulk
Elasticsearch creates an internal id on its own when omitting and id in the json bulk.
1 parent 7871f35 commit 35ce879

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
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")

0 commit comments

Comments
 (0)