Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,22 @@ abstract static class Builder<Factory> extends RuntimeField.Builder {
this.parseFromSourceFactory = parseFromSourceFactory;
}

abstract RuntimeField newRuntimeField(Factory scriptFactory);

@Override
protected final RuntimeField createRuntimeField(MappingParserContext parserContext) {
if (script.get() == null) {
return newRuntimeField(parseFromSourceFactory);
return createRuntimeField(parseFromSourceFactory);
}
Factory factory = parserContext.scriptCompiler().compile(script.getValue(), scriptContext);
return newRuntimeField(factory);
return createRuntimeField(factory);
}

final RuntimeField createRuntimeField(Factory scriptFactory) {
AbstractScriptFieldType<?> fieldType = createFieldType(name, scriptFactory, getScript(), meta());
return new LeafRuntimeField(name, fieldType, this);
}

abstract AbstractScriptFieldType<?> createFieldType(String name, Factory factory, Script script, Map<String, String> meta);

@Override
protected List<FieldMapper.Parameter<?>> getParameters() {
List<FieldMapper.Parameter<?>> parameters = new ArrayList<>(super.getParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.index.fielddata.BooleanScriptFieldData;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand All @@ -27,38 +26,29 @@

import java.time.ZoneId;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Supplier;

public final class BooleanScriptFieldType extends AbstractScriptFieldType<BooleanFieldScript.LeafFactory> {

public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(name ->
new Builder<>(name, BooleanFieldScript.CONTEXT, BooleanFieldScript.PARSE_FROM_SOURCE) {
@Override
RuntimeField newRuntimeField(BooleanFieldScript.Factory scriptFactory) {
return runtimeField(name, this, scriptFactory, getScript(), meta());
}
});
public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(Builder::new);

private static RuntimeField runtimeField(
String name,
ToXContent toXContent,
BooleanFieldScript.Factory scriptFactory,
Script script,
Map<String, String> meta
) {
return new LeafRuntimeField(name, new BooleanScriptFieldType(name, scriptFactory, script, meta), toXContent);
private static class Builder extends AbstractScriptFieldType.Builder<BooleanFieldScript.Factory> {
Builder(String name) {
super(name, BooleanFieldScript.CONTEXT, BooleanFieldScript.PARSE_FROM_SOURCE);
}

@Override
AbstractScriptFieldType<?> createFieldType(String name,
BooleanFieldScript.Factory factory,
Script script,
Map<String, String> meta) {
return new BooleanScriptFieldType(name, factory, script, meta);
}
}

public static RuntimeField sourceOnly(String name) {
return runtimeField(
name,
(builder, params) -> builder,
BooleanFieldScript.PARSE_FROM_SOURCE,
DEFAULT_SCRIPT,
Collections.emptyMap()
);
return new Builder(name).createRuntimeField(BooleanFieldScript.PARSE_FROM_SOURCE);
}

BooleanScriptFieldType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.common.util.LocaleUtils;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.fielddata.DateScriptFieldData;
Expand Down Expand Up @@ -45,77 +44,62 @@

public class DateScriptFieldType extends AbstractScriptFieldType<DateFieldScript.LeafFactory> {

public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(name ->
new Builder<>(name, DateFieldScript.CONTEXT, DateFieldScript.PARSE_FROM_SOURCE) {
private final FieldMapper.Parameter<String> format = FieldMapper.Parameter.stringParam(
"format",
true,
initializerNotSupported(),
null
).setSerializer((b, n, v) -> {
if (v != null && false == v.equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern())) {
b.field(n, v);
}
}, Object::toString).acceptsNull();

private final FieldMapper.Parameter<Locale> locale = new FieldMapper.Parameter<>(
"locale",
true,
() -> null,
(n, c, o) -> o == null ? null : LocaleUtils.parse(o.toString()),
initializerNotSupported()
).setSerializer((b, n, v) -> {
if (v != null && false == v.equals(Locale.ROOT)) {
b.field(n, v.toString());
}
}, Object::toString).acceptsNull();

@Override
protected List<FieldMapper.Parameter<?>> getParameters() {
List<FieldMapper.Parameter<?>> parameters = new ArrayList<>(super.getParameters());
parameters.add(format);
parameters.add(locale);
return Collections.unmodifiableList(parameters);
public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(Builder::new);

private static class Builder extends AbstractScriptFieldType.Builder<DateFieldScript.Factory> {
private final FieldMapper.Parameter<String> format = FieldMapper.Parameter.stringParam(
"format",
true,
initializerNotSupported(),
null
).setSerializer((b, n, v) -> {
if (v != null && false == v.equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern())) {
b.field(n, v);
}

@Override
RuntimeField newRuntimeField(DateFieldScript.Factory scriptFactory) {
String pattern = format.getValue() == null ? DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern() : format.getValue();
Locale locale = this.locale.getValue() == null ? Locale.ROOT : this.locale.getValue();
DateFormatter dateTimeFormatter = DateFormatter.forPattern(pattern).withLocale(locale);
return runtimeField(name, this, dateTimeFormatter, scriptFactory, getScript(), meta());
}, Object::toString).acceptsNull();

private final FieldMapper.Parameter<Locale> locale = new FieldMapper.Parameter<>(
"locale",
true,
() -> null,
(n, c, o) -> o == null ? null : LocaleUtils.parse(o.toString()),
initializerNotSupported()
).setSerializer((b, n, v) -> {
if (v != null && false == v.equals(Locale.ROOT)) {
b.field(n, v.toString());
}
});
}, Object::toString).acceptsNull();

private final DateFormatter dateTimeFormatter;
private final DateMathParser dateMathParser;
Builder(String name) {
super(name, DateFieldScript.CONTEXT, DateFieldScript.PARSE_FROM_SOURCE);
}

private static RuntimeField runtimeField(
String name,
ToXContent toXContent,
DateFormatter dateFormatter,
DateFieldScript.Factory scriptFactory,
Script script,
Map<String, String> meta
) {
return new LeafRuntimeField(name, new DateScriptFieldType(name, scriptFactory, dateFormatter, script, meta), toXContent);
@Override
protected List<FieldMapper.Parameter<?>> getParameters() {
List<FieldMapper.Parameter<?>> parameters = new ArrayList<>(super.getParameters());
parameters.add(format);
parameters.add(locale);
return Collections.unmodifiableList(parameters);
}

@Override
AbstractScriptFieldType<?> createFieldType(String name, DateFieldScript.Factory factory, Script script, Map<String, String> meta) {
String pattern = format.getValue() == null ? DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern() : format.getValue();
Locale locale = this.locale.getValue() == null ? Locale.ROOT : this.locale.getValue();
DateFormatter dateTimeFormatter = DateFormatter.forPattern(pattern).withLocale(locale);
return new DateScriptFieldType(name, factory, dateTimeFormatter, script, meta);
}
}

public static RuntimeField sourceOnly(String name, DateFormatter dateTimeFormatter) {
return runtimeField(
name,
(builder, params) -> {
if (DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern().equals(dateTimeFormatter.pattern()) == false) {
builder.field("format", dateTimeFormatter.pattern());
}
return builder;
},
dateTimeFormatter,
DateFieldScript.PARSE_FROM_SOURCE,
DEFAULT_SCRIPT,
Collections.emptyMap());
Builder builder = new Builder(name);
builder.format.setValue(dateTimeFormatter.pattern());
return builder.createRuntimeField(DateFieldScript.PARSE_FROM_SOURCE);
}

private final DateFormatter dateTimeFormatter;
private final DateMathParser dateMathParser;

DateScriptFieldType(
String name,
DateFieldScript.Factory scriptFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.index.fielddata.DoubleScriptFieldData;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand All @@ -29,37 +28,29 @@

import java.time.ZoneId;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Supplier;

public final class DoubleScriptFieldType extends AbstractScriptFieldType<DoubleFieldScript.LeafFactory> {

public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(name ->
new Builder<>(name, DoubleFieldScript.CONTEXT, DoubleFieldScript.PARSE_FROM_SOURCE) {
@Override
RuntimeField newRuntimeField(DoubleFieldScript.Factory scriptFactory) {
return runtimeField(name, this, scriptFactory, getScript(), meta());
}
});
public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(Builder::new);

private static RuntimeField runtimeField(
String name,
ToXContent toXContent,
DoubleFieldScript.Factory scriptFactory,
Script script,
Map<String, String> meta
) {
return new LeafRuntimeField(name, new DoubleScriptFieldType(name, scriptFactory, script, meta), toXContent);
private static class Builder extends AbstractScriptFieldType.Builder<DoubleFieldScript.Factory> {
Builder(String name) {
super(name, DoubleFieldScript.CONTEXT, DoubleFieldScript.PARSE_FROM_SOURCE);
}

@Override
AbstractScriptFieldType<?> createFieldType(String name,
DoubleFieldScript.Factory factory,
Script script,
Map<String, String> meta) {
return new DoubleScriptFieldType(name, factory, script, meta);
}
}

public static RuntimeField sourceOnly(String name) {
return runtimeField(
name,
(builder, params) -> builder,
DoubleFieldScript.PARSE_FROM_SOURCE,
DEFAULT_SCRIPT,
Collections.emptyMap());
return new Builder(name).createRuntimeField(DoubleFieldScript.PARSE_FROM_SOURCE);
}

DoubleScriptFieldType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public final class GeoPointScriptFieldType extends AbstractScriptFieldType<GeoPo
public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(name ->
new Builder<>(name, GeoPointFieldScript.CONTEXT, GeoPointFieldScript.PARSE_FROM_SOURCE) {
@Override
RuntimeField newRuntimeField(GeoPointFieldScript.Factory scriptFactory) {
return new LeafRuntimeField(name, new GeoPointScriptFieldType(name, scriptFactory, getScript(), meta()), this);
AbstractScriptFieldType<?> createFieldType(String name,
GeoPointFieldScript.Factory factory,
Script script,
Map<String, String> meta) {
return new GeoPointScriptFieldType(name, factory, getScript(), meta());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public final class IpScriptFieldType extends AbstractScriptFieldType<IpFieldScri
public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(name ->
new Builder<>(name, IpFieldScript.CONTEXT, IpFieldScript.PARSE_FROM_SOURCE) {
@Override
RuntimeField newRuntimeField(IpFieldScript.Factory scriptFactory) {
return new LeafRuntimeField(name, new IpScriptFieldType(name, scriptFactory, getScript(), meta()), this);
AbstractScriptFieldType<?> createFieldType(String name,
IpFieldScript.Factory factory,
Script script,
Map<String, String> meta) {
return new IpScriptFieldType(name, factory, getScript(), meta());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.index.fielddata.StringScriptFieldData;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.script.Script;
Expand All @@ -31,7 +30,6 @@

import java.time.ZoneId;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -41,31 +39,24 @@

public final class KeywordScriptFieldType extends AbstractScriptFieldType<StringFieldScript.LeafFactory> {

public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(name ->
new Builder<>(name, StringFieldScript.CONTEXT, StringFieldScript.PARSE_FROM_SOURCE) {
@Override
RuntimeField newRuntimeField(StringFieldScript.Factory scriptFactory) {
return runtimeField(name, this, scriptFactory, getScript(), meta());
}
});
public static final RuntimeField.Parser PARSER = new RuntimeField.Parser(Builder::new);

private static RuntimeField runtimeField(
String name,
ToXContent toXContent,
StringFieldScript.Factory scriptFactory,
Script script,
Map<String, String> meta
) {
return new LeafRuntimeField(name, new KeywordScriptFieldType(name, scriptFactory, script, meta), toXContent);
private static class Builder extends AbstractScriptFieldType.Builder<StringFieldScript.Factory> {
Builder(String name) {
super(name, StringFieldScript.CONTEXT, StringFieldScript.PARSE_FROM_SOURCE);
}

@Override
AbstractScriptFieldType<?> createFieldType(String name,
StringFieldScript.Factory factory,
Script script,
Map<String, String> meta) {
return new KeywordScriptFieldType(name, factory, script, meta);
}
}

public static RuntimeField sourceOnly(String name) {
return runtimeField(
name,
(builder, params) -> builder,
StringFieldScript.PARSE_FROM_SOURCE,
DEFAULT_SCRIPT,
Collections.emptyMap());
return new Builder(name).createRuntimeField(StringFieldScript.PARSE_FROM_SOURCE);
}

public KeywordScriptFieldType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
* a single MappedFieldType from {@link RuntimeField#asMappedFieldTypes()}
*/
public final class LeafRuntimeField implements RuntimeField {

protected final String name;
protected final ToXContent toXContent;
protected final MappedFieldType mappedFieldType;
private final String name;
private final ToXContent toXContent;
private final MappedFieldType mappedFieldType;

public LeafRuntimeField(String name, MappedFieldType mappedFieldType, ToXContent toXContent) {
this.name = name;
Expand Down
Loading