import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import java.util.Map;
public class ListUpdate extends AbstractExecutableScript {
if (fieldValue == null && value != null) {
// 0. The field does not exist (this is a upsert then)
- source.put(field, value);
+ List values = new ArrayList<Object>(1);
+ values.add(value);
+ source.put(field, values);
} else if (!XContentMapValues.isArray(fieldValue) && value != null) {
// 1. The field is not yet a list
Map currentFieldValue = XContentMapValues.nodeMapValue(fieldValue, "current FieldValue");
if (XContentMapValues.nodeStringValue(currentFieldValue.get(idField), null).equals(idValue)) {
source.put(field, value);
} else {
- source.put(field, org.elasticsearch.common.collect.ImmutableSet.of(fieldValue, value));
+ List values = new ArrayList<Object>(2);
+ values.add(fieldValue);
+ values.add(value);
+ source.put(field, values);
}
} else {
// 3. field is a list
import org.junit.Before;
import org.junit.Test;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
assertThat(e.getMessage()).isEqualTo("Missing 'idField' parameter");
}
}
+
+
+ @Test
+ public void update_list() throws Exception {
+
+ String listField = "listField";
+ Collection<Map<String, Object>> mapFields;
+ Map source = new HashMap<String, Object>();
+ source.put("field1", "value1");
+
+ // 0 Create list when field does not exists
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put(ListUpdate.FIELD, listField);
+ params.put(ListUpdate.ID_FIELD, "key");
+ params.put(ListUpdate.ID_VALUE, "1");
+ params.put(ListUpdate.VALUE, mapOf("key", "1", "value", "A"));
+
+ ExecutableScript script = factory.newScript(params);
+ script.setNextVar("ctx", ImmutableMap.of("_source",source));
+ script.run();
+
+ mapFields = (Collection)source.get(listField);
+ System.out.println("source = " + source);
+ assertThat(mapFields).hasSize(1);
+
+ // Add item to existing list
+ params = new HashMap<String, Object>();
+ params.put(ListUpdate.FIELD, listField);
+ params.put(ListUpdate.ID_FIELD, "key");
+ params.put(ListUpdate.ID_VALUE, "2");
+ params.put(ListUpdate.VALUE, mapOf("key", "2", "value", "B"));
+ script = factory.newScript(params);
+ script.setNextVar("ctx", ImmutableMap.of("_source",source));
+ script.run();
+ mapFields = (Collection)source.get(listField);
+ assertThat(mapFields).hasSize(2);
+
+ // updated first item in list
+ params = new HashMap<String, Object>();
+ params.put(ListUpdate.FIELD, listField);
+ params.put(ListUpdate.ID_FIELD, "key");
+ params.put(ListUpdate.ID_VALUE, "1");
+ params.put(ListUpdate.VALUE, mapOf("key", "1", "value", "a"));
+ script = factory.newScript(params);
+ script.setNextVar("ctx", ImmutableMap.of("_source",source));
+ script.run();
+ mapFields = (Collection)source.get(listField);
+ assertThat(mapFields).hasSize(2);
+
+ // updated second item in list
+ params = new HashMap<String, Object>();
+ params.put(ListUpdate.FIELD, listField);
+ params.put(ListUpdate.ID_FIELD, "key");
+ params.put(ListUpdate.ID_VALUE, "2");
+ params.put(ListUpdate.VALUE, mapOf("key", "2", "value","b"));
+ script = factory.newScript(params);
+ script.setNextVar("ctx", ImmutableMap.of("_source",source));
+ script.run();
+ mapFields = (Collection)source.get(listField);
+ assertThat(mapFields).hasSize(2);
+
+ // delete first item
+ params = new HashMap<String, Object>();
+ params.put(ListUpdate.FIELD, listField);
+ params.put(ListUpdate.ID_FIELD, "key");
+ params.put(ListUpdate.ID_VALUE, "1");
+ params.put(ListUpdate.VALUE, null);
+ script = factory.newScript(params);
+ script.setNextVar("ctx", ImmutableMap.of("_source",source));
+ script.run();
+ mapFields = (Collection)source.get(listField);
+ assertThat(mapFields).hasSize(1);
+ }
+
+ private Map<String, Object> mapOf(String k, String v, String k1, String v1) {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put(k, v);
+ map.put(k1, v1);
+ return map;
+ }
}
\ No newline at end of file