add lang parameter to all script constructs, allowing for (later) custom script languages

This commit is contained in:
kimchy 2010-10-01 22:14:20 +02:00
parent 99fcfde307
commit aa116e5d40
17 changed files with 63 additions and 28 deletions

View File

@ -75,9 +75,9 @@ public class ScriptFieldsFunction implements FieldsFunction {
final SourceMap sourceMap; final SourceMap sourceMap;
public ScriptFieldsFunction(String script, ScriptService scriptService, MapperService mapperService, FieldDataCache fieldDataCache) { public ScriptFieldsFunction(String scriptLang, String script, ScriptService scriptService, MapperService mapperService, FieldDataCache fieldDataCache) {
this.scriptService = scriptService; this.scriptService = scriptService;
this.script = scriptService.compile(script); this.script = scriptService.compile(scriptLang, script);
this.docMap = new DocMap(cachedFieldData.get().get(), mapperService, fieldDataCache); this.docMap = new DocMap(cachedFieldData.get().get(), mapperService, fieldDataCache);
this.sourceMap = new SourceMap(); this.sourceMap = new SourceMap();
} }

View File

@ -60,6 +60,7 @@ public class CustomScoreQueryParser extends AbstractIndexComponent implements XC
Query query = null; Query query = null;
float boost = 1.0f; float boost = 1.0f;
String script = null; String script = null;
String scriptLang = null;
Map<String, Object> vars = null; Map<String, Object> vars = null;
String currentFieldName = null; String currentFieldName = null;
@ -76,6 +77,8 @@ public class CustomScoreQueryParser extends AbstractIndexComponent implements XC
} else if (token.isValue()) { } else if (token.isValue()) {
if ("script".equals(currentFieldName)) { if ("script".equals(currentFieldName)) {
script = parser.text(); script = parser.text();
} else if ("lang".equals(currentFieldName)) {
scriptLang = parser.text();
} else if ("boost".equals(currentFieldName)) { } else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue(); boost = parser.floatValue();
} }
@ -88,7 +91,7 @@ public class CustomScoreQueryParser extends AbstractIndexComponent implements XC
throw new QueryParsingException(index, "[custom_score] requires 'script' field"); throw new QueryParsingException(index, "[custom_score] requires 'script' field");
} }
FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query,
new ScriptScoreFunction(new ScriptFieldsFunction(script, parseContext.scriptService(), parseContext.mapperService(), parseContext.indexCache().fieldData()), vars)); new ScriptScoreFunction(new ScriptFieldsFunction(scriptLang, script, parseContext.scriptService(), parseContext.mapperService(), parseContext.indexCache().fieldData()), vars));
functionScoreQuery.setBoost(boost); functionScoreQuery.setBoost(boost);
return functionScoreQuery; return functionScoreQuery;
} }

View File

@ -60,6 +60,7 @@ public class ScriptFilterParser extends AbstractIndexComponent implements XConte
XContentParser.Token token; XContentParser.Token token;
String script = null; String script = null;
String scriptLang = null;
Map<String, Object> params = null; Map<String, Object> params = null;
String filterName = null; String filterName = null;
@ -74,6 +75,8 @@ public class ScriptFilterParser extends AbstractIndexComponent implements XConte
} else if (token.isValue()) { } else if (token.isValue()) {
if ("script".equals(currentFieldName)) { if ("script".equals(currentFieldName)) {
script = parser.text(); script = parser.text();
} else if ("lang".equals(currentFieldName)) {
scriptLang = parser.text();
} else if ("_name".equals(currentFieldName)) { } else if ("_name".equals(currentFieldName)) {
filterName = parser.text(); filterName = parser.text();
} }
@ -87,7 +90,7 @@ public class ScriptFilterParser extends AbstractIndexComponent implements XConte
params = Maps.newHashMap(); params = Maps.newHashMap();
} }
Filter filter = new ScriptFilter(script, params, parseContext.mapperService(), parseContext.indexCache().fieldData(), parseContext.scriptService()); Filter filter = new ScriptFilter(scriptLang, script, params, parseContext.mapperService(), parseContext.indexCache().fieldData(), parseContext.scriptService());
if (filterName != null) { if (filterName != null) {
parseContext.addNamedFilter(filterName, filter); parseContext.addNamedFilter(filterName, filter);
} }
@ -96,6 +99,8 @@ public class ScriptFilterParser extends AbstractIndexComponent implements XConte
public static class ScriptFilter extends Filter { public static class ScriptFilter extends Filter {
private final String scriptLang;
private final String script; private final String script;
private final Map<String, Object> params; private final Map<String, Object> params;
@ -106,8 +111,9 @@ public class ScriptFilterParser extends AbstractIndexComponent implements XConte
private final ScriptService scriptService; private final ScriptService scriptService;
private ScriptFilter(String script, Map<String, Object> params, private ScriptFilter(String scriptLang, String script, Map<String, Object> params,
MapperService mapperService, FieldDataCache fieldDataCache, ScriptService scriptService) { MapperService mapperService, FieldDataCache fieldDataCache, ScriptService scriptService) {
this.scriptLang = scriptLang;
this.script = script; this.script = script;
this.params = params; this.params = params;
this.mapperService = mapperService; this.mapperService = mapperService;
@ -142,7 +148,7 @@ public class ScriptFilterParser extends AbstractIndexComponent implements XConte
} }
@Override public DocIdSet getDocIdSet(final IndexReader reader) throws IOException { @Override public DocIdSet getDocIdSet(final IndexReader reader) throws IOException {
final ScriptFieldsFunction function = new ScriptFieldsFunction(script, scriptService, mapperService, fieldDataCache); final ScriptFieldsFunction function = new ScriptFieldsFunction(scriptLang, script, scriptService, mapperService, fieldDataCache);
function.setNextReader(reader); function.setNextReader(reader);
return new GetDocSet(reader.maxDoc()) { return new GetDocSet(reader.maxDoc()) {
@Override public boolean isCacheable() { @Override public boolean isCacheable() {

View File

@ -71,6 +71,9 @@ public class ScriptService extends AbstractComponent {
if (compiled != null) { if (compiled != null) {
return compiled; return compiled;
} }
if (type == null) {
type = defaultType;
}
synchronized (cache) { synchronized (cache) {
compiled = cache.get(script); compiled = cache.get(script);
if (compiled != null) { if (compiled != null) {

View File

@ -57,6 +57,7 @@ public class GeoDistanceFacetCollectorParser implements FacetCollectorParser {
String fieldName = null; String fieldName = null;
String valueFieldName = null; String valueFieldName = null;
String valueScript = null; String valueScript = null;
String scriptLang = null;
Map<String, Object> params = null; Map<String, Object> params = null;
double lat = Double.NaN; double lat = Double.NaN;
double lon = Double.NaN; double lon = Double.NaN;
@ -133,6 +134,8 @@ public class GeoDistanceFacetCollectorParser implements FacetCollectorParser {
valueFieldName = parser.text(); valueFieldName = parser.text();
} else if ("value_script".equals(currentName) || "valueScript".equals(currentName)) { } else if ("value_script".equals(currentName) || "valueScript".equals(currentName)) {
valueScript = parser.text(); valueScript = parser.text();
} else if ("lang".equals(currentName)) {
scriptLang = parser.text();
} else { } else {
// assume the value is the actual value // assume the value is the actual value
String value = parser.text(); String value = parser.text();
@ -166,7 +169,7 @@ public class GeoDistanceFacetCollectorParser implements FacetCollectorParser {
if (valueScript != null) { if (valueScript != null) {
return new ScriptGeoDistanceFacetCollector(facetName, fieldName, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]), return new ScriptGeoDistanceFacetCollector(facetName, fieldName, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
context, valueScript, params); context, scriptLang, valueScript, params);
} }
return new GeoDistanceFacetCollector(facetName, fieldName, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]), return new GeoDistanceFacetCollector(facetName, fieldName, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),

View File

@ -40,11 +40,11 @@ public class ScriptGeoDistanceFacetCollector extends GeoDistanceFacetCollector {
public ScriptGeoDistanceFacetCollector(String facetName, String fieldName, double lat, double lon, DistanceUnit unit, GeoDistance geoDistance, public ScriptGeoDistanceFacetCollector(String facetName, String fieldName, double lat, double lon, DistanceUnit unit, GeoDistance geoDistance,
GeoDistanceFacet.Entry[] entries, SearchContext context, GeoDistanceFacet.Entry[] entries, SearchContext context,
String script, Map<String, Object> params) { String scriptLang, String script, Map<String, Object> params) {
super(facetName, fieldName, lat, lon, unit, geoDistance, entries, context); super(facetName, fieldName, lat, lon, unit, geoDistance, entries, context);
this.params = params; this.params = params;
this.valueFunction = new ScriptFieldsFunction(script, context.scriptService(), context.mapperService(), context.fieldDataCache()); this.valueFunction = new ScriptFieldsFunction(scriptLang, script, context.scriptService(), context.mapperService(), context.fieldDataCache());
} }
@Override protected void doSetNextReader(IndexReader reader, int docBase) throws IOException { @Override protected void doSetNextReader(IndexReader reader, int docBase) throws IOException {

View File

@ -45,6 +45,7 @@ public class HistogramFacetCollectorParser implements FacetCollectorParser {
String valueField = null; String valueField = null;
String keyScript = null; String keyScript = null;
String valueScript = null; String valueScript = null;
String scriptLang = null;
Map<String, Object> params = null; Map<String, Object> params = null;
long interval = 0; long interval = 0;
HistogramFacet.ComparatorType comparatorType = HistogramFacet.ComparatorType.KEY; HistogramFacet.ComparatorType comparatorType = HistogramFacet.ComparatorType.KEY;
@ -74,12 +75,14 @@ public class HistogramFacetCollectorParser implements FacetCollectorParser {
valueScript = parser.text(); valueScript = parser.text();
} else if ("order".equals(fieldName) || "comparator".equals(fieldName)) { } else if ("order".equals(fieldName) || "comparator".equals(fieldName)) {
comparatorType = HistogramFacet.ComparatorType.fromString(parser.text()); comparatorType = HistogramFacet.ComparatorType.fromString(parser.text());
} else if ("lang".equals(fieldName)) {
scriptLang = parser.text();
} }
} }
} }
if (keyScript != null && valueScript != null) { if (keyScript != null && valueScript != null) {
return new ScriptHistogramFacetCollector(facetName, keyScript, valueScript, params, interval, comparatorType, context); return new ScriptHistogramFacetCollector(facetName, scriptLang, keyScript, valueScript, params, interval, comparatorType, context);
} }
if (keyField == null) { if (keyField == null) {

View File

@ -50,10 +50,10 @@ public class ScriptHistogramFacetCollector extends AbstractFacetCollector {
private final TLongDoubleHashMap totals = new TLongDoubleHashMap(); private final TLongDoubleHashMap totals = new TLongDoubleHashMap();
public ScriptHistogramFacetCollector(String facetName, String keyScript, String valueScript, Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType, SearchContext context) { public ScriptHistogramFacetCollector(String facetName, String scriptLang, String keyScript, String valueScript, Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType, SearchContext context) {
super(facetName); super(facetName);
this.keyFunction = new ScriptFieldsFunction(keyScript, context.scriptService(), context.mapperService(), context.fieldDataCache()); this.keyFunction = new ScriptFieldsFunction(scriptLang, keyScript, context.scriptService(), context.mapperService(), context.fieldDataCache());
this.valueFunction = new ScriptFieldsFunction(valueScript, context.scriptService(), context.mapperService(), context.fieldDataCache()); this.valueFunction = new ScriptFieldsFunction(scriptLang, valueScript, context.scriptService(), context.mapperService(), context.fieldDataCache());
this.interval = interval > 0 ? interval : 0; this.interval = interval > 0 ? interval : 0;
this.params = params; this.params = params;
this.comparatorType = comparatorType; this.comparatorType = comparatorType;

View File

@ -45,6 +45,7 @@ public class RangeFacetCollectorParser implements FacetCollectorParser {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException { @Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
String keyField = null; String keyField = null;
String valueField = null; String valueField = null;
String scriptLang = null;
String keyScript = null; String keyScript = null;
String valueScript = null; String valueScript = null;
Map<String, Object> params = null; Map<String, Object> params = null;
@ -96,6 +97,8 @@ public class RangeFacetCollectorParser implements FacetCollectorParser {
keyScript = parser.text(); keyScript = parser.text();
} else if ("value_script".equals(fieldName) || "valueScript".equals(fieldName)) { } else if ("value_script".equals(fieldName) || "valueScript".equals(fieldName)) {
valueScript = parser.text(); valueScript = parser.text();
} else if ("lang".equals(fieldName)) {
scriptLang = parser.text();
} }
} }
} }
@ -123,7 +126,7 @@ public class RangeFacetCollectorParser implements FacetCollectorParser {
} }
if (keyScript != null && valueScript != null) { if (keyScript != null && valueScript != null) {
return new ScriptRangeFacetCollector(facetName, keyScript, valueScript, params, rangeEntries, context); return new ScriptRangeFacetCollector(facetName, scriptLang, keyScript, valueScript, params, rangeEntries, context);
} }
if (keyField == null) { if (keyField == null) {

View File

@ -42,10 +42,10 @@ public class ScriptRangeFacetCollector extends AbstractFacetCollector {
private final RangeFacet.Entry[] entries; private final RangeFacet.Entry[] entries;
public ScriptRangeFacetCollector(String facetName, String keyScript, String valueScript, Map<String, Object> params, RangeFacet.Entry[] entries, SearchContext context) { public ScriptRangeFacetCollector(String facetName, String scriptLang, String keyScript, String valueScript, Map<String, Object> params, RangeFacet.Entry[] entries, SearchContext context) {
super(facetName); super(facetName);
this.keyFunction = new ScriptFieldsFunction(keyScript, context.scriptService(), context.mapperService(), context.fieldDataCache()); this.keyFunction = new ScriptFieldsFunction(scriptLang, keyScript, context.scriptService(), context.mapperService(), context.fieldDataCache());
this.valueFunction = new ScriptFieldsFunction(valueScript, context.scriptService(), context.mapperService(), context.fieldDataCache()); this.valueFunction = new ScriptFieldsFunction(scriptLang, valueScript, context.scriptService(), context.mapperService(), context.fieldDataCache());
this.params = params; this.params = params;
this.entries = entries; this.entries = entries;
} }

View File

@ -48,10 +48,10 @@ public class ScriptStatisticalFacetCollector extends AbstractFacetCollector {
private long count; private long count;
public ScriptStatisticalFacetCollector(String facetName, String script, Map<String, Object> params, SearchContext context) { public ScriptStatisticalFacetCollector(String facetName, String scriptLang, String script, Map<String, Object> params, SearchContext context) {
super(facetName); super(facetName);
this.params = params; this.params = params;
this.function = new ScriptFieldsFunction(script, context.scriptService(), context.mapperService(), context.fieldDataCache()); this.function = new ScriptFieldsFunction(scriptLang, script, context.scriptService(), context.mapperService(), context.fieldDataCache());
} }
@Override protected void doCollect(int doc) throws IOException { @Override protected void doCollect(int doc) throws IOException {

View File

@ -52,6 +52,7 @@ public class StatisticalFacetCollectorParser implements FacetCollectorParser {
String currentFieldName = null; String currentFieldName = null;
String script = null; String script = null;
String scriptLang = null;
Map<String, Object> params = cachedParams.get().get(); Map<String, Object> params = cachedParams.get().get();
params.clear(); params.clear();
XContentParser.Token token; XContentParser.Token token;
@ -67,6 +68,8 @@ public class StatisticalFacetCollectorParser implements FacetCollectorParser {
field = parser.text(); field = parser.text();
} else if ("script".equals(currentFieldName)) { } else if ("script".equals(currentFieldName)) {
script = parser.text(); script = parser.text();
} else if ("lang".equals(currentFieldName)) {
scriptLang = parser.text();
} }
} }
} }
@ -76,7 +79,7 @@ public class StatisticalFacetCollectorParser implements FacetCollectorParser {
if (field != null) { if (field != null) {
return new StatisticalFacetCollector(facetName, field, context); return new StatisticalFacetCollector(facetName, field, context);
} else { } else {
return new ScriptStatisticalFacetCollector(facetName, script, params, context); return new ScriptStatisticalFacetCollector(facetName, scriptLang, script, params, context);
} }
} }
} }

View File

@ -76,7 +76,7 @@ public class TermsFacetCollector extends AbstractFacetCollector {
private final FieldsFunction scriptFunction; private final FieldsFunction scriptFunction;
public TermsFacetCollector(String facetName, String fieldName, int size, InternalTermsFacet.ComparatorType comparatorType, SearchContext context, public TermsFacetCollector(String facetName, String fieldName, int size, InternalTermsFacet.ComparatorType comparatorType, SearchContext context,
ImmutableSet<String> excluded, Pattern pattern, String script, Map<String, Object> params) { ImmutableSet<String> excluded, Pattern pattern, String scriptLang, String script, Map<String, Object> params) {
super(facetName); super(facetName);
this.fieldDataCache = context.fieldDataCache(); this.fieldDataCache = context.fieldDataCache();
this.size = size; this.size = size;
@ -100,7 +100,7 @@ public class TermsFacetCollector extends AbstractFacetCollector {
} }
if (script != null) { if (script != null) {
scriptFunction = new ScriptFieldsFunction(script, context.scriptService(), context.mapperService(), fieldDataCache); scriptFunction = new ScriptFieldsFunction(scriptLang, script, context.scriptService(), context.mapperService(), fieldDataCache);
if (params == null) { if (params == null) {
params = Maps.newHashMapWithExpectedSize(1); params = Maps.newHashMapWithExpectedSize(1);
} }

View File

@ -51,6 +51,7 @@ public class TermsFacetCollectorParser implements FacetCollectorParser {
String regex = null; String regex = null;
String regexFlags = null; String regexFlags = null;
TermsFacet.ComparatorType comparatorType = TermsFacet.ComparatorType.COUNT; TermsFacet.ComparatorType comparatorType = TermsFacet.ComparatorType.COUNT;
String scriptLang = null;
String script = null; String script = null;
Map<String, Object> params = null; Map<String, Object> params = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
@ -81,6 +82,8 @@ public class TermsFacetCollectorParser implements FacetCollectorParser {
comparatorType = TermsFacet.ComparatorType.fromString(parser.text()); comparatorType = TermsFacet.ComparatorType.fromString(parser.text());
} else if ("script".equals(fieldName)) { } else if ("script".equals(fieldName)) {
script = parser.text(); script = parser.text();
} else if ("lang".equals(fieldName)) {
scriptLang = parser.text();
} }
} }
} }
@ -93,6 +96,6 @@ public class TermsFacetCollectorParser implements FacetCollectorParser {
if (regex != null) { if (regex != null) {
pattern = Regex.compile(regex, regexFlags); pattern = Regex.compile(regex, regexFlags);
} }
return new TermsFacetCollector(facetName, field, size, comparatorType, context, excluded, pattern, script, params); return new TermsFacetCollector(facetName, field, size, comparatorType, context, excluded, pattern, scriptLang, script, params);
} }
} }

View File

@ -39,7 +39,7 @@ public class FieldsParseElement implements SearchParseElement {
String name = parser.text(); String name = parser.text();
if (name.contains("_source.") || name.contains("doc[")) { if (name.contains("_source.") || name.contains("doc[")) {
// script field to load from source // script field to load from source
context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, new ScriptFieldsFunction(name, context.scriptService(), context.mapperService(), context.fieldDataCache()), null)); context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, new ScriptFieldsFunction(null, name, context.scriptService(), context.mapperService(), context.fieldDataCache()), null));
} else { } else {
context.fieldNames().add(name); context.fieldNames().add(name);
} }
@ -51,7 +51,7 @@ public class FieldsParseElement implements SearchParseElement {
String name = parser.text(); String name = parser.text();
if (name.contains("_source.") || name.contains("doc[")) { if (name.contains("_source.") || name.contains("doc[")) {
// script field to load from source // script field to load from source
context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, new ScriptFieldsFunction(name, context.scriptService(), context.mapperService(), context.fieldDataCache()), null)); context.scriptFields().add(new ScriptFieldsContext.ScriptField(name, new ScriptFieldsFunction(null, name, context.scriptService(), context.mapperService(), context.fieldDataCache()), null));
} else { } else {
context.fieldNames().add(name); context.fieldNames().add(name);
} }

View File

@ -51,6 +51,7 @@ public class ScriptFieldsParseElement implements SearchParseElement {
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
String fieldName = currentFieldName; String fieldName = currentFieldName;
String script = null; String script = null;
String scriptLang = null;
Map<String, Object> params = null; Map<String, Object> params = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
@ -58,10 +59,14 @@ public class ScriptFieldsParseElement implements SearchParseElement {
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
params = parser.map(); params = parser.map();
} else if (token.isValue()) { } else if (token.isValue()) {
script = parser.text(); if ("script".equals(currentFieldName)) {
script = parser.text();
} else if ("lang".equals(currentFieldName)) {
scriptLang = parser.text();
}
} }
} }
context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, new ScriptFieldsFunction(script, context.scriptService(), context.mapperService(), context.fieldDataCache()), params)); context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, new ScriptFieldsFunction(scriptLang, script, context.scriptService(), context.mapperService(), context.fieldDataCache()), params));
} }
} }
} }

View File

@ -42,6 +42,7 @@ public class ScriptSortParser implements SortParser {
@Override public SortField parse(XContentParser parser, SearchContext context) throws Exception { @Override public SortField parse(XContentParser parser, SearchContext context) throws Exception {
String script = null; String script = null;
String scriptLang = null;
String type = null; String type = null;
Map<String, Object> params = null; Map<String, Object> params = null;
boolean reverse = false; boolean reverse = false;
@ -62,6 +63,8 @@ public class ScriptSortParser implements SortParser {
type = parser.text(); type = parser.text();
} else if ("params".equals(currentName)) { } else if ("params".equals(currentName)) {
params = parser.map(); params = parser.map();
} else if ("lang".equals(currentName)) {
scriptLang = parser.text();
} }
} }
} }
@ -72,7 +75,7 @@ public class ScriptSortParser implements SortParser {
if (type == null) { if (type == null) {
throw new SearchParseException(context, "_script sorting requires setting the type of the script"); throw new SearchParseException(context, "_script sorting requires setting the type of the script");
} }
FieldsFunction fieldsFunction = new ScriptFieldsFunction(script, context.scriptService(), context.mapperService(), context.fieldDataCache()); FieldsFunction fieldsFunction = new ScriptFieldsFunction(scriptLang, script, context.scriptService(), context.mapperService(), context.fieldDataCache());
FieldComparatorSource fieldComparatorSource; FieldComparatorSource fieldComparatorSource;
if ("string".equals(type)) { if ("string".equals(type)) {
fieldComparatorSource = StringFieldsFunctionDataComparator.comparatorSource(fieldsFunction, params); fieldComparatorSource = StringFieldsFunctionDataComparator.comparatorSource(fieldsFunction, params);