Merge pull request #16110 from talevy/fix-split-append

[Ingest] split string into ArrayList so it can be appended to
This commit is contained in:
Tal Levy 2016-01-21 09:04:19 -08:00
commit 6ee5ea398e
3 changed files with 36 additions and 1 deletions

View File

@ -24,7 +24,10 @@ import org.elasticsearch.ingest.core.AbstractProcessorFactory;
import org.elasticsearch.ingest.core.IngestDocument;
import org.elasticsearch.ingest.core.ConfigurationUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
@ -59,7 +62,9 @@ public class SplitProcessor extends AbstractProcessor {
if (oldVal == null) {
throw new IllegalArgumentException("field [" + field + "] is null, cannot split.");
}
document.setFieldValue(field, Arrays.asList(oldVal.split(separator)));
List<String> splitList = new ArrayList<>();
Collections.addAll(splitList, oldVal.split(separator));
document.setFieldValue(field, splitList);
}
@Override

View File

@ -20,11 +20,17 @@
package org.elasticsearch.ingest.core;
import org.elasticsearch.ingest.TestProcessor;
import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.ingest.processor.AppendProcessor;
import org.elasticsearch.ingest.processor.SetProcessor;
import org.elasticsearch.ingest.processor.SplitProcessor;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.equalTo;

View File

@ -19,15 +19,20 @@
package org.elasticsearch.ingest.processor;
import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.ingest.core.CompoundProcessor;
import org.elasticsearch.ingest.core.IngestDocument;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.core.Processor;
import org.elasticsearch.ingest.core.TemplateService;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.CoreMatchers;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@ -77,4 +82,23 @@ public class SplitProcessorTests extends ESTestCase {
assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
}
}
public void testSplitAppendable() throws Exception {
TemplateService templateService = TestTemplateService.instance();
Map splitConfig = new HashMap<>();
splitConfig.put("field", "flags");
splitConfig.put("separator", "\\|");
Processor splitProcessor = (new SplitProcessor.Factory()).create(splitConfig);
Map appendConfig = new HashMap<>();
appendConfig.put("field", "flags");
appendConfig.put("value", Collections.singletonList("additional_flag"));
Processor appendProcessor = (new AppendProcessor.Factory(templateService)).create(appendConfig);
CompoundProcessor compoundProcessor = new CompoundProcessor(splitProcessor, appendProcessor);
Map<String, Object> source = new HashMap<>();
source.put("flags", "new|hot|super|fun|interesting");
IngestDocument ingestDocument = new IngestDocument(source, new HashMap<>());
compoundProcessor.execute(ingestDocument);
List<String> expectedFlags = Arrays.asList("new", "hot", "super", "fun", "interesting", "additional_flag");
assertThat(ingestDocument.getFieldValue("flags", List.class), CoreMatchers.equalTo(expectedFlags));
}
}