-
Notifications
You must be signed in to change notification settings - Fork 25.2k
version set in ingest pipeline #27573
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
Changes from 7 commits
4e1b507
180d8c1
590bd72
f84d796
e42a8de
47c3733
7981402
d93d6bb
5595f42
ee9a6df
4b1ad02
380fced
10e41bd
1e5c3f9
8799107
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.Processor; | ||
import org.elasticsearch.ingest.RandomDocumentPicks; | ||
|
@@ -101,10 +102,28 @@ public void testSetExistingNullFieldWithOverrideDisabled() throws Exception { | |
|
||
public void testSetMetadata() throws Exception { | ||
IngestDocument.MetaData randomMetaData = randomFrom(IngestDocument.MetaData.values()); | ||
Processor processor = createSetProcessor(randomMetaData.getFieldName(), "_value", true); | ||
Processor processor; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous comment. |
||
long version = 0L; | ||
String versionType = new String(); | ||
if (randomMetaData == IngestDocument.MetaData.VERSION) { | ||
version = randomLong(); | ||
processor = createSetProcessor(randomMetaData.getFieldName(), version, true); | ||
} else if (randomMetaData == IngestDocument.MetaData.VERSION_TYPE) { | ||
versionType = randomFrom("internal", "external", "external_gt", "external_gte"); | ||
processor = createSetProcessor(randomMetaData.getFieldName(), versionType, true); | ||
} else { | ||
processor = createSetProcessor(randomMetaData.getFieldName(), "_value", true); | ||
} | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
processor.execute(ingestDocument); | ||
assertThat(ingestDocument.getFieldValue(randomMetaData.getFieldName(), String.class), Matchers.equalTo("_value")); | ||
if (randomMetaData == IngestDocument.MetaData.VERSION) { | ||
assertThat(ingestDocument.getFieldValue(randomMetaData.getFieldName(), Long.class), Matchers.equalTo(version)); | ||
} else if (randomMetaData == IngestDocument.MetaData.VERSION_TYPE) { | ||
assertThat(ingestDocument.getFieldValue(randomMetaData.getFieldName(), VersionType.class), | ||
Matchers.equalTo(VersionType.fromString(versionType))); | ||
} else { | ||
assertThat(ingestDocument.getFieldValue(randomMetaData.getFieldName(), String.class), Matchers.equalTo("_value")); | ||
} | ||
} | ||
|
||
private static Processor createSetProcessor(String fieldName, Object fieldValue, boolean overrideEnabled) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,14 @@ | |
package org.elasticsearch.ingest; | ||
|
||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.settings.Setting; | ||
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.index.mapper.IdFieldMapper; | ||
import org.elasticsearch.index.mapper.IndexFieldMapper; | ||
import org.elasticsearch.index.mapper.ParentFieldMapper; | ||
import org.elasticsearch.index.mapper.RoutingFieldMapper; | ||
import org.elasticsearch.index.mapper.SourceFieldMapper; | ||
import org.elasticsearch.index.mapper.TypeFieldMapper; | ||
import org.elasticsearch.script.ScriptService; | ||
import org.elasticsearch.index.mapper.VersionFieldMapper; | ||
import org.elasticsearch.script.TemplateScript; | ||
|
||
import java.time.ZoneOffset; | ||
|
@@ -73,6 +73,17 @@ public IngestDocument(String index, String type, String id, String routing, Stri | |
this.ingestMetadata.put(TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC)); | ||
} | ||
|
||
public IngestDocument(String index, String type, String id, String routing,String parent, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding a new constructor, can the existing constructor be modified to also accept version and versionType? I know this requires changing more tests, but I think this class should only have two constructors. |
||
Long version, VersionType versionType,Map<String, Object> source) { | ||
this(index, type, id, routing, parent, source); | ||
if (version != null) { | ||
sourceAndMetadata.put(MetaData.VERSION.getFieldName(), version); | ||
} | ||
if (versionType != null) { | ||
sourceAndMetadata.put(MetaData.VERSION_TYPE.getFieldName(), versionType); | ||
} | ||
} | ||
|
||
/** | ||
* Copy constructor that creates a new {@link IngestDocument} which has exactly the same properties as the one provided as argument | ||
*/ | ||
|
@@ -457,6 +468,9 @@ private void setFieldValue(String path, Object value, boolean append) { | |
} | ||
|
||
String leafKey = fieldPath.pathElements[fieldPath.pathElements.length - 1]; | ||
if (leafKey == MetaData.VERSION_TYPE.getFieldName()) { | ||
value = VersionType.fromString(value.toString()); | ||
} | ||
if (context == null) { | ||
throw new IllegalArgumentException("cannot set [" + leafKey + "] with null parent as part of path [" + path + "]"); | ||
} | ||
|
@@ -559,10 +573,17 @@ private Map<String, Object> createTemplateModel() { | |
* one time operation that extracts the metadata fields from the ingest document and returns them. | ||
* Metadata fields that used to be accessible as ordinary top level fields will be removed as part of this call. | ||
*/ | ||
public Map<MetaData, String> extractMetadata() { | ||
Map<MetaData, String> metadataMap = new EnumMap<>(MetaData.class); | ||
public Map<MetaData, Object> extractMetadata() { | ||
Map<MetaData, Object> metadataMap = new EnumMap<>(MetaData.class); | ||
for (MetaData metaData : MetaData.values()) { | ||
metadataMap.put(metaData, cast(metaData.getFieldName(), sourceAndMetadata.remove(metaData.getFieldName()), String.class)); | ||
if (metaData == MetaData.VERSION) { | ||
metadataMap.put(metaData, cast(metaData.getFieldName(), sourceAndMetadata.remove(metaData.getFieldName()), Long.class)); | ||
} else if (metaData == MetaData.VERSION_TYPE) { | ||
metadataMap.put(metaData, | ||
cast(metaData.getFieldName(), sourceAndMetadata.remove(metaData.getFieldName()), VersionType.class)); | ||
} else { | ||
metadataMap.put(metaData, cast(metaData.getFieldName(), sourceAndMetadata.remove(metaData.getFieldName()), String.class)); | ||
} | ||
} | ||
return metadataMap; | ||
} | ||
|
@@ -651,7 +672,9 @@ public enum MetaData { | |
TYPE(TypeFieldMapper.NAME), | ||
ID(IdFieldMapper.NAME), | ||
ROUTING(RoutingFieldMapper.NAME), | ||
PARENT(ParentFieldMapper.NAME); | ||
PARENT(ParentFieldMapper.NAME), | ||
VERSION(VersionFieldMapper.NAME), | ||
VERSION_TYPE(VersionFieldMapper.VersionFieldType.NAME); | ||
|
||
private final String fieldName; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can version and version_type be tested in a dedicated test? I think that makes these tests easier to read.