forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DerivedFieldMapper and support parsing it in mappings (opensearch…
…-project#12569) Adds a DerivedFieldMapper to support the Derived Fields feature enhancement as well as updating the mapper parsing logic to recognize and currently parse derived fields in the mappings. --------- Signed-off-by: Mohammad Qureshi <[email protected]> Signed-off-by: Rishabh Maurya <[email protected]> Co-authored-by: Rishabh Maurya <[email protected]> (cherry picked from commit 8ca3e6a)
- Loading branch information
1 parent
c658ad7
commit 694e186
Showing
10 changed files
with
686 additions
and
3 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
129 changes: 129 additions & 0 deletions
129
server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.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,129 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.mapper; | ||
|
||
import org.apache.lucene.index.IndexableField; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.script.Script; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A field mapper for derived fields | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class DerivedFieldMapper extends ParametrizedFieldMapper { | ||
|
||
public static final String CONTENT_TYPE = "derived"; | ||
|
||
private static DerivedFieldMapper toType(FieldMapper in) { | ||
return (DerivedFieldMapper) in; | ||
} | ||
|
||
/** | ||
* Builder for this field mapper | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public static class Builder extends ParametrizedFieldMapper.Builder { | ||
// TODO: The type of parameter may change here if the actual underlying FieldType object is needed | ||
private final Parameter<String> type = Parameter.stringParam("type", false, m -> toType(m).type, "text"); | ||
|
||
private final Parameter<Script> script = new Parameter<>( | ||
"script", | ||
false, | ||
() -> null, | ||
(n, c, o) -> o == null ? null : Script.parse(o), | ||
m -> toType(m).script | ||
).setSerializerCheck((id, ic, value) -> value != null); | ||
|
||
public Builder(String name) { | ||
super(name); | ||
} | ||
|
||
@Override | ||
protected List<Parameter<?>> getParameters() { | ||
return Arrays.asList(type, script); | ||
} | ||
|
||
@Override | ||
public DerivedFieldMapper build(BuilderContext context) { | ||
FieldMapper fieldMapper = DerivedFieldSupportedTypes.getFieldMapperFromType(type.getValue(), name, context); | ||
Function<Object, IndexableField> fieldFunction = DerivedFieldSupportedTypes.getIndexableFieldGeneratorType( | ||
type.getValue(), | ||
name | ||
); | ||
DerivedFieldType ft = new DerivedFieldType( | ||
buildFullName(context), | ||
type.getValue(), | ||
script.getValue(), | ||
fieldMapper, | ||
fieldFunction | ||
); | ||
return new DerivedFieldMapper(name, ft, multiFieldsBuilder.build(this, context), copyTo.build(), this); | ||
} | ||
} | ||
|
||
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n)); | ||
private final String type; | ||
private final Script script; | ||
|
||
protected DerivedFieldMapper( | ||
String simpleName, | ||
MappedFieldType mappedFieldType, | ||
MultiFields multiFields, | ||
CopyTo copyTo, | ||
Builder builder | ||
) { | ||
super(simpleName, mappedFieldType, multiFields, copyTo); | ||
this.type = builder.type.getValue(); | ||
this.script = builder.script.getValue(); | ||
} | ||
|
||
@Override | ||
public DerivedFieldType fieldType() { | ||
return (DerivedFieldType) super.fieldType(); | ||
} | ||
|
||
@Override | ||
protected void parseCreateField(ParseContext context) throws IOException { | ||
// Leaving this empty as the parsing should be handled via the Builder when root object is parsed. | ||
// The context would not contain anything in this case since the DerivedFieldMapper is not indexed or stored. | ||
throw new UnsupportedOperationException("should not be invoked"); | ||
} | ||
|
||
@Override | ||
public ParametrizedFieldMapper.Builder getMergeBuilder() { | ||
return new Builder(simpleName()).init(this); | ||
} | ||
|
||
@Override | ||
protected String contentType() { | ||
return CONTENT_TYPE; | ||
} | ||
|
||
@Override | ||
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { | ||
getMergeBuilder().toXContent(builder, includeDefaults); | ||
multiFields.toXContent(builder, params); | ||
copyTo.toXContent(builder, params); | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public Script getScript() { | ||
return script; | ||
} | ||
} |
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
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.