-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script: Ingest Metadata and CtxMap (#88458)
Create a `Metadata` superclass for ingest and update contexts. Create a `CtxMap` superclass for `ctx` backwards compatibility in ingest and update contexts. `script.CtxMap` was moved from `ingest.IngestSourceAndMetadata` `CtxMap` takes a `Metadata` subclass and validates update via the `FieldProperty`s passed in. `Metadata` provides typed getters and setters and implements a `Map`-like interface, making it easy for a class containing `CtxMap` to implement the full `Map` interface. The `FieldProperty` record that configures how to validate fields. Fields have a `type`, are `writeable` or read-only, and `nullable` or not and may have an additional validation useful for Set/Enum validation.
- Loading branch information
1 parent
d7d9ff2
commit 85b8d3d
Showing
13 changed files
with
593 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
server/src/main/java/org/elasticsearch/ingest/IngestCtxMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.ingest; | ||
|
||
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.script.CtxMap; | ||
import org.elasticsearch.script.Metadata; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Map containing ingest source and metadata. | ||
* | ||
* The Metadata values in {@link IngestDocument.Metadata} are validated when put in the map. | ||
* _index, _id and _routing must be a String or null | ||
* _version_type must be a lower case VersionType or null | ||
* _version must be representable as a long without loss of precision or null | ||
* _dyanmic_templates must be a map | ||
* _if_seq_no must be a long or null | ||
* _if_primary_term must be a long or null | ||
* | ||
* The map is expected to be used by processors, server code should the typed getter and setters where possible. | ||
*/ | ||
class IngestCtxMap extends CtxMap { | ||
|
||
/** | ||
* Create an IngestCtxMap with the given metadata, source and default validators | ||
*/ | ||
IngestCtxMap( | ||
String index, | ||
String id, | ||
long version, | ||
String routing, | ||
VersionType versionType, | ||
ZonedDateTime timestamp, | ||
Map<String, Object> source | ||
) { | ||
super(new HashMap<>(source), new IngestDocMetadata(index, id, version, routing, versionType, timestamp)); | ||
} | ||
|
||
/** | ||
* Create IngestCtxMap from a source and metadata | ||
* | ||
* @param source the source document map | ||
* @param metadata the metadata map | ||
*/ | ||
IngestCtxMap(Map<String, Object> source, Metadata metadata) { | ||
super(source, metadata); | ||
} | ||
|
||
/** | ||
* Fetch the timestamp from the ingestMetadata, if it exists | ||
* @return the timestamp for the document or null | ||
*/ | ||
public static ZonedDateTime getTimestamp(Map<String, Object> ingestMetadata) { | ||
if (ingestMetadata == null) { | ||
return null; | ||
} | ||
Object ts = ingestMetadata.get(IngestDocument.TIMESTAMP); | ||
if (ts instanceof ZonedDateTime timestamp) { | ||
return timestamp; | ||
} else if (ts instanceof String str) { | ||
return ZonedDateTime.parse(str); | ||
} | ||
return null; | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
server/src/main/java/org/elasticsearch/ingest/IngestDocMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.ingest; | ||
|
||
import org.elasticsearch.common.util.Maps; | ||
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.script.Metadata; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
class IngestDocMetadata extends Metadata { | ||
private static final FieldProperty<String> UPDATABLE_STRING = new FieldProperty<>(String.class, true, true, null); | ||
static final Map<String, FieldProperty<?>> PROPERTIES = Map.of( | ||
INDEX, | ||
UPDATABLE_STRING, | ||
ID, | ||
UPDATABLE_STRING, | ||
ROUTING, | ||
UPDATABLE_STRING, | ||
VERSION_TYPE, | ||
new FieldProperty<>(String.class, true, true, (k, v) -> { | ||
try { | ||
VersionType.fromString(v); | ||
return; | ||
} catch (IllegalArgumentException ignored) {} | ||
throw new IllegalArgumentException( | ||
k | ||
+ " must be a null or one of [" | ||
+ Arrays.stream(VersionType.values()).map(vt -> VersionType.toString(vt)).collect(Collectors.joining(", ")) | ||
+ "] but was [" | ||
+ v | ||
+ "] with type [" | ||
+ v.getClass().getName() | ||
+ "]" | ||
); | ||
}), | ||
VERSION, | ||
new FieldProperty<>(Number.class, false, true, FieldProperty.LONGABLE_NUMBER), | ||
TYPE, | ||
new FieldProperty<>(String.class, true, false, null), | ||
IF_SEQ_NO, | ||
new FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER), | ||
IF_PRIMARY_TERM, | ||
new FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER), | ||
DYNAMIC_TEMPLATES, | ||
new FieldProperty<>(Map.class, true, true, null) | ||
); | ||
|
||
protected final ZonedDateTime timestamp; | ||
|
||
IngestDocMetadata(String index, String id, long version, String routing, VersionType versionType, ZonedDateTime timestamp) { | ||
this(metadataMap(index, id, version, routing, versionType), timestamp); | ||
} | ||
|
||
IngestDocMetadata(Map<String, Object> metadata, ZonedDateTime timestamp) { | ||
super(metadata, PROPERTIES); | ||
this.timestamp = timestamp; | ||
} | ||
|
||
/** | ||
* Create the backing metadata map with the standard contents assuming default validators. | ||
*/ | ||
protected static Map<String, Object> metadataMap(String index, String id, long version, String routing, VersionType versionType) { | ||
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(IngestDocument.Metadata.values().length); | ||
metadata.put(IngestDocument.Metadata.INDEX.getFieldName(), index); | ||
metadata.put(IngestDocument.Metadata.ID.getFieldName(), id); | ||
metadata.put(IngestDocument.Metadata.VERSION.getFieldName(), version); | ||
if (routing != null) { | ||
metadata.put(IngestDocument.Metadata.ROUTING.getFieldName(), routing); | ||
} | ||
if (versionType != null) { | ||
metadata.put(IngestDocument.Metadata.VERSION_TYPE.getFieldName(), VersionType.toString(versionType)); | ||
} | ||
return metadata; | ||
} | ||
|
||
@Override | ||
public ZonedDateTime getTimestamp() { | ||
return timestamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.