Skip to content

Field API path syntax support in IngestDocument #125804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions server/src/main/java/org/elasticsearch/ingest/IngestDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.UpdateForV10;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.mapper.IdFieldMapper;
Expand All @@ -24,6 +25,7 @@
import org.elasticsearch.script.CtxMap;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.script.field.WriteField;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -192,6 +194,17 @@ public <T> T getFieldValue(String path, Class<T> clazz) {
*/
public <T> T getFieldValue(String path, Class<T> clazz, boolean ignoreMissing) {
final FieldPath fieldPath = FieldPath.of(path);
WriteField writeField = getWriteField(path);
if (writeField != null) {
if (writeField.exists() == false) {
if (ignoreMissing == false) {
throw new IllegalArgumentException("no field found for path " + path);
} else {
return null;
}
}
return cast(path, writeField.get(null), clazz);
}
Object context = fieldPath.initialContext(this);
ResolveResult result = resolve(fieldPath.pathElements, fieldPath.pathElements.length, path, context);
if (result.wasSuccessful) {
Expand Down Expand Up @@ -257,6 +270,10 @@ public boolean hasField(String path) {
* @throws IllegalArgumentException if the path is null, empty or invalid.
*/
public boolean hasField(String path, boolean failOutOfRange) {
WriteField writeField = getWriteField(path);
if (writeField != null) {
return writeField.exists();
}
final FieldPath fieldPath = FieldPath.of(path);
Object context = fieldPath.initialContext(this);
for (int i = 0; i < fieldPath.pathElements.length - 1; i++) {
Expand Down Expand Up @@ -325,6 +342,17 @@ public void removeField(String path) {
removeField(path, false);
}

@Nullable
private WriteField getWriteField(String path) {
if (path == null) {
return null;
}
if ((path.startsWith("$('") && path.endsWith("')")) || (path.startsWith("$(\"") && path.endsWith("\")"))) {
return new WriteField(path.substring(3, path.length() - 2), this::getCtxMap);
}
return null;
}

/**
* Removes the field identified by the provided path.
*
Expand All @@ -333,6 +361,14 @@ public void removeField(String path) {
* @throws IllegalArgumentException if the path is null, empty, or invalid; or if the field doesn't exist (and ignoreMissing is false).
*/
public void removeField(String path, boolean ignoreMissing) {
WriteField writeField = getWriteField(path);
if (writeField != null) {
if (ignoreMissing == false && writeField.exists() == false) {
throw new IllegalArgumentException("no field found for path " + path);
}
writeField.remove();
return;
}
final FieldPath fieldPath = FieldPath.of(path);
Object context = fieldPath.initialContext(this);
ResolveResult result = resolve(fieldPath.pathElements, fieldPath.pathElements.length - 1, path, context);
Expand Down Expand Up @@ -548,6 +584,15 @@ public void setFieldValue(String path, Object value, boolean ignoreEmptyValue) {
}

private void setFieldValue(String path, Object value, boolean append, boolean allowDuplicates) {
WriteField writeField = getWriteField(path);
if (writeField != null) {
if (append && writeField.exists()) {
writeField.set(appendValues(writeField.get(null), value, allowDuplicates));
} else {
writeField.set(value);
}
return;
}
final FieldPath fieldPath = FieldPath.of(path);
Object context = fieldPath.initialContext(this);
for (int i = 0; i < fieldPath.pathElements.length - 1; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,4 +1227,31 @@ public void testSourceHashMapIsNotCopied() {
assertThat(document2.getCtxMap().getMetadata(), not(sameInstance(document1.getCtxMap().getMetadata())));
}
}

public void testFieldApiPathSyntax() {
IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());

doc.setFieldValue("$('foo.bar.baz')", "qux");
assertThat(doc.getFieldValue("foo.bar.baz", String.class), equalTo("qux"));
assertThat(doc.getFieldValue("$(\"foo.bar.baz\")", String.class), equalTo("qux"));

doc.appendFieldValue("$('foo.bar.baz')", "quux");
assertThat(doc.getFieldValue("foo.bar.baz", List.class), equalTo(List.of("qux", "quux")));
doc.appendFieldValue("$('foo.bar.baz')", "quux", false);
assertThat(doc.getFieldValue("foo.bar.baz", List.class), equalTo(List.of("qux", "quux")));

IllegalArgumentException e;
e = expectThrows(IllegalArgumentException.class, () -> document.getFieldValue("$('foo.missing')", String.class, false));
assertThat(e.getMessage(), is("no field found for path $('foo.missing')"));

doc.getSource().put("host.name", "localhost");
assertTrue(doc.hasField("$('foo.bar.baz')"));
assertThat(doc.getFieldValue("$('host.name')", String.class), equalTo("localhost"));

doc.removeField("$('foo.bar.baz')");
assertFalse(doc.hasField("$('foo.bar.baz')"));
// when removing the same field again, it should throw an exception
e = expectThrows(IllegalArgumentException.class, () -> doc.removeField("$('foo.bar.baz')"));
assertThat(e.getMessage(), is("no field found for path $('foo.bar.baz')"));
}
}