Remove deprecated script APIs

The script APIs have been deprecated long ago we can now remove them.
This commit still keeps the parsing code since it might be used in a
query that is still stuck in transaction log. This issue should be discussed
elsewhere.

Closes #11619
This commit is contained in:
Simon Willnauer 2015-06-12 12:43:51 +02:00
parent e34cb18f8a
commit 09a30e7eee
53 changed files with 109 additions and 7989 deletions

View File

@ -165,12 +165,6 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleCustomO
}
return defaultValue;
}
@Override
@Deprecated
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
return paramAsBoolean(key, defaultValue);
}
};
private ImmutableMap<String, FieldMappingMetaData> findFieldMappingsByType(DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) {

View File

@ -430,60 +430,6 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
return this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param script
* The script to use
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public SearchRequestBuilder addScriptField(String name, String script) {
sourceBuilder().scriptField(name, script);
return this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param script
* The script to use
* @param params
* Parameters that the script can use.
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public SearchRequestBuilder addScriptField(String name, String script, Map<String, Object> params) {
sourceBuilder().scriptField(name, script, params);
return this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param lang
* The language of the script
* @param script
* The script to use
* @param params
* Parameters that the script can use (can be <tt>null</tt>).
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public SearchRequestBuilder addScriptField(String name, String lang, String script, Map<String, Object> params) {
sourceBuilder().scriptField(name, lang, script, params);
return this;
}
/**
* Adds a sort against the given field name and the sort ordering.
*

View File

@ -28,7 +28,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import java.util.Map;
@ -87,64 +86,6 @@ public class UpdateRequestBuilder extends InstanceShardOperationRequestBuilder<U
return this;
}
/**
* The script to execute. Note, make sure not to send different script each
* times and instead use script params if possible with the same
* (automatically compiled) script.
* <p/>
* The script works with the variable <code>ctx</code>, which is bound to
* the entry, e.g. <code>ctx._source.mycounter += 1</code>.
*
* @see #setScriptLang(String)
* @see #setScriptParams(Map)
*
* @deprecated use {@link #setScript(Script)} instead
*/
@Deprecated
public UpdateRequestBuilder setScript(String script, ScriptService.ScriptType scriptType) {
request.script(script, scriptType);
return this;
}
/**
* The language of the script to execute. Valid options are: mvel, js,
* groovy, python, and native (Java)<br>
* Default: groovy
* <p/>
* Ref:
* http://www.elasticsearch.org/guide/en/elasticsearch/reference/current
* /modules-scripting.html
*
* @deprecated use {@link #setScript(Script)} instead
*/
@Deprecated
public UpdateRequestBuilder setScriptLang(String scriptLang) {
request.scriptLang(scriptLang);
return this;
}
/**
* Sets the script parameters to use with the script.
*
* @deprecated use {@link #setScript(Script)} instead
*/
@Deprecated
public UpdateRequestBuilder setScriptParams(Map<String, Object> scriptParams) {
request.scriptParams(scriptParams);
return this;
}
/**
* Add a script parameter.
*
* @deprecated use {@link #setScript(Script)} instead
*/
@Deprecated
public UpdateRequestBuilder addScriptParam(String name, Object value) {
request.addScriptParam(name, value);
return this;
}
/**
* Explicitly specify the fields that will be returned. By default, nothing is returned.
*/

View File

@ -37,13 +37,6 @@ public interface ToXContent {
boolean paramAsBoolean(String key, boolean defaultValue);
Boolean paramAsBoolean(String key, Boolean defaultValue);
/**
* @deprecated since 1.0.0
* use {@link ToXContent.Params#paramAsBoolean(String, Boolean)} instead
*/
@Deprecated
Boolean paramAsBooleanOptional(String key, Boolean defaultValue);
}
public static final Params EMPTY_PARAMS = new Params() {
@ -67,10 +60,6 @@ public interface ToXContent {
return defaultValue;
}
@Override @Deprecated
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
return paramAsBoolean(key, defaultValue);
}
};
public static class MapParams implements Params {
@ -104,11 +93,6 @@ public interface ToXContent {
public Boolean paramAsBoolean(String key, Boolean defaultValue) {
return Booleans.parseBoolean(param(key), defaultValue);
}
@Override @Deprecated
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
return paramAsBoolean(key, defaultValue);
}
}
public static class DelegatingMapParams extends MapParams {
@ -139,11 +123,6 @@ public interface ToXContent {
public Boolean paramAsBoolean(String key, Boolean defaultValue) {
return super.paramAsBoolean(key, delegate.paramAsBoolean(key, defaultValue));
}
@Override @Deprecated
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
return super.paramAsBooleanOptional(key, delegate.paramAsBooleanOptional(key, defaultValue));
}
}
XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException;

View File

@ -609,17 +609,6 @@ public abstract class QueryBuilders {
return new ScriptQueryBuilder(script);
}
/**
* A builder for filter based on a script.
*
* @param script
* The script to filter by.
* @deprecated Use {@link #scriptQuery(Script)} instead.
*/
@Deprecated
public static ScriptQueryBuilder scriptQuery(String script) {
return new ScriptQueryBuilder(script);
}
/**
* A filter to filter based on a specific distance from a specific geo location / point.

View File

@ -31,65 +31,12 @@ public class ScriptQueryBuilder extends QueryBuilder {
private Script script;
@Deprecated
private String scriptString;
@Deprecated
private Map<String, Object> params;
@Deprecated
private String lang;
private String queryName;
public ScriptQueryBuilder(Script script) {
this.script = script;
}
/**
* @deprecated Use {@link #ScriptQueryBuilder(Script)} instead.
*/
@Deprecated
public ScriptQueryBuilder(String script) {
this.scriptString = script;
}
/**
* @deprecated Use {@link #ScriptQueryBuilder(Script)} instead.
*/
@Deprecated
public ScriptQueryBuilder addParam(String name, Object value) {
if (params == null) {
params = new HashMap<>();
}
params.put(name, value);
return this;
}
/**
* @deprecated Use {@link #ScriptQueryBuilder(Script)} instead.
*/
@Deprecated
public ScriptQueryBuilder params(Map<String, Object> params) {
if (this.params == null) {
this.params = params;
} else {
this.params.putAll(params);
}
return this;
}
/**
* Sets the script language.
*
* @deprecated Use {@link #ScriptQueryBuilder(Script)} instead.
*/
@Deprecated
public ScriptQueryBuilder lang(String lang) {
this.lang = lang;
return this;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
@ -102,20 +49,7 @@ public class ScriptQueryBuilder extends QueryBuilder {
protected void doXContent(XContentBuilder builder, Params builderParams) throws IOException {
builder.startObject(ScriptQueryParser.NAME);
if (script != null) {
builder.field(ScriptField.SCRIPT.getPreferredName(), script);
} else {
if (this.scriptString != null) {
builder.field("script", scriptString);
}
if (this.params != null) {
builder.field("params", this.params);
}
if (this.lang != null) {
builder.field("lang", lang);
}
}
builder.field(ScriptField.SCRIPT.getPreferredName(), script);
if (queryName != null) {
builder.field("_name", queryName);
}

View File

@ -58,44 +58,11 @@ public class ScoreFunctionBuilders {
}
public static ScriptScoreFunctionBuilder scriptFunction(Script script) {
return (new ScriptScoreFunctionBuilder()).script(script);
return (new ScriptScoreFunctionBuilder(script));
}
/**
* @deprecated Use {@link #scriptFunction(Script)} instead.
*/
@Deprecated
public static ScriptScoreFunctionBuilder scriptFunction(String script) {
return (new ScriptScoreFunctionBuilder()).script(script);
}
/**
* @deprecated Use {@link #scriptFunction(Script)} instead.
*/
@Deprecated
public static ScriptScoreFunctionBuilder scriptFunction(String script, String lang) {
return (new ScriptScoreFunctionBuilder()).script(script).lang(lang);
}
/**
* @deprecated Use {@link #scriptFunction(Script)} instead.
*/
@Deprecated
public static ScriptScoreFunctionBuilder scriptFunction(String script, String lang, Map<String, Object> params) {
return (new ScriptScoreFunctionBuilder()).script(script).lang(lang).params(params);
}
/**
* @deprecated Use {@link #scriptFunction(Script)} instead.
*/
@Deprecated
public static ScriptScoreFunctionBuilder scriptFunction(String script, Map<String, Object> params) {
return (new ScriptScoreFunctionBuilder()).script(script).params(params);
}
@Deprecated
public static FactorBuilder factorFunction(float boost) {
return (new FactorBuilder()).boostFactor(boost);
return (new ScriptScoreFunctionBuilder(new Script(script)));
}
public static RandomScoreFunctionBuilder randomFunction(int seed) {

View File

@ -34,85 +34,19 @@ import java.util.Map;
*/
public class ScriptScoreFunctionBuilder extends ScoreFunctionBuilder {
private Script script;
private final Script script;
private String scriptString;
private String lang;
private Map<String, Object> params = null;
public ScriptScoreFunctionBuilder() {
}
public ScriptScoreFunctionBuilder script(Script script) {
public ScriptScoreFunctionBuilder(Script script) {
if (script == null) {
throw new IllegalArgumentException("script must not be null");
}
this.script = script;
return this;
}
/**
* @deprecated Use {@link #script(Script)} instead
*/
@Deprecated
public ScriptScoreFunctionBuilder script(String script) {
this.scriptString = script;
return this;
}
/**
* Sets the language of the script.@deprecated Use {@link #script(Script)}
* instead
*/
@Deprecated
public ScriptScoreFunctionBuilder lang(String lang) {
this.lang = lang;
return this;
}
/**
* Additional parameters that can be provided to the script.@deprecated Use
* {@link #script(Script)} instead
*/
@Deprecated
public ScriptScoreFunctionBuilder params(Map<String, Object> params) {
if (this.params == null) {
this.params = params;
} else {
this.params.putAll(params);
}
return this;
}
/**
* Additional parameters that can be provided to the script.@deprecated Use
* {@link #script(Script)} instead
*/
@Deprecated
public ScriptScoreFunctionBuilder param(String key, Object value) {
if (params == null) {
params = new HashMap<>();
}
params.put(key, value);
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(getName());
if (script != null) {
builder.field(ScriptField.SCRIPT.getPreferredName(), script);
} else {
if (scriptString != null) {
builder.field("script", scriptString);
}
if (lang != null) {
builder.field("lang", lang);
}
if (this.params != null) {
builder.field("params", this.params);
}
}
builder.field(ScriptField.SCRIPT.getPreferredName(), script);
builder.endObject();
}

View File

@ -154,60 +154,6 @@ public abstract class BaseInnerHitBuilder<T extends BaseInnerHitBuilder> impleme
return (T) this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param script
* The script to use
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public T addScriptField(String name, String script) {
sourceBuilder().scriptField(name, script);
return (T) this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param script
* The script to use
* @param params
* Parameters that the script can use.
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public T addScriptField(String name, String script, Map<String, Object> params) {
sourceBuilder().scriptField(name, script, params);
return (T) this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param lang
* The language of the script
* @param script
* The script to use
* @param params
* Parameters that the script can use (can be <tt>null</tt>).
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public T addScriptField(String name, String lang, String script, Map<String, Object> params) {
sourceBuilder().scriptField(name, lang, script, params);
return (T) this;
}
/**
* Adds a sort against the given field name and the sort ordering.
*

View File

@ -80,16 +80,6 @@ public class NodeService extends AbstractComponent {
this.httpServer = httpServer;
}
@Deprecated
public void putNodeAttribute(String key, String value) {
putAttribute(key, value);
}
@Deprecated
public void removeNodeAttribute(String key) {
removeAttribute(key);
}
public synchronized void putAttribute(String key, String value) {
serviceAttributes = new MapBuilder<>(serviceAttributes).put(key, value).immutableMap();
}

View File

@ -134,11 +134,6 @@ public abstract class RestRequest extends ContextAndHeaderHolder implements ToXC
return Booleans.parseBoolean(param(key), defaultValue);
}
@Override @Deprecated
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
return paramAsBoolean(key, defaultValue);
}
public TimeValue paramAsTime(String key, TimeValue defaultValue) {
return parseTimeValue(param(key), defaultValue, key);
}

View File

@ -56,26 +56,19 @@ public class Script implements ToXContent, Streamable {
/**
* Constructor for simple inline script. The script will have no lang or
* params set.
*
*
* @param script
* The inline script to execute.
*/
public Script(String script) {
if (script == null) {
throw new IllegalArgumentException("The parameter script (String) must not be null in Script.");
}
this.script = script;
this(script, null);
}
/**
* For sub-classes to use to override the default language
*/
protected Script(String script, String lang) {
if (script == null) {
throw new IllegalArgumentException("The parameter script (String) must not be null in Script.");
}
this.script = script;
this.lang = lang;
this(script, ScriptType.INLINE, lang, null);
}
/**
@ -93,7 +86,7 @@ public class Script implements ToXContent, Streamable {
* @param params
* The map of parameters the script will be executed with.
*/
public Script(String script, ScriptType type, @Nullable String lang, @Nullable Map<String, Object> params) {
public Script(String script, ScriptType type, @Nullable String lang, @Nullable Map<String, ? extends Object> params) {
if (script == null) {
throw new IllegalArgumentException("The parameter script (String) must not be null in Script.");
}
@ -103,7 +96,7 @@ public class Script implements ToXContent, Streamable {
this.script = script;
this.type = type;
this.lang = lang;
this.params = params;
this.params = (Map<String, Object>)params;
}
/**

View File

@ -80,85 +80,6 @@ public abstract class ValuesSourceAggregationBuilder<B extends ValuesSourceAggre
return (B) this;
}
/**
* Sets the script which generates the values. If the script is configured
* along with the field (as in {@link #field(String)}), then this script
* will be treated as a {@code value script}. A <i>value script</i> will be
* applied on the values that are extracted from the field data (you can
* refer to that value in the script using the {@code _value} reserved
* variable). If only the script is configured (and the no field is
* configured next to it), then the script will be responsible to generate
* the values that will be aggregated.
*
* @param script
* The configured script.
* @return This builder (fluent interface support)
* @deprecated Use {@link #script(Script)} instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public B script(String script) {
this.scriptString = script;
return (B) this;
}
/**
* Sets the language of the script (if one is defined).
* <p/>
* Also see {@link #script(String)}.
*
* @param lang
* The language of the script.
* @return This builder (fluent interface support)
* @deprecated Use {@link #script(Script)} instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public B lang(String lang) {
this.lang = lang;
return (B) this;
}
/**
* Sets the value of a parameter that is used in the script (if one is
* configured).
*
* @param name
* The name of the parameter.
* @param value
* The value of the parameter.
* @return This builder (fluent interface support)
* @deprecated Use {@link #script(Script)} instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public B param(String name, Object value) {
if (params == null) {
params = Maps.newHashMap();
}
params.put(name, value);
return (B) this;
}
/**
* Sets the values of a parameters that are used in the script (if one is
* configured).
*
* @param params
* The the parameters.
* @return This builder (fluent interface support)
* @deprecated Use {@link #script(Script)} instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public B params(Map<String, Object> params) {
if (this.params == null) {
this.params = Maps.newHashMap();
}
this.params.putAll(params);
return (B) this;
}
/**
* Configure the value to use when documents miss a value.
*/

View File

@ -189,79 +189,17 @@ public class ScriptHeuristic extends SignificanceHeuristic {
public static class ScriptHeuristicBuilder implements SignificanceHeuristicBuilder {
private Script script = null;
private String scriptString = null;
private ScriptType type = null;
private String lang = null;
private Map<String, Object> params = null;
public ScriptHeuristicBuilder setScript(Script script) {
this.script = script;
return this;
}
/**
* @deprecated use {@link #setScript(Script)}
*/
@Deprecated
public ScriptHeuristicBuilder setScript(String script) {
if (script != null) {
this.scriptString = script;
this.type = ScriptType.INLINE;
}
return this;
}
/**
* @deprecated use {@link #setScript(Script)}
*/
@Deprecated
public ScriptHeuristicBuilder setScriptFile(String script) {
if (script != null) {
this.scriptString = script;
this.type = ScriptType.FILE;
}
return this;
}
/**
* @deprecated use {@link #setScript(Script)}
*/
@Deprecated
public ScriptHeuristicBuilder setLang(String lang) {
this.lang = lang;
return this;
}
/**
* @deprecated use {@link #setScript(Script)}
*/
@Deprecated
public ScriptHeuristicBuilder setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* @deprecated use {@link #setScript(Script)}
*/
@Deprecated
public ScriptHeuristicBuilder setScriptId(String scriptId) {
if (scriptId != null) {
this.scriptString = scriptId;
this.type = ScriptType.INDEXED;
}
return this;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
builder.startObject(STREAM.getName());
builder.field(ScriptField.SCRIPT.getPreferredName());
if (script == null) {
new Script(scriptString, type, lang, params).toXContent(builder, builderParams);
} else {
script.toXContent(builder, builderParams);
}
script.toXContent(builder, builderParams);
builder.endObject();
return builder;
}

View File

@ -37,34 +37,6 @@ public class ScriptedMetricBuilder extends MetricsAggregationBuilder {
private Script combineScript = null;
private Script reduceScript = null;
private Map<String, Object> params = null;
@Deprecated
private Map<String, Object> reduceParams = null;
@Deprecated
private String initScriptString = null;
@Deprecated
private String mapScriptString = null;
@Deprecated
private String combineScriptString = null;
@Deprecated
private String reduceScriptString = null;
@Deprecated
private String initScriptFile = null;
@Deprecated
private String mapScriptFile = null;
@Deprecated
private String combineScriptFile = null;
@Deprecated
private String reduceScriptFile = null;
@Deprecated
private String initScriptId = null;
@Deprecated
private String mapScriptId = null;
@Deprecated
private String combineScriptId = null;
@Deprecated
private String reduceScriptId = null;
@Deprecated
private String lang = null;
/**
* Sole constructor.
@ -114,162 +86,6 @@ public class ScriptedMetricBuilder extends MetricsAggregationBuilder {
return this;
}
/**
* Set parameters that will be available in the <tt>reduce</tt> phase.
*
* @deprecated Use {@link #reduceScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder reduceParams(Map<String, Object> reduceParams) {
this.reduceParams = reduceParams;
return this;
}
/**
* Set the <tt>init</tt> script.
*
* @deprecated Use {@link #initScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder initScript(String initScript) {
this.initScriptString = initScript;
return this;
}
/**
* Set the <tt>map</tt> script.
*
* @deprecated Use {@link #mapScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder mapScript(String mapScript) {
this.mapScriptString = mapScript;
return this;
}
/**
* Set the <tt>combine</tt> script.
*
* @deprecated Use {@link #combineScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder combineScript(String combineScript) {
this.combineScriptString = combineScript;
return this;
}
/**
* Set the <tt>reduce</tt> script.
*
* @deprecated Use {@link #reduceScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder reduceScript(String reduceScript) {
this.reduceScriptString = reduceScript;
return this;
}
/**
* Set the <tt>init</tt> script file.
*
* @deprecated Use {@link #initScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder initScriptFile(String initScriptFile) {
this.initScriptFile = initScriptFile;
return this;
}
/**
* Set the <tt>map</tt> script file.
*
* @deprecated Use {@link #mapScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder mapScriptFile(String mapScriptFile) {
this.mapScriptFile = mapScriptFile;
return this;
}
/**
* Set the <tt>combine</tt> script file.
*
* @deprecated Use {@link #combineScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder combineScriptFile(String combineScriptFile) {
this.combineScriptFile = combineScriptFile;
return this;
}
/**
* Set the <tt>reduce</tt> script file.
*
* @deprecated Use {@link #reduceScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder reduceScriptFile(String reduceScriptFile) {
this.reduceScriptFile = reduceScriptFile;
return this;
}
/**
* Set the indexed <tt>init</tt> script id.
*
* @deprecated Use {@link #initScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder initScriptId(String initScriptId) {
this.initScriptId = initScriptId;
return this;
}
/**
* Set the indexed <tt>map</tt> script id.
*
* @deprecated Use {@link #mapScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder mapScriptId(String mapScriptId) {
this.mapScriptId = mapScriptId;
return this;
}
/**
* Set the indexed <tt>combine</tt> script id.
*
* @deprecated Use {@link #combineScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder combineScriptId(String combineScriptId) {
this.combineScriptId = combineScriptId;
return this;
}
/**
* Set the indexed <tt>reduce</tt> script id.
*
* @deprecated Use {@link #reduceScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder reduceScriptId(String reduceScriptId) {
this.reduceScriptId = reduceScriptId;
return this;
}
/**
* Set the script language.
*
* @deprecated Use {@link #initScript(Script)}, {@link #mapScript(Script)},
* {@link #combineScript(Script)}, and
* {@link #reduceScript(Script)} instead.
*/
@Deprecated
public ScriptedMetricBuilder lang(String lang) {
this.lang = lang;
return this;
}
@Override
protected void internalXContent(XContentBuilder builder, Params builderParams) throws IOException {
@ -288,68 +104,10 @@ public class ScriptedMetricBuilder extends MetricsAggregationBuilder {
if (reduceScript != null) {
builder.field(ScriptedMetricParser.REDUCE_SCRIPT_FIELD.getPreferredName(), reduceScript);
}
if (params != null) {
builder.field(ScriptedMetricParser.PARAMS_FIELD.getPreferredName());
builder.map(params);
}
if (reduceParams != null) {
builder.field(ScriptedMetricParser.REDUCE_PARAMS_FIELD.getPreferredName());
builder.map(reduceParams);
}
if (initScriptString != null) {
builder.field(ScriptedMetricParser.INIT_SCRIPT, initScriptString);
}
if (mapScriptString != null) {
builder.field(ScriptedMetricParser.MAP_SCRIPT, mapScriptString);
}
if (combineScriptString != null) {
builder.field(ScriptedMetricParser.COMBINE_SCRIPT, combineScriptString);
}
if (reduceScriptString != null) {
builder.field(ScriptedMetricParser.REDUCE_SCRIPT, reduceScriptString);
}
if (initScriptFile != null) {
builder.field(ScriptedMetricParser.INIT_SCRIPT + ScriptParameterParser.FILE_SUFFIX, initScriptFile);
}
if (mapScriptFile != null) {
builder.field(ScriptedMetricParser.MAP_SCRIPT + ScriptParameterParser.FILE_SUFFIX, mapScriptFile);
}
if (combineScriptFile != null) {
builder.field(ScriptedMetricParser.COMBINE_SCRIPT + ScriptParameterParser.FILE_SUFFIX, combineScriptFile);
}
if (reduceScriptFile != null) {
builder.field(ScriptedMetricParser.REDUCE_SCRIPT + ScriptParameterParser.FILE_SUFFIX, reduceScriptFile);
}
if (initScriptId != null) {
builder.field(ScriptedMetricParser.INIT_SCRIPT + ScriptParameterParser.INDEXED_SUFFIX, initScriptId);
}
if (mapScriptId != null) {
builder.field(ScriptedMetricParser.MAP_SCRIPT + ScriptParameterParser.INDEXED_SUFFIX, mapScriptId);
}
if (combineScriptId != null) {
builder.field(ScriptedMetricParser.COMBINE_SCRIPT + ScriptParameterParser.INDEXED_SUFFIX, combineScriptId);
}
if (reduceScriptId != null) {
builder.field(ScriptedMetricParser.REDUCE_SCRIPT + ScriptParameterParser.INDEXED_SUFFIX, reduceScriptId);
}
if (lang != null) {
builder.field(ScriptedMetricParser.LANG_FIELD.getPreferredName(), lang);
}
}
}

View File

@ -152,60 +152,6 @@ public class TopHitsBuilder extends AbstractAggregationBuilder {
return this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param script
* The script to use
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public TopHitsBuilder addScriptField(String name, String script) {
sourceBuilder().scriptField(name, script);
return this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param script
* The script to use
* @param params
* Parameters that the script can use.
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public TopHitsBuilder addScriptField(String name, String script, Map<String, Object> params) {
sourceBuilder().scriptField(name, script, params);
return this;
}
/**
* Adds a script based field to load and return. The field does not have to
* be stored, but its recommended to use non analyzed or numeric fields.
*
* @param name
* The name that will represent this value in the return hit
* @param lang
* The language of the script
* @param script
* The script to use
* @param params
* Parameters that the script can use (can be <tt>null</tt>).
* @deprecated Use {@link #addScriptField(String, Script)} instead.
*/
@Deprecated
public TopHitsBuilder addScriptField(String name, String lang, String script, Map<String, Object> params) {
sourceBuilder().scriptField(name, lang, script, params);
return this;
}
/**
* Adds a sort against the given field name and the sort ordering.
*

View File

@ -605,54 +605,6 @@ public class SearchSourceBuilder extends ToXContentToBytes {
return this;
}
/**
* Adds a script field under the given name with the provided script.
*
* @param name
* The name of the field
* @param script
* The script
* @deprecated Use {@link #scriptField(String, Script)} instead.
*/
@Deprecated
public SearchSourceBuilder scriptField(String name, String script) {
return scriptField(name, null, script, null);
}
/**
* Adds a script field.
*
* @param name
* The name of the field
* @param script
* The script to execute
* @param params
* The script parameters
* @deprecated Use {@link #scriptField(String, Script)} instead.
*/
@Deprecated
public SearchSourceBuilder scriptField(String name, String script, Map<String, Object> params) {
return scriptField(name, null, script, params);
}
/**
* Adds a script field.
*
* @param name
* The name of the field
* @param lang
* The language of the script
* @param script
* The script to execute
* @param params
* The script parameters (can be <tt>null</tt>)
* @deprecated Use {@link #scriptField(String, Script)} instead.
*/
@Deprecated
public SearchSourceBuilder scriptField(String name, String lang, String script, Map<String, Object> params) {
return scriptField(name, new Script(script, ScriptType.INLINE, lang, params));
}
/**
* Sets the boost a specific index will receive when the query is executeed
* against it.

View File

@ -119,83 +119,4 @@ public class UpdateRequestTests extends ElasticsearchTestCase {
assertThat(doc.get("field1").toString(), equalTo("value1"));
assertThat(((Map) doc.get("compound")).get("field2").toString(), equalTo("value2"));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUpdateRequestOldAPI() throws Exception {
UpdateRequest request = new UpdateRequest("test", "type", "1");
// simple script
request.source(XContentFactory.jsonBuilder().startObject().field("script", "script1").endObject());
assertThat(request.scriptString(), equalTo("script1"));
// script with params
request = new UpdateRequest("test", "type", "1");
request.source(XContentFactory.jsonBuilder().startObject()
.field("script", "script1")
.startObject("params").field("param1", "value1").endObject()
.endObject());
assertThat(request.scriptString(), notNullValue());
assertThat(request.scriptString(), equalTo("script1"));
assertThat(request.scriptParams().get("param1").toString(), equalTo("value1"));
request = new UpdateRequest("test", "type", "1");
request.source(XContentFactory.jsonBuilder().startObject()
.startObject("params").field("param1", "value1").endObject()
.field("script", "script1")
.endObject());
assertThat(request.scriptString(), notNullValue());
assertThat(request.scriptString(), equalTo("script1"));
assertThat(request.scriptParams().get("param1").toString(), equalTo("value1"));
// script with params and upsert
request = new UpdateRequest("test", "type", "1");
request.source(XContentFactory.jsonBuilder().startObject()
.startObject("params").field("param1", "value1").endObject()
.field("script", "script1")
.startObject("upsert").field("field1", "value1").startObject("compound").field("field2", "value2").endObject().endObject()
.endObject());
assertThat(request.scriptString(), notNullValue());
assertThat(request.scriptString(), equalTo("script1"));
assertThat(request.scriptParams().get("param1").toString(), equalTo("value1"));
Map<String, Object> upsertDoc = XContentHelper.convertToMap(request.upsertRequest().source(), true).v2();
assertThat(upsertDoc.get("field1").toString(), equalTo("value1"));
assertThat(((Map) upsertDoc.get("compound")).get("field2").toString(), equalTo("value2"));
request = new UpdateRequest("test", "type", "1");
request.source(XContentFactory.jsonBuilder().startObject()
.startObject("upsert").field("field1", "value1").startObject("compound").field("field2", "value2").endObject().endObject()
.startObject("params").field("param1", "value1").endObject()
.field("script", "script1")
.endObject());
assertThat(request.scriptString(), notNullValue());
assertThat(request.scriptString(), equalTo("script1"));
assertThat(request.scriptParams().get("param1").toString(), equalTo("value1"));
upsertDoc = XContentHelper.convertToMap(request.upsertRequest().source(), true).v2();
assertThat(upsertDoc.get("field1").toString(), equalTo("value1"));
assertThat(((Map) upsertDoc.get("compound")).get("field2").toString(), equalTo("value2"));
request = new UpdateRequest("test", "type", "1");
request.source(XContentFactory.jsonBuilder().startObject()
.startObject("params").field("param1", "value1").endObject()
.startObject("upsert").field("field1", "value1").startObject("compound").field("field2", "value2").endObject().endObject()
.field("script", "script1")
.endObject());
assertThat(request.scriptString(), notNullValue());
assertThat(request.scriptString(), equalTo("script1"));
assertThat(request.scriptParams().get("param1").toString(), equalTo("value1"));
upsertDoc = XContentHelper.convertToMap(request.upsertRequest().source(), true).v2();
assertThat(upsertDoc.get("field1").toString(), equalTo("value1"));
assertThat(((Map) upsertDoc.get("compound")).get("field2").toString(), equalTo("value2"));
// script with doc
request = new UpdateRequest("test", "type", "1");
request.source(XContentFactory.jsonBuilder().startObject()
.startObject("doc").field("field1", "value1").startObject("compound").field("field2", "value2").endObject().endObject()
.endObject());
Map<String, Object> doc = request.doc().sourceAsMap();
assertThat(doc.get("field1").toString(), equalTo("value1"));
assertThat(((Map) doc.get("compound")).get("field2").toString(), equalTo("value2"));
}
}

View File

@ -155,97 +155,6 @@ public class BulkTests extends ElasticsearchIntegrationTest {
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(4l));
}
@Test
public void testBulkUpdate_simpleOldScriptAPI() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
ensureGreen();
BulkResponse bulkResponse = client().prepareBulk()
.add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("1").setSource("field", 1))
.add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("2").setSource("field", 2).setCreate(true))
.add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("3").setSource("field", 3))
.add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("4").setSource("field", 4))
.add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("5").setSource("field", 5)).execute()
.actionGet();
assertThat(bulkResponse.hasFailures(), equalTo(false));
assertThat(bulkResponse.getItems().length, equalTo(5));
for (BulkItemResponse bulkItemResponse : bulkResponse) {
assertThat(bulkItemResponse.getIndex(), equalTo("test"));
}
bulkResponse = client()
.prepareBulk()
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("1")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE))
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setRetryOnConflict(3))
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("3").setDoc(jsonBuilder().startObject().field("field1", "test").endObject()))
.execute().actionGet();
assertThat(bulkResponse.hasFailures(), equalTo(false));
assertThat(bulkResponse.getItems().length, equalTo(3));
for (BulkItemResponse bulkItemResponse : bulkResponse) {
assertThat(bulkItemResponse.getIndex(), equalTo("test"));
}
assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getId(), equalTo("1"));
assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getVersion(), equalTo(2l));
assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getId(), equalTo("2"));
assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getVersion(), equalTo(2l));
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getId(), equalTo("3"));
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(2l));
GetResponse getResponse = client().prepareGet().setIndex("test").setType("type1").setId("1").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(2l));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(2l));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("2").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(2l));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(3l));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("3").setFields("field1").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(2l));
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("test"));
bulkResponse = client().prepareBulk()
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("6")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
.setUpsert(jsonBuilder().startObject().field("field", 0).endObject()))
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("7")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE))
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE))
.execute().actionGet();
assertThat(bulkResponse.hasFailures(), equalTo(true));
assertThat(bulkResponse.getItems().length, equalTo(3));
assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getId(), equalTo("6"));
assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getVersion(), equalTo(1l));
assertThat(bulkResponse.getItems()[1].getResponse(), nullValue());
assertThat(bulkResponse.getItems()[1].getFailure().getIndex(), equalTo("test"));
assertThat(bulkResponse.getItems()[1].getFailure().getId(), equalTo("7"));
assertThat(bulkResponse.getItems()[1].getFailure().getMessage(), containsString("document missing"));
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getId(), equalTo("2"));
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getIndex(), equalTo("test"));
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(3l));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("6").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(1l));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(0l));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("7").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(false));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("2").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(3l));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(4l));
}
@Test
public void testBulkVersioning() throws Exception {
createIndex("test");
@ -334,49 +243,6 @@ public class BulkTests extends ElasticsearchIntegrationTest {
assertThat(bulkResponse.getItems()[2].getResponse(), nullValue());
}
/*
* TODO Remove in 2.0
*/
@Test
public void testBulkUpdate_malformedScriptsOldScriptAPI() throws Exception {
createIndex("test");
ensureGreen();
BulkResponse bulkResponse = client().prepareBulk()
.add(client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource("field", 1))
.add(client().prepareIndex().setIndex("test").setType("type1").setId("2").setSource("field", 1))
.add(client().prepareIndex().setIndex("test").setType("type1").setId("3").setSource("field", 1)).execute().actionGet();
assertThat(bulkResponse.hasFailures(), equalTo(false));
assertThat(bulkResponse.getItems().length, equalTo(3));
bulkResponse = client()
.prepareBulk()
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1")
.setScript("ctx._source.field += a", ScriptService.ScriptType.INLINE).setFields("field"))
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setFields("field"))
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3")
.setScript("ctx._source.field += a", ScriptService.ScriptType.INLINE).setFields("field"))
.execute().actionGet();
assertThat(bulkResponse.hasFailures(), equalTo(true));
assertThat(bulkResponse.getItems().length, equalTo(3));
assertThat(bulkResponse.getItems()[0].getFailure().getId(), equalTo("1"));
assertThat(bulkResponse.getItems()[0].getFailure().getMessage(), containsString("failed to execute script"));
assertThat(bulkResponse.getItems()[0].getResponse(), nullValue());
assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getId(), equalTo("2"));
assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getVersion(), equalTo(2l));
assertThat(((Integer) ((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getGetResult().field("field").getValue()), equalTo(2));
assertThat(bulkResponse.getItems()[1].getFailure(), nullValue());
assertThat(bulkResponse.getItems()[2].getFailure().getId(), equalTo("3"));
assertThat(bulkResponse.getItems()[2].getFailure().getMessage(), containsString("failed to execute script"));
assertThat(bulkResponse.getItems()[2].getResponse(), nullValue());
}
@Test
public void testBulkUpdate_largerVolume() throws Exception {
createIndex("test");
@ -511,146 +377,6 @@ public class BulkTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testBulkUpdate_largerVolumeOldScriptAPI() throws Exception {
createIndex("test");
ensureGreen();
int numDocs = scaledRandomIntBetween(100, 2000);
if (numDocs % 2 == 1) {
numDocs++; // this test needs an even num of docs
}
logger.info("Bulk-Indexing {} docs", numDocs);
BulkRequestBuilder builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i))
.setScript("ctx._source.counter += 1", ScriptService.ScriptType.INLINE).setFields("counter")
.setUpsert(jsonBuilder().startObject().field("counter", 1).endObject())
);
}
BulkResponse response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getVersion(), equalTo(1l));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo("update"));
assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getId(), equalTo(Integer.toString(i)));
assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getVersion(), equalTo(1l));
assertThat(((Integer) ((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue()), equalTo(1));
for (int j = 0; j < 5; j++) {
GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).setFields("counter").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(1l));
assertThat((Long) getResponse.getField("counter").getValue(), equalTo(1l));
}
}
builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
UpdateRequestBuilder updateBuilder = client().prepareUpdate()
.setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter");
if (i % 2 == 0) {
updateBuilder.setScript("ctx._source.counter += 1", ScriptService.ScriptType.INLINE);
} else {
updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
}
if (i % 3 == 0) {
updateBuilder.setRetryOnConflict(3);
}
builder.add(updateBuilder);
}
response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getVersion(), equalTo(2l));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo("update"));
assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getId(), equalTo(Integer.toString(i)));
assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getVersion(), equalTo(2l));
assertThat(((Integer) ((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue()), equalTo(2));
}
builder = client().prepareBulk();
int maxDocs = numDocs / 2 + numDocs;
for (int i = (numDocs / 2); i < maxDocs; i++) {
builder.add(
client().prepareUpdate()
.setIndex("test").setType("type1").setId(Integer.toString(i)).setScript("ctx._source.counter += 1", ScriptService.ScriptType.INLINE)
);
}
response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(true));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
int id = i + (numDocs / 2);
if (i >= (numDocs / 2)) {
assertThat(response.getItems()[i].getFailure().getId(), equalTo(Integer.toString(id)));
assertThat(response.getItems()[i].getFailure().getMessage(), containsString("document missing"));
} else {
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(id)));
assertThat(response.getItems()[i].getVersion(), equalTo(3l));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo("update"));
}
}
builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
builder.add(
client().prepareUpdate()
.setIndex("test").setType("type1").setId(Integer.toString(i))
.setScript("ctx.op = \"none\"", ScriptService.ScriptType.INLINE)
);
}
response = builder.execute().actionGet();
assertThat(response.buildFailureMessage(), response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getItemId(), equalTo(i));
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo("update"));
}
builder = client().prepareBulk();
for (int i = 0; i < numDocs; i++) {
builder.add(
client().prepareUpdate()
.setIndex("test").setType("type1").setId(Integer.toString(i))
.setScript("ctx.op = \"delete\"", ScriptService.ScriptType.INLINE)
);
}
response = builder.execute().actionGet();
assertThat(response.hasFailures(), equalTo(false));
assertThat(response.getItems().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getItems()[i].getItemId(), equalTo(i));
assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
assertThat(response.getItems()[i].getIndex(), equalTo("test"));
assertThat(response.getItems()[i].getType(), equalTo("type1"));
assertThat(response.getItems()[i].getOpType(), equalTo("update"));
for (int j = 0; j < 5; j++) {
GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).setFields("counter").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(false));
}
}
}
@Test
public void testBulkIndexingWhileInitializing() throws Exception {

View File

@ -56,6 +56,7 @@ import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.core.NumberFieldMapper;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.index.search.geo.GeoDistanceRangeQuery;
import org.elasticsearch.index.search.geo.GeoPolygonQuery;
import org.elasticsearch.index.search.geo.InMemoryGeoBoundingBoxQuery;
@ -74,7 +75,6 @@ import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.factorFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery;
import static org.hamcrest.Matchers.*;
@ -1254,23 +1254,23 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest {
}
@Test
public void testCustomBoostFactorQueryBuilder_withFunctionScore() throws IOException {
public void testCustomWeightFactorQueryBuilder_withFunctionScore() throws IOException {
IndexQueryParserService queryParser = queryParser();
Query parsedQuery = queryParser.parse(functionScoreQuery(termQuery("name.last", "banon"), factorFunction(1.3f))).query();
Query parsedQuery = queryParser.parse(functionScoreQuery(termQuery("name.last", "banon"), ScoreFunctionBuilders.weightFactorFunction(1.3f))).query();
assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class));
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery;
assertThat(((TermQuery) functionScoreQuery.getSubQuery()).getTerm(), equalTo(new Term("name.last", "banon")));
assertThat((double) ((BoostScoreFunction) functionScoreQuery.getFunction()).getBoost(), closeTo(1.3, 0.001));
assertThat((double) ((WeightFactorFunction) functionScoreQuery.getFunction()).getWeight(), closeTo(1.3, 0.001));
}
@Test
public void testCustomBoostFactorQueryBuilder_withFunctionScoreWithoutQueryGiven() throws IOException {
public void testCustomWeightFactorQueryBuilder_withFunctionScoreWithoutQueryGiven() throws IOException {
IndexQueryParserService queryParser = queryParser();
Query parsedQuery = queryParser.parse(functionScoreQuery(factorFunction(1.3f))).query();
Query parsedQuery = queryParser.parse(functionScoreQuery(ScoreFunctionBuilders.weightFactorFunction(1.3f))).query();
assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class));
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery;
assertThat(functionScoreQuery.getSubQuery() instanceof MatchAllDocsQuery, equalTo(true));
assertThat((double) ((BoostScoreFunction) functionScoreQuery.getFunction()).getBoost(), closeTo(1.3, 0.001));
assertThat((double) ((WeightFactorFunction) functionScoreQuery.getFunction()).getWeight(), closeTo(1.3, 0.001));
}
@Test
@ -2330,12 +2330,6 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest {
} catch (QueryParsingException e) {
assertThat(e.getDetailedMessage(), containsString(BoostScoreFunction.BOOST_WEIGHT_ERROR_MESSAGE));
}
try {
functionScoreQuery().add(factorFunction(2.0f).setWeight(2.0f));
fail("Expect exception here because boost_factor must not have a weight");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString(BoostScoreFunction.BOOST_WEIGHT_ERROR_MESSAGE));
}
query = jsonBuilder().startObject().startObject("function_score")
.startArray("functions")
.startObject().field("boost_factor",2).endObject()

View File

@ -48,19 +48,4 @@ public class TemplateQueryBuilderTest extends ElasticsearchTestCase {
assertEquals("{\"template\":{\"inline\":\"I am a $template string\",\"params\":{\"template\":\"filled\"}}}", content.string());
}
/*
* TODO Remove in 2.0
*/
@Test
public void testJSONGenerationOldScriptAPI() throws IOException {
Map<String, Object> vars = new HashMap<>();
vars.put("template", "filled");
TemplateQueryBuilder builder = new TemplateQueryBuilder("I am a $template string", vars);
XContentBuilder content = XContentFactory.jsonBuilder();
content.startObject();
builder.doXContent(content, null);
content.endObject();
content.close();
assertEquals("{\"template\":{\"inline\":\"I am a $template string\",\"params\":{\"template\":\"filled\"}}}", content.string());
}
}

View File

@ -545,283 +545,4 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
assertHitCount(searchResponse, 5);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testTemplateInBodyOldScriptAPI() throws IOException {
Map<String, Object> vars = new HashMap<>();
vars.put("template", "all");
TemplateQueryBuilder builder = new TemplateQueryBuilder("{\"match_{{template}}\": {}}\"", vars);
SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
assertHitCount(sr, 2);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testTemplateWOReplacementInBodyOldScriptAPI() throws IOException {
Map<String, Object> vars = new HashMap<>();
TemplateQueryBuilder builder = new TemplateQueryBuilder("{\"match_all\": {}}\"", vars);
SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
assertHitCount(sr, 2);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testTemplateInFileOldScriptAPI() {
Map<String, Object> vars = new HashMap<>();
vars.put("template", "all");
TemplateQueryBuilder builder = new TemplateQueryBuilder("storedTemplate", ScriptService.ScriptType.FILE, vars);
SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
assertHitCount(sr, 2);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testIndexedTemplateOldScriptAPI() throws Exception {
createIndex(ScriptService.SCRIPT_INDEX);
ensureGreen(ScriptService.SCRIPT_INDEX);
List<IndexRequestBuilder> builders = new ArrayList<>();
builders.add(client().prepareIndex(ScriptService.SCRIPT_INDEX, MustacheScriptEngineService.NAME, "1a").setSource(
"{" + "\"template\":{" + " \"query\":{" + " \"match\":{"
+ " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"));
builders.add(client().prepareIndex(ScriptService.SCRIPT_INDEX, MustacheScriptEngineService.NAME, "2").setSource(
"{" + "\"template\":{" + " \"query\":{" + " \"match\":{"
+ " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"));
builders.add(client().prepareIndex(ScriptService.SCRIPT_INDEX, MustacheScriptEngineService.NAME, "3").setSource(
"{" + "\"template\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }"
+ "}"));
indexRandom(true, builders);
builders.clear();
builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));
indexRandom(true, builders);
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("fieldParam", "foo");
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("/mustache/1a")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 4);
try {
client().prepareSearch("test").setTypes("type").setTemplateName("/template_index/mustache/1000")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
fail("shouldn't get here");
} catch (SearchPhaseExecutionException spee) {
//all good
}
try {
searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("/myindex/mustache/1")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertFailures(searchResponse);
} catch (SearchPhaseExecutionException spee) {
//all good
}
searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("1a")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 4);
templateParams.put("fieldParam", "bar");
searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("/mustache/2")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 1);
Map<String, Object> vars = new HashMap<>();
vars.put("fieldParam", "bar");
TemplateQueryBuilder builder = new TemplateQueryBuilder("3", ScriptService.ScriptType.INDEXED, vars);
SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
assertHitCount(sr, 1);
String query = "{\"template\": {\"id\": \"3\",\"params\" : {\"fieldParam\" : \"foo\"}}}";
sr = client().prepareSearch().setQuery(query).get();
assertHitCount(sr, 4);
query = "{\"template\": {\"id\": \"/mustache/3\",\"params\" : {\"fieldParam\" : \"foo\"}}}";
sr = client().prepareSearch().setQuery(query).get();
assertHitCount(sr, 4);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testThatParametersCanBeSetOldScriptAPI() throws Exception {
index("test", "type", "1", jsonBuilder().startObject().field("theField", "foo").endObject());
index("test", "type", "2", jsonBuilder().startObject().field("theField", "foo 2").endObject());
index("test", "type", "3", jsonBuilder().startObject().field("theField", "foo 3").endObject());
index("test", "type", "4", jsonBuilder().startObject().field("theField", "foo 4").endObject());
index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject());
refresh();
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("mySize", "2");
templateParams.put("myField", "theField");
templateParams.put("myValue", "foo");
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("full-query-template")
.setTemplateParams(templateParams).setTemplateType(ScriptService.ScriptType.FILE).get();
assertHitCount(searchResponse, 4);
// size kicks in here...
assertThat(searchResponse.getHits().getHits().length, is(2));
templateParams.put("myField", "otherField");
searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("full-query-template")
.setTemplateParams(templateParams).setTemplateType(ScriptService.ScriptType.FILE).get();
assertHitCount(searchResponse, 1);
}
/*
* TODO Remove in 2.0
*/
@Test(expected = SearchPhaseExecutionException.class)
public void testIndexedTemplateClientOldScriptAPI() throws Exception {
createIndex(ScriptService.SCRIPT_INDEX);
ensureGreen(ScriptService.SCRIPT_INDEX);
PutIndexedScriptResponse scriptResponse = client().preparePutIndexedScript(
MustacheScriptEngineService.NAME,
"testTemplate",
"{" + "\"template\":{" + " \"query\":{" + " \"match\":{"
+ " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}").get();
assertTrue(scriptResponse.isCreated());
scriptResponse = client().preparePutIndexedScript(
MustacheScriptEngineService.NAME,
"testTemplate",
"{" + "\"template\":{" + " \"query\":{" + " \"match\":{"
+ " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}").get();
assertEquals(scriptResponse.getVersion(), 2);
GetIndexedScriptResponse getResponse = client().prepareGetIndexedScript(MustacheScriptEngineService.NAME, "testTemplate").get();
assertTrue(getResponse.isExists());
List<IndexRequestBuilder> builders = new ArrayList<>();
builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));
indexRandom(true, builders);
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("fieldParam", "foo");
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("testTemplate")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 4);
DeleteIndexedScriptResponse deleteResponse = client().prepareDeleteIndexedScript(MustacheScriptEngineService.NAME, "testTemplate")
.get();
assertTrue(deleteResponse.isFound());
getResponse = client().prepareGetIndexedScript(MustacheScriptEngineService.NAME, "testTemplate").get();
assertFalse(getResponse.isExists());
client().prepareSearch("test").setTypes("type").setTemplateName("/template_index/mustache/1000")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
}
/*
* TODO Remove in 2.0
*/
// Relates to #10397
@Test
public void testIndexedTemplateOverwriteOldScriptAPI() throws Exception {
createIndex("testindex");
ensureGreen("testindex");
index("testindex", "test", "1", jsonBuilder().startObject().field("searchtext", "dev1").endObject());
refresh();
int iterations = randomIntBetween(2, 11);
for (int i = 1; i < iterations; i++) {
PutIndexedScriptResponse scriptResponse = client().preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"ooophrase_prefix\"}}}}").get();
assertEquals(i * 2 - 1, scriptResponse.getVersion());
GetIndexedScriptResponse getResponse = client().prepareGetIndexedScript(MustacheScriptEngineService.NAME, "git01").get();
assertTrue(getResponse.isExists());
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("P_Keyword1", "dev");
try {
client().prepareSearch("testindex").setTypes("test").setTemplateName("git01")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
fail("Broken test template is parsing w/o error.");
} catch (SearchPhaseExecutionException e) {
// the above is expected to fail
}
PutIndexedScriptRequestBuilder builder = client().preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"phrase_prefix\"}}}}").setOpType(
OpType.INDEX);
scriptResponse = builder.get();
assertEquals(i * 2, scriptResponse.getVersion());
SearchResponse searchResponse = client().prepareSearch("testindex").setTypes("test").setTemplateName("git01")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 1);
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testIndexedTemplateWithArrayOldScriptAPI() throws Exception {
createIndex(ScriptService.SCRIPT_INDEX);
ensureGreen(ScriptService.SCRIPT_INDEX);
List<IndexRequestBuilder> builders = new ArrayList<>();
String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
builders.add(client().prepareIndex(ScriptService.SCRIPT_INDEX, MustacheScriptEngineService.NAME, "4").setSource(
jsonBuilder().startObject().field("template", multiQuery).endObject()));
indexRandom(true, builders);
builders.clear();
builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));
indexRandom(true, builders);
Map<String, Object> arrayTemplateParams = new HashMap<>();
String[] fieldParams = { "foo", "bar" };
arrayTemplateParams.put("fieldParam", fieldParams);
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("/mustache/4")
.setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(arrayTemplateParams).get();
assertHitCount(searchResponse, 5);
}
}

View File

@ -615,142 +615,6 @@ public class SimpleNestedTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testSimpleNestedSortingOldScriptAPI() throws Exception {
assertAcked(prepareCreate("test").setSettings(settingsBuilder().put(indexSettings()).put("index.refresh_interval", -1)).addMapping(
"type1",
jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested")
.startObject("properties").startObject("field1").field("type", "long").field("store", "yes").endObject()
.endObject().endObject().endObject().endObject().endObject()));
ensureGreen();
client().prepareIndex("test", "type1", "1")
.setSource(
jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("field1", 5).endObject()
.startObject().field("field1", 4).endObject().endArray().endObject()).execute().actionGet();
client().prepareIndex("test", "type1", "2")
.setSource(
jsonBuilder().startObject().field("field1", 2).startArray("nested1").startObject().field("field1", 1).endObject()
.startObject().field("field1", 2).endObject().endArray().endObject()).execute().actionGet();
client().prepareIndex("test", "type1", "3")
.setSource(
jsonBuilder().startObject().field("field1", 3).startArray("nested1").startObject().field("field1", 3).endObject()
.startObject().field("field1", 4).endObject().endArray().endObject()).execute().actionGet();
refresh();
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type1").setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.fieldSort("nested1.field1").order(SortOrder.ASC)).execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("4"));
searchResponse = client().prepareSearch("test").setTypes("type1").setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.fieldSort("nested1.field1").order(SortOrder.DESC)).execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("5"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("4"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("2"));
searchResponse = client()
.prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.scriptSort("_fields['nested1.field1'].value + 1", "number").setNestedPath("nested1").order(SortOrder.DESC))
.execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("6.0"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("5.0"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("3.0"));
searchResponse = client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.scriptSort("_fields['nested1.field1'].value + 1", "number").setNestedPath("nested1").sortMode("sum").order(SortOrder.DESC))
.execute().actionGet();
// B/c of sum it is actually +2
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("11.0"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("9.0"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("5.0"));
searchResponse = client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.scriptSort("_fields['nested1.field1'].value", "number")
.setNestedFilter(rangeQuery("nested1.field1").from(1).to(3))
.setNestedPath("nested1").sortMode("avg").order(SortOrder.DESC))
.execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo(Double.toString(Double.MAX_VALUE)));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("3.0"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("1.5"));
searchResponse = client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.scriptSort("_fields['nested1.field1'].value", "string")
.setNestedPath("nested1").order(SortOrder.DESC))
.execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("5"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("4"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("2"));
searchResponse = client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.scriptSort("_fields['nested1.field1'].value", "string")
.setNestedPath("nested1").order(SortOrder.ASC))
.execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("4"));
try {
client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.scriptSort("_fields['nested1.field1'].value", "string")
.setNestedPath("nested1").sortMode("sum").order(SortOrder.ASC))
.execute().actionGet();
Assert.fail("SearchPhaseExecutionException should have been thrown");
} catch (SearchPhaseExecutionException e) {
assertThat(e.toString(), containsString("type [string] doesn't support mode [SUM]"));
}
}
@Test
public void testSimpleNestedSorting_withNestedFilterMissing() throws Exception {

View File

@ -630,505 +630,4 @@ public class IndexLookupTests extends ElasticsearchIntegrationTest {
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testCallWithDifferentFlagsFailsOldScriptAPI() throws Exception {
initTestData();
// should throw an exception, we cannot call with different flags twice
// if the flags of the second call were not included in the first call.
String script = "term = _index['int_payload_field']['b']; return _index['int_payload_field'].get('b', _POSITIONS).tf();";
try {
client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).execute().actionGet();
} catch (SearchPhaseExecutionException e) {
assertThat(
"got: " + e.toString(),
e.toString()
.indexOf(
"You must call get with all required flags! Instead of _index['int_payload_field'].get('b', _FREQUENCIES) and _index['int_payload_field'].get('b', _POSITIONS) call _index['int_payload_field'].get('b', _FREQUENCIES | _POSITIONS) once]"),
Matchers.greaterThan(-1));
}
// Should not throw an exception this way round
script = "term = _index['int_payload_field'].get('b', _POSITIONS | _FREQUENCIES);return _index['int_payload_field']['b'].tf();";
client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).execute().actionGet();
}
/*
* TODO Remove in 2.0
*/
@Test
public void testIteratorAndRecordingOldScriptAPI() throws Exception {
initTestData();
// call twice with record: should work as expected
String script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllFlag, "position");
checkArrayValsInEachDocOldScriptAPI(script, expectedPositionsArray, 3);
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllFlag, "startOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedStartOffsetsArray, 3);
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllFlag, "endOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedEndOffsetsArray, 3);
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllFlag, "payloadAsInt(-1)");
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 3);
// no record and get iterator twice: should fail
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "position");
checkExceptionsOldScriptAPI(script);
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "startOffset");
checkExceptionsOldScriptAPI(script);
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "endOffset");
checkExceptionsOldScriptAPI(script);
script = createPositionsArrayScriptIterateTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "payloadAsInt(-1)");
checkExceptionsOldScriptAPI(script);
// no record and get termObject twice and iterate: should fail
script = createPositionsArrayScriptGetInfoObjectTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "position");
checkExceptionsOldScriptAPI(script);
script = createPositionsArrayScriptGetInfoObjectTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "startOffset");
checkExceptionsOldScriptAPI(script);
script = createPositionsArrayScriptGetInfoObjectTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "endOffset");
checkExceptionsOldScriptAPI(script);
script = createPositionsArrayScriptGetInfoObjectTwiceOldScriptAPI("b", includeAllWithoutRecordFlag, "payloadAsInt(-1)");
checkExceptionsOldScriptAPI(script);
}
/*
* TODO Remove in 2.0
*/
private String createPositionsArrayScriptGetInfoObjectTwiceOldScriptAPI(String term, String flags, String what) {
String script = "term = _index['int_payload_field'].get('" + term + "'," + flags + "); array=[]; for (pos in term) {array.add(pos."
+ what + ")}; _index['int_payload_field'].get('" + term + "'," + flags + "); array=[]; for (pos in term) {array.add(pos."
+ what + ")}";
return script;
}
/*
* TODO Remove in 2.0
*/
private String createPositionsArrayScriptIterateTwiceOldScriptAPI(String term, String flags, String what) {
String script = "term = _index['int_payload_field'].get('" + term + "'," + flags + "); array=[]; for (pos in term) {array.add(pos."
+ what + ")}; array=[]; for (pos in term) {array.add(pos." + what + ")}; array";
return script;
}
/*
* TODO Remove in 2.0
*/
private String createPositionsArrayScriptOldScriptAPI(String field, String term, String flags, String what) {
String script = "term = _index['" + field + "'].get('" + term + "'," + flags + "); array=[]; for (pos in term) {array.add(pos."
+ what + ")}; array";
return script;
}
/*
* TODO Remove in 2.0
*/
private String createPositionsArrayScriptDefaultGetOldScriptAPI(String field, String term, String what) {
String script = "term = _index['" + field + "']['" + term + "']; array=[]; for (pos in term) {array.add(pos." + what + ")}; array";
return script;
}
/*
* TODO Remove in 2.0
*/
@Test
public void testFlagsOldScriptAPI() throws Exception {
initTestData();
// check default flag
String script = createPositionsArrayScriptDefaultGetOldScriptAPI("int_payload_field", "b", "position");
// there should be no positions
/*
* TODO: the following tests fail with the new postings enum apis
* because of a bogus assert in BlockDocsEnum
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScriptDefaultGet("int_payload_field", "b",
* "startOffset"); // there should be no offsets
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScriptDefaultGet("int_payload_field", "b",
* "endOffset"); // there should be no offsets
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScriptDefaultGet("int_payload_field", "b",
* "payloadAsInt(-1)"); // there should be no payload
* checkArrayValsInEachDoc(script, emptyArray, 3);
*
* // check FLAG_FREQUENCIES flag script =
* createPositionsArrayScript("int_payload_field", "b", "_FREQUENCIES",
* "position"); // there should be no positions
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScript("int_payload_field", "b", "_FREQUENCIES",
* "startOffset"); // there should be no offsets
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScript("int_payload_field", "b", "_FREQUENCIES",
* "endOffset"); // there should be no offsets
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScript("int_payload_field", "b", "_FREQUENCIES",
* "payloadAsInt(-1)"); // there should be no payloads
* checkArrayValsInEachDoc(script, emptyArray, 3);
*/
// check FLAG_POSITIONS flag
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_POSITIONS", "position");
// there should be positions
checkArrayValsInEachDocOldScriptAPI(script, expectedPositionsArray, 3);
/*
* TODO: these tests make a bogus assumption that asking for positions
* will return only positions script =
* createPositionsArrayScript("int_payload_field", "b", "_POSITIONS",
* "startOffset"); // there should be no offsets
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScript("int_payload_field", "b", "_POSITIONS",
* "endOffset"); // there should be no offsets
* checkArrayValsInEachDoc(script, emptyArray, 3); script =
* createPositionsArrayScript("int_payload_field", "b", "_POSITIONS",
* "payloadAsInt(-1)"); // there should be no payloads
* checkArrayValsInEachDoc(script, emptyArray, 3);
*/
// check FLAG_OFFSETS flag
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_OFFSETS", "position");
// there should be positions and s forth ...
checkArrayValsInEachDocOldScriptAPI(script, expectedPositionsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_OFFSETS", "startOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedStartOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_OFFSETS", "endOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedEndOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_OFFSETS", "payloadAsInt(-1)");
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 3);
// check FLAG_PAYLOADS flag
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_PAYLOADS", "position");
checkArrayValsInEachDocOldScriptAPI(script, expectedPositionsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_PAYLOADS", "startOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedStartOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_PAYLOADS", "endOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedEndOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", "_PAYLOADS", "payloadAsInt(-1)");
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 3);
// check all flags
String allFlags = "_POSITIONS | _OFFSETS | _PAYLOADS";
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", allFlags, "position");
checkArrayValsInEachDocOldScriptAPI(script, expectedPositionsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", allFlags, "startOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedStartOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", allFlags, "endOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedEndOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", allFlags, "payloadAsInt(-1)");
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 3);
// check all flags without record
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", includeAllWithoutRecordFlag, "position");
checkArrayValsInEachDocOldScriptAPI(script, expectedPositionsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", includeAllWithoutRecordFlag, "startOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedStartOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", includeAllWithoutRecordFlag, "endOffset");
checkArrayValsInEachDocOldScriptAPI(script, expectedEndOffsetsArray, 3);
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "b", includeAllWithoutRecordFlag, "payloadAsInt(-1)");
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 3);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testAllExceptPosAndOffsetOldSciptAPI() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("float_payload_field").field("type", "string").field("index_options", "offsets").field("term_vector", "no")
.field("analyzer", "payload_float").endObject().startObject("string_payload_field").field("type", "string")
.field("index_options", "offsets").field("term_vector", "no").field("analyzer", "payload_string").endObject()
.startObject("int_payload_field").field("type", "string").field("index_options", "offsets")
.field("analyzer", "payload_int").endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").addMapping("type1", mapping).setSettings(
Settings.settingsBuilder().put(indexSettings())
.put("index.analysis.analyzer.payload_float.tokenizer", "whitespace")
.putArray("index.analysis.analyzer.payload_float.filter", "delimited_float")
.put("index.analysis.filter.delimited_float.delimiter", "|")
.put("index.analysis.filter.delimited_float.encoding", "float")
.put("index.analysis.filter.delimited_float.type", "delimited_payload_filter")
.put("index.analysis.analyzer.payload_string.tokenizer", "whitespace")
.putArray("index.analysis.analyzer.payload_string.filter", "delimited_string")
.put("index.analysis.filter.delimited_string.delimiter", "|")
.put("index.analysis.filter.delimited_string.encoding", "identity")
.put("index.analysis.filter.delimited_string.type", "delimited_payload_filter")
.put("index.analysis.analyzer.payload_int.tokenizer", "whitespace")
.putArray("index.analysis.analyzer.payload_int.filter", "delimited_int")
.put("index.analysis.filter.delimited_int.delimiter", "|")
.put("index.analysis.filter.delimited_int.encoding", "int")
.put("index.analysis.filter.delimited_int.type", "delimited_payload_filter").put("index.number_of_shards", 1)));
ensureYellow();
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("float_payload_field", "a|1 b|2 a|3 b "), client()
.prepareIndex("test", "type1", "2").setSource("string_payload_field", "a|a b|b a|a b "),
client().prepareIndex("test", "type1", "3").setSource("float_payload_field", "a|4 b|5 a|6 b "),
client().prepareIndex("test", "type1", "4").setSource("string_payload_field", "a|b b|a a|b b "),
client().prepareIndex("test", "type1", "5").setSource("float_payload_field", "c "),
client().prepareIndex("test", "type1", "6").setSource("int_payload_field", "c|1"));
// get the number of all docs
String script = "_index.numDocs()";
checkValueInEachDocOldScriptAPI(6, script, 6);
// get the number of docs with field float_payload_field
script = "_index['float_payload_field'].docCount()";
checkValueInEachDocOldScriptAPI(3, script, 6);
// corner case: what if the field does not exist?
script = "_index['non_existent_field'].docCount()";
checkValueInEachDocOldScriptAPI(0, script, 6);
// get the number of all tokens in all docs
script = "_index['float_payload_field'].sumttf()";
checkValueInEachDocOldScriptAPI(9, script, 6);
// corner case get the number of all tokens in all docs for non existent
// field
script = "_index['non_existent_field'].sumttf()";
checkValueInEachDocOldScriptAPI(0, script, 6);
// get the sum of doc freqs in all docs
script = "_index['float_payload_field'].sumdf()";
checkValueInEachDocOldScriptAPI(5, script, 6);
// get the sum of doc freqs in all docs for non existent field
script = "_index['non_existent_field'].sumdf()";
checkValueInEachDocOldScriptAPI(0, script, 6);
// check term frequencies for 'a'
script = "term = _index['float_payload_field']['a']; if (term != null) {term.tf()}";
Map<String, Object> expectedResults = new HashMap<>();
expectedResults.put("1", 2);
expectedResults.put("2", 0);
expectedResults.put("3", 2);
expectedResults.put("4", 0);
expectedResults.put("5", 0);
expectedResults.put("6", 0);
checkValueInEachDocOldScriptAPI(script, expectedResults, 6);
expectedResults.clear();
// check doc frequencies for 'c'
script = "term = _index['float_payload_field']['c']; if (term != null) {term.df()}";
expectedResults.put("1", 1l);
expectedResults.put("2", 1l);
expectedResults.put("3", 1l);
expectedResults.put("4", 1l);
expectedResults.put("5", 1l);
expectedResults.put("6", 1l);
checkValueInEachDocOldScriptAPI(script, expectedResults, 6);
expectedResults.clear();
// check doc frequencies for term that does not exist
script = "term = _index['float_payload_field']['non_existent_term']; if (term != null) {term.df()}";
expectedResults.put("1", 0l);
expectedResults.put("2", 0l);
expectedResults.put("3", 0l);
expectedResults.put("4", 0l);
expectedResults.put("5", 0l);
expectedResults.put("6", 0l);
checkValueInEachDocOldScriptAPI(script, expectedResults, 6);
expectedResults.clear();
// check doc frequencies for term that does not exist
script = "term = _index['non_existent_field']['non_existent_term']; if (term != null) {term.tf()}";
expectedResults.put("1", 0);
expectedResults.put("2", 0);
expectedResults.put("3", 0);
expectedResults.put("4", 0);
expectedResults.put("5", 0);
expectedResults.put("6", 0);
checkValueInEachDocOldScriptAPI(script, expectedResults, 6);
expectedResults.clear();
// check total term frequencies for 'a'
script = "term = _index['float_payload_field']['a']; if (term != null) {term.ttf()}";
expectedResults.put("1", 4l);
expectedResults.put("2", 4l);
expectedResults.put("3", 4l);
expectedResults.put("4", 4l);
expectedResults.put("5", 4l);
expectedResults.put("6", 4l);
checkValueInEachDocOldScriptAPI(script, expectedResults, 6);
expectedResults.clear();
// check float payload for 'b'
HashMap<String, List<Object>> expectedPayloadsArray = new HashMap<>();
script = createPositionsArrayScriptOldScriptAPI("float_payload_field", "b", includeAllFlag, "payloadAsFloat(-1)");
float missingValue = -1;
List<Object> payloadsFor1 = new ArrayList<>();
payloadsFor1.add(2f);
payloadsFor1.add(missingValue);
expectedPayloadsArray.put("1", payloadsFor1);
List<Object> payloadsFor2 = new ArrayList<>();
payloadsFor2.add(5f);
payloadsFor2.add(missingValue);
expectedPayloadsArray.put("3", payloadsFor2);
expectedPayloadsArray.put("6", new ArrayList<>());
expectedPayloadsArray.put("5", new ArrayList<>());
expectedPayloadsArray.put("4", new ArrayList<>());
expectedPayloadsArray.put("2", new ArrayList<>());
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 6);
// check string payload for 'b'
expectedPayloadsArray.clear();
payloadsFor1.clear();
payloadsFor2.clear();
script = createPositionsArrayScriptOldScriptAPI("string_payload_field", "b", includeAllFlag, "payloadAsString()");
payloadsFor1.add("b");
payloadsFor1.add(null);
expectedPayloadsArray.put("2", payloadsFor1);
payloadsFor2.add("a");
payloadsFor2.add(null);
expectedPayloadsArray.put("4", payloadsFor2);
expectedPayloadsArray.put("6", new ArrayList<>());
expectedPayloadsArray.put("5", new ArrayList<>());
expectedPayloadsArray.put("3", new ArrayList<>());
expectedPayloadsArray.put("1", new ArrayList<>());
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 6);
// check int payload for 'c'
expectedPayloadsArray.clear();
payloadsFor1.clear();
payloadsFor2.clear();
script = createPositionsArrayScriptOldScriptAPI("int_payload_field", "c", includeAllFlag, "payloadAsInt(-1)");
payloadsFor1 = new ArrayList<>();
payloadsFor1.add(1);
expectedPayloadsArray.put("6", payloadsFor1);
expectedPayloadsArray.put("5", new ArrayList<>());
expectedPayloadsArray.put("4", new ArrayList<>());
expectedPayloadsArray.put("3", new ArrayList<>());
expectedPayloadsArray.put("2", new ArrayList<>());
expectedPayloadsArray.put("1", new ArrayList<>());
checkArrayValsInEachDocOldScriptAPI(script, expectedPayloadsArray, 6);
}
/*
* TODO Remove in 2.0
*/
private void checkArrayValsInEachDocOldScriptAPI(Script script, HashMap<String, List<Object>> expectedArray, int expectedHitSize) {
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script)
.execute().actionGet();
assertHitCount(sr, expectedHitSize);
int nullCounter = 0;
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("tvtest").getValues();
Object expectedResult = expectedArray.get(hit.getId());
assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
if (expectedResult != null) {
nullCounter++;
}
}
assertThat(nullCounter, equalTo(expectedArray.size()));
}
/*
* TODO Remove in 2.0
*/
private void checkExceptionsOldScriptAPI(String script) {
try {
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script)
.execute().actionGet();
assertThat(sr.getHits().hits().length, equalTo(0));
ShardSearchFailure[] shardFails = sr.getShardFailures();
for (ShardSearchFailure fail : shardFails) {
assertThat(fail.reason().indexOf("Cannot iterate twice! If you want to iterate more that once, add _CACHE explicitly."),
Matchers.greaterThan(-1));
}
} catch (SearchPhaseExecutionException ex) {
assertThat("got " + ex.toString(),
ex.toString().indexOf("Cannot iterate twice! If you want to iterate more that once, add _CACHE explicitly."),
Matchers.greaterThan(-1));
}
}
/*
* TODO Remove in 2.0
*/
private void checkValueInEachDocWithFunctionScoreOldScriptAPI(String fieldScript, Map<String, Object> expectedFieldVals,
String scoreScript, Map<String, Object> expectedScore, int numExpectedDocs) {
SearchResponse sr = client().prepareSearch("test")
.setQuery(QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.scriptFunction(new Script(scoreScript))))
.addScriptField("tvtest", fieldScript).execute().actionGet();
assertHitCount(sr, numExpectedDocs);
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("tvtest").getValues().get(0);
Object expectedResult = expectedFieldVals.get(hit.getId());
assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
assertThat("for doc " + hit.getId(), ((Float) expectedScore.get(hit.getId())).doubleValue(),
Matchers.closeTo(hit.score(), 1.e-4));
}
}
/*
* TODO Remove in 2.0
*/
private void checkOnlyFunctionScoreOldScriptAPI(String scoreScript, Map<String, Object> expectedScore, int numExpectedDocs) {
SearchResponse sr = client().prepareSearch("test")
.setQuery(QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.scriptFunction(new Script(scoreScript)))).execute()
.actionGet();
assertHitCount(sr, numExpectedDocs);
for (SearchHit hit : sr.getHits().getHits()) {
assertThat("for doc " + hit.getId(), ((Float) expectedScore.get(hit.getId())).doubleValue(),
Matchers.closeTo(hit.score(), 1.e-4));
}
}
/*
* TODO Remove in 2.0
*/
private void checkValueInEachDocOldScriptAPI(String script, Map<String, Object> expectedResults, int numExpectedDocs) {
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script)
.execute().actionGet();
assertHitCount(sr, numExpectedDocs);
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("tvtest").getValues().get(0);
Object expectedResult = expectedResults.get(hit.getId());
assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
}
}
/*
* TODO Remove in 2.0
*/
private void checkValueInEachDocOldScriptAPI(int value, String script, int numExpectedDocs) {
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script)
.execute().actionGet();
assertHitCount(sr, numExpectedDocs);
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("tvtest").getValues().get(0);
if (result instanceof Integer) {
assertThat((Integer) result, equalTo(value));
} else if (result instanceof Long) {
assertThat(((Long) result).intValue(), equalTo(value));
} else {
fail();
}
}
}
/*
* TODO Remove in 2.0
*/
private void checkArrayValsInEachDocOldScriptAPI(String script, HashMap<String, List<Object>> expectedArray, int expectedHitSize) {
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script)
.execute().actionGet();
assertHitCount(sr, expectedHitSize);
int nullCounter = 0;
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("tvtest").getValues();
Object expectedResult = expectedArray.get(hit.getId());
assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
if (expectedResult != null) {
nullCounter++;
}
}
assertThat(nullCounter, equalTo(expectedArray.size()));
}
}

View File

@ -132,27 +132,6 @@ public class IndexedScriptTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testDisabledUpdateIndexedScriptsOnlyOldScriptAPI() {
if (randomBoolean()) {
client().preparePutIndexedScript(GroovyScriptEngineService.NAME, "script1", "{\"script\":\"2\"}").get();
} else {
client().prepareIndex(ScriptService.SCRIPT_INDEX, GroovyScriptEngineService.NAME, "script1").setSource("{\"script\":\"2\"}")
.get();
}
client().prepareIndex("test", "scriptTest", "1").setSource("{\"theField\":\"foo\"}").get();
try {
client().prepareUpdate("test", "scriptTest", "1").setScript("script1", ScriptService.ScriptType.INDEXED).setScriptLang(GroovyScriptEngineService.NAME).get();
fail("update script should have been rejected");
} catch(Exception e) {
assertThat(e.getMessage(), containsString("failed to execute script"));
assertThat(ExceptionsHelper.detailedMessage(e), containsString("scripts of type [indexed], operation [update] and lang [groovy] are disabled"));
}
}
@Test
public void testDisabledAggsDynamicScripts() {
//dynamic scripts don't need to be enabled for an indexed script to be indexed and later on executed
@ -199,38 +178,4 @@ public class IndexedScriptTests extends ElasticsearchIntegrationTest {
assertThat(e.toString(), containsString("scripts of type [indexed], operation [aggs] and lang [expression] are disabled"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testAllOpsDisabledIndexedScriptsOldScriptAPI() throws IOException {
if (randomBoolean()) {
client().preparePutIndexedScript(ExpressionScriptEngineService.NAME, "script1", "{\"script\":\"2\"}").get();
} else {
client().prepareIndex(ScriptService.SCRIPT_INDEX, ExpressionScriptEngineService.NAME, "script1")
.setSource("{\"script\":\"2\"}").get();
}
client().prepareIndex("test", "scriptTest", "1").setSource("{\"theField\":\"foo\"}").get();
try {
client().prepareUpdate("test", "scriptTest", "1").setScript("script1", ScriptService.ScriptType.INDEXED).setScriptLang(ExpressionScriptEngineService.NAME).get();
fail("update script should have been rejected");
} catch(Exception e) {
assertThat(e.getMessage(), containsString("failed to execute script"));
assertThat(e.getCause().toString(), containsString("scripts of type [indexed], operation [update] and lang [expression] are disabled"));
}
try {
String query = "{ \"script_fields\" : { \"test1\" : { \"script_id\" : \"script1\", \"lang\":\"expression\" }}}";
client().prepareSearch().setSource(query).setIndices("test").setTypes("scriptTest").get();
fail("search script should have been rejected");
} catch(Exception e) {
assertThat(e.toString(), containsString("scripts of type [indexed], operation [search] and lang [expression] are disabled"));
}
try {
String source = "{\"aggs\": {\"test\": { \"terms\" : { \"script_id\":\"script1\", \"script_lang\":\"expression\" } } } }";
client().prepareSearch("test").setSource(source).get();
} catch(Exception e) {
assertThat(e.toString(), containsString("scripts of type [indexed], operation [aggs] and lang [expression] are disabled"));
}
}
}

View File

@ -147,34 +147,4 @@ public class OnDiskScriptTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testAllOpsDisabledOnDiskScriptsOldScriptAPI() {
//whether we even compile or cache the on disk scripts doesn't change the end result (the returned error)
client().prepareIndex("test", "scriptTest", "1").setSource("{\"theField\":\"foo\"}").get();
refresh();
String source = "{\"aggs\": {\"test\": { \"terms\" : { \"script_file\":\"script1\", \"lang\": \"mustache\" } } } }";
try {
client().prepareSearch("test").setSource(source).get();
fail("aggs script should have been rejected");
} catch (Exception e) {
assertThat(e.toString(), containsString("scripts of type [file], operation [aggs] and lang [mustache] are disabled"));
}
String query = "{ \"query\" : { \"match_all\": {}} , \"script_fields\" : { \"test1\" : { \"script_file\" : \"script1\", \"lang\":\"mustache\" }}, size:1}";
try {
client().prepareSearch().setSource(query).setIndices("test").setTypes("scriptTest").get();
fail("search script should have been rejected");
} catch (Exception e) {
assertThat(e.toString(), containsString("scripts of type [file], operation [search] and lang [mustache] are disabled"));
}
try {
client().prepareUpdate("test", "scriptTest", "1").setScript("script1", ScriptService.ScriptType.FILE).setScriptLang(MustacheScriptEngineService.NAME).get();
fail("update script should have been rejected");
} catch(Exception e) {
assertThat(e.getMessage(), containsString("failed to execute script"));
assertThat(e.getCause().toString(), containsString("scripts of type [file], operation [update] and lang [mustache] are disabled"));
}
}
}

View File

@ -77,34 +77,6 @@ public class ScriptFieldTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
public void testNativeScriptOldScriptAPI() throws InterruptedException, ExecutionException {
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("text", "doc1"), client()
.prepareIndex("test", "type1", "2").setSource("text", "doc2"),
client().prepareIndex("test", "type1", "3").setSource("text", "doc3"), client().prepareIndex("test", "type1", "4")
.setSource("text", "doc4"), client().prepareIndex("test", "type1", "5").setSource("text", "doc5"), client()
.prepareIndex("test", "type1", "6").setSource("text", "doc6"));
client().admin().indices().prepareFlush("test").execute().actionGet();
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())
.addScriptField("int", "native", "int", null).addScriptField("float", "native", "float", null)
.addScriptField("double", "native", "double", null).addScriptField("long", "native", "long", null).execute().actionGet();
assertThat(sr.getHits().hits().length, equalTo(6));
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("int").getValues().get(0);
assertThat(result, equalTo((Object) intArray));
result = hit.getFields().get("long").getValues().get(0);
assertThat(result, equalTo((Object) longArray));
result = hit.getFields().get("float").getValues().get(0);
assertThat(result, equalTo((Object) floatArray));
result = hit.getFields().get("double").getValues().get(0);
assertThat(result, equalTo((Object) doubleArray));
}
}
static class IntArrayScriptFactory implements NativeScriptFactory {
@Override
public ExecutableScript newScript(@Nullable Map<String, Object> params) {

View File

@ -1314,378 +1314,4 @@ public class DateHistogramTests extends ElasticsearchIntegrationTest {
assertThat(e.toString(), containsString("ElasticsearchParseException"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateHistogram("histo").field("date")
.script("new DateTime(_value).plusMonths(1).getMillis()")
.interval(DateHistogramInterval.MONTH))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(3));
DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(2l));
key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
}
/*
* TODO Remove in 2.0
*/
/**
* The script will change to document date values to the following:
*
* doc 1: [ Feb 2, Mar 3] doc 2: [ Mar 2, Apr 3] doc 3: [ Mar 15, Apr 16]
* doc 4: [ Apr 2, May 3] doc 5: [ Apr 15, May 16] doc 6: [ Apr 23, May 24]
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateHistogram("histo").field("dates")
.script("new DateTime(_value, DateTimeZone.UTC).plusMonths(1).getMillis()")
.interval(DateHistogramInterval.MONTH))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(4));
DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(5l));
key = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(3);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
}
/*
* TODO Remove in 2.0
*/
/**
* The script will change to document date values to the following:
*
* doc 1: [ Feb 2, Mar 3] doc 2: [ Mar 2, Apr 3] doc 3: [ Mar 15, Apr 16]
* doc 4: [ Apr 2, May 3] doc 5: [ Apr 15, May 16] doc 6: [ Apr 23, May 24]
*
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateHistogram("histo").field("dates")
.script("new DateTime((long)_value, DateTimeZone.UTC).plusMonths(1).getMillis()")
.interval(DateHistogramInterval.MONTH)
.subAggregation(max("max")))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(4));
DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
Max max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 3, 3, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 4, 16, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(5l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 5, 24, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(3);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 5, 24, 0, 0, DateTimeZone.UTC).getMillis()));
}
/*
* TODO Remove in 2.0
*/
/**
* Jan 2 Feb 2 Feb 15 Mar 2 Mar 15 Mar 23
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(dateHistogram("histo").script("doc['date'].value").interval(DateHistogramInterval.MONTH))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(3));
DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(2l));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_WithSubAggregator_InheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateHistogram("histo")
.script("doc['date'].value")
.interval(DateHistogramInterval.MONTH)
.subAggregation(max("max")))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(3));
DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
Max max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) new DateTime(2012, 1, 2, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(2l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) new DateTime(2012, 2, 15, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) new DateTime(2012, 3, 23, 0, 0, DateTimeZone.UTC).getMillis()));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(dateHistogram("histo").script("doc['dates'].values").interval(DateHistogramInterval.MONTH))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(4));
DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(5l));
key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(3);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateHistogram("histo")
.script("doc['dates'].values")
.interval(DateHistogramInterval.MONTH)
.subAggregation(max("max")))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(4));
DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
Histogram.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(1l));
Max max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 2, 3, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 3, 16, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(5l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 4, 24, 0, 0, DateTimeZone.UTC).getMillis()));
key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
bucket = buckets.get(3);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
assertThat(((DateTime) bucket.getKey()), equalTo(key));
assertThat(bucket.getDocCount(), equalTo(3l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat((long) max.getValue(), equalTo(new DateTime(2012, 4, 24, 0, 0, DateTimeZone.UTC).getMillis()));
}
}

View File

@ -1064,382 +1064,4 @@ public class DateRangeTests extends ElasticsearchIntegrationTest {
assertThat(buckets.get(0).getAggregations().asList().isEmpty(), is(true));
}
/*
* TODO Remove in 2.0
*/
@Test
public void dateMathOldScriptAPI() throws Exception {
DateRangeBuilder rangeBuilder = dateRange("range");
if (randomBoolean()) {
rangeBuilder.field("date");
} else {
rangeBuilder.script("doc['date'].value");
}
SearchResponse response = client().prepareSearch("idx")
.addAggregation(rangeBuilder
.addUnboundedTo("a long time ago", "now-50y")
.addRange("recently", "now-50y", "now-1y")
.addUnboundedFrom("last year", "now-1y"))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
assertThat(range.getBuckets().size(), equalTo(3));
// TODO: use diamond once JI-9019884 is fixed
List<Range.Bucket> buckets = new ArrayList<Range.Bucket>(range.getBuckets());
Range.Bucket bucket = buckets.get(0);
assertThat((String) bucket.getKey(), equalTo("a long time ago"));
assertThat(bucket.getKeyAsString(), equalTo("a long time ago"));
assertThat(bucket.getDocCount(), equalTo(0L));
bucket = buckets.get(1);
assertThat((String) bucket.getKey(), equalTo("recently"));
assertThat(bucket.getKeyAsString(), equalTo("recently"));
assertThat(bucket.getDocCount(), equalTo((long) numDocs));
bucket = buckets.get(2);
assertThat((String) bucket.getKey(), equalTo("last year"));
assertThat(bucket.getKeyAsString(), equalTo("last year"));
assertThat(bucket.getDocCount(), equalTo(0L));
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateRange("range").field("dates")
.script("new DateTime(_value.longValue(), DateTimeZone.UTC).plusMonths(1).getMillis()")
.addUnboundedTo(date(2, 15))
.addRange(date(2, 15), date(3, 15))
.addUnboundedFrom(date(3, 15)))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), nullValue());
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(1l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
assertThat(((DateTime) bucket.getTo()), nullValue());
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 1l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateRange("range").field("dates")
.script("new DateTime(_value.longValue(), DateTimeZone.UTC).plusMonths(1).getMillis()")
.addUnboundedTo(date(2, 15))
.addRange(date(2, 15), date(3, 15))
.addUnboundedFrom(date(3, 15))
.subAggregation(max("max")))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), nullValue());
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(1l));
Max max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) date(3, 3).getMillis()));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) date(4, 3).getMillis()));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
assertThat(((DateTime) bucket.getTo()), nullValue());
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 1l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateRange("range").script("doc['date'].value").addUnboundedTo(date(2, 15)).addRange(date(2, 15), date(3, 15))
.addUnboundedFrom(date(3, 15))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), nullValue());
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
assertThat(((DateTime) bucket.getTo()), nullValue());
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 4l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_WithSubAggregator_InheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateRange("range")
.script("doc['date'].value")
.addUnboundedTo(date(2, 15))
.addRange(date(2, 15), date(3, 15))
.addUnboundedFrom(date(3, 15))
.subAggregation(max("max")))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), nullValue());
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
Max max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) date(2, 2).getMillis()));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) date(3, 2).getMillis()));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
assertThat(((DateTime) bucket.getTo()), nullValue());
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 4l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateRange("range")
.script("doc['dates'].values")
.addUnboundedTo(date(2, 15))
.addRange(date(2, 15), date(3, 15))
.addUnboundedFrom(date(3, 15)))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), nullValue());
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(3l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
assertThat(((DateTime) bucket.getTo()), nullValue());
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 2l));
}
@Test
public void script_MultiValued_WithAggregatorInheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
dateRange("range")
.script("doc['dates'].values")
.addUnboundedTo(date(2, 15))
.addRange(date(2, 15), date(3, 15))
.addUnboundedFrom(date(3, 15))
.subAggregation(min("min")))
.execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), nullValue());
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(2l));
Min min = bucket.getAggregations().get("min");
assertThat(min, notNullValue());
assertThat(min.getValue(), equalTo((double) date(1, 2).getMillis()));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getDocCount(), equalTo(3l));
min = bucket.getAggregations().get("min");
assertThat(min, notNullValue());
assertThat(min.getValue(), equalTo((double) date(2, 2).getMillis()));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
assertThat(((DateTime) bucket.getTo()), nullValue());
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 2l));
min = bucket.getAggregations().get("min");
assertThat(min, notNullValue());
assertThat(min.getValue(), equalTo((double) date(2, 15).getMillis()));
}
}

View File

@ -1300,294 +1300,4 @@ public class DoubleTermsTests extends AbstractTermsTests {
public void otherDocCount() {
testOtherDocCount(SINGLE_VALUED_FIELD_NAME, MULTI_VALUED_FIELD_NAME);
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("_value + 1")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i + 1d)));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i + 1));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("_value + 1")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i + 1d)));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i + 1));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_NotUniqueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("(long) _value / 1000 + 1")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(1));
Terms.Bucket bucket = terms.getBucketByKey("1.0");
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("1.0"));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(1));
assertThat(bucket.getDocCount(), equalTo(5l));
}
/*
*
* [1, 2] [2, 3] [3, 4] [4, 5] [5, 6]
*
* 1 - count: 1 - sum: 1 2 - count: 2 - sum: 4 3 - count: 2 - sum: 6 4 -
* count: 2 - sum: 8 5 - count: 2 - sum: 10 6 - count: 1 - sum: 6
*/
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("_value + 1").subAggregation(sum("sum"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i + 1d)));
assertThat(bucket.getKeyAsNumber().doubleValue(), equalTo(i + 1d));
final long count = i == 0 || i == 5 ? 1 : 2;
double s = 0;
for (int j = 0; j < NUM_DOCS; ++j) {
if (i == j || i == j + 1) {
s += j + 1;
s += j + 1 + 1;
}
}
assertThat(bucket.getDocCount(), equalTo(count));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getValue(), equalTo(s));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
"doc['" + MULTI_VALUED_FIELD_NAME + "'].value")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (double) i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (double) i));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
"doc['" + MULTI_VALUED_FIELD_NAME + "']")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (double) i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (double) i));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInherited_NoExplicitTypeOldScriptAPI() throws Exception {
// since no type is explicitly defined, es will assume all values returned by the script to be strings (bytes),
// so the aggregation should fail, since the "sum" aggregation can only operation on numeric values.
try {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values()))
.script("doc['" + MULTI_VALUED_FIELD_NAME + "']").subAggregation(sum("sum"))).execute().actionGet();
fail("expected to fail as sub-aggregation sum requires a numeric value source context, but there is none");
} catch (Exception e) {
// expected
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInherited_WithExplicitTypeOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values()))
.script("doc['" + MULTI_VALUED_FIELD_NAME + "']").valueType(Terms.ValueType.DOUBLE)
.subAggregation(sum("sum"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + i + ".0");
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i + ".0"));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
final long count = i == 0 || i == 5 ? 1 : 2;
double s = 0;
for (int j = 0; j < NUM_DOCS; ++j) {
if (i == j || i == j + 1) {
s += j;
s += j + 1;
}
}
assertThat(bucket.getDocCount(), equalTo(count));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getValue(), equalTo(s));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_ScoreOldScriptAPI() {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.setQuery(
functionScoreQuery(matchAllQuery()).add(
ScoreFunctionBuilders.scriptFunction(new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value"))))
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script("ceil(_score.doubleValue()/3)"))
.execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(3));
for (int i = 0; i < 3; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (double) i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (double) i));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
assertThat(bucket.getDocCount(), equalTo(i == 1 ? 3L : 1L));
}
}
}

View File

@ -1018,255 +1018,4 @@ public class HistogramTests extends ElasticsearchIntegrationTest {
assertThat(e.toString(), containsString("Missing required field [interval]"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).script("_value + 1").interval(interval))
.execute().actionGet();
assertSearchResponse(response);
final int numBuckets = (numDocs + 1) / interval - 2 / interval + 1;
final long[] counts = new long[(numDocs + 1) / interval + 1];
for (int i = 0; i < numDocs; ++i) {
++counts[(i + 2) / interval];
}
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numBuckets));
for (int i = 0; i < numBuckets; i++) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
int key = ((2 / interval) + i) * interval;
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) key));
assertThat(bucket.getDocCount(), equalTo(counts[key / interval]));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(histogram("histo").field(MULTI_VALUED_FIELD_NAME).script("_value + 1").interval(interval))
.execute().actionGet();
assertSearchResponse(response);
final int numBuckets = (numDocs + 2) / interval - 2 / interval + 1;
final long[] counts = new long[(numDocs + 2) / interval + 1];
for (int i = 0; i < numDocs; ++i) {
final int bucket1 = (i + 2) / interval;
final int bucket2 = (i + 3) / interval;
++counts[bucket1];
if (bucket1 != bucket2) {
++counts[bucket2];
}
}
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numBuckets));
for (int i = 0; i < numBuckets; i++) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
int key = ((2 / interval) + i) * interval;
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) key));
assertThat(bucket.getDocCount(), equalTo(counts[key / interval]));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(histogram("histo").field(MULTI_VALUED_FIELD_NAME).script("_value + 1").interval(interval)
.subAggregation(terms(MULTI_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.term(true))))
.execute().actionGet();
assertSearchResponse(response);
final int numBuckets = (numDocs + 2) / interval - 2 / interval + 1;
final long[] counts = new long[(numDocs + 2) / interval + 1];
for (int i = 0; i < numDocs; ++i) {
final int bucket1 = (i + 2) / interval;
final int bucket2 = (i + 3) / interval;
++counts[bucket1];
if (bucket1 != bucket2) {
++counts[bucket2];
}
}
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numBuckets));
for (int i = 0; i < numBuckets; i++) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
int key = ((2 / interval) + i) * interval;
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) key));
assertThat(bucket.getDocCount(), equalTo(counts[key / interval]));
Terms terms = bucket.getAggregations().get(MULTI_VALUED_FIELD_NAME);
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo(MULTI_VALUED_FIELD_NAME));
int minTerm = Math.max(2, key - 1);
int maxTerm = Math.min(numDocs + 2, (key / interval + 1) * interval);
assertThat(terms.getBuckets().size(), equalTo(maxTerm - minTerm + 1));
Iterator<Terms.Bucket> iter = terms.getBuckets().iterator();
for (int j = minTerm; j <= maxTerm; ++j) {
assertThat(iter.next().getKeyAsNumber().longValue(), equalTo((long) j));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(histogram("histo").script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value").interval(interval))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numValueBuckets));
for (int i = 0; i < numValueBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valueCounts[i]));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_WithSubAggregator_InheritedOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(histogram("histo").script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value").interval(interval)
.subAggregation(sum("sum")))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
assertThat(histo.getBuckets().size(), equalTo(numValueBuckets));
// TODO: use diamond once JI-9019884 is fixed
List<Histogram.Bucket> buckets = new ArrayList<Histogram.Bucket>(histo.getBuckets());
for (int i = 0; i < numValueBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valueCounts[i]));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
long s = 0;
for (int j = 0; j < numDocs; ++j) {
if ((j + 1) / interval == i) {
s += j + 1;
}
}
assertThat(sum.getValue(), equalTo((double) s));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(histogram("histo").script("doc['" + MULTI_VALUED_FIELD_NAME + "']").interval(interval))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numValuesBuckets));
for (int i = 0; i < numValuesBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valuesCounts[i]));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInheritedOldScriptAPi() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(histogram("histo").script("doc['" + MULTI_VALUED_FIELD_NAME + "']").interval(interval)
.subAggregation(sum("sum")))
.execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numValuesBuckets));
for (int i = 0; i < numValuesBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valuesCounts[i]));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
long s = 0;
for (int j = 0; j < numDocs; ++j) {
if ((j + 1) / interval == i || (j + 2) / interval == i) {
s += j + 1;
s += j + 2;
}
}
assertThat(sum.getValue(), equalTo((double) s));
}
}
}

View File

@ -869,360 +869,4 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest {
assertThat(buckets.get(0).getToAsString(), equalTo("10.0.0.10"));
assertThat(buckets.get(0).getDocCount(), equalTo(0l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").field("ip").script("_value").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200")).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(100l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(55l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").field("ips").script("_value").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200")).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(101l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(56l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").field("ips").script("_value").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200").subAggregation(max("max"))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
Max max = bucket.getAggregations().get("max");
assertThat(max, Matchers.notNullValue());
assertThat((long) max.getValue(), equalTo(IpFieldMapper.ipToLong("10.0.0.100")));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(101l));
max = bucket.getAggregations().get("max");
assertThat(max, Matchers.notNullValue());
assertThat((long) max.getValue(), equalTo(IpFieldMapper.ipToLong("10.0.0.200")));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(56l));
max = bucket.getAggregations().get("max");
assertThat(max, Matchers.notNullValue());
assertThat((long) max.getValue(), equalTo(IpFieldMapper.ipToLong("10.0.0.255")));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").script("doc['ip'].value").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200")).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(100l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(55l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_WithSubAggregator_InheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").script("doc['ip'].value").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200").subAggregation(max("max"))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
Max max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.99")));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(100l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.199")));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(55l));
max = bucket.getAggregations().get("max");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.254")));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").script("doc['ips'].values").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200")).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(101l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(56l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
ipRange("range").script("doc['ips'].values").addUnboundedTo("10.0.0.100").addRange("10.0.0.100", "10.0.0.200")
.addUnboundedFrom("10.0.0.200").subAggregation(max("max"))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getDocCount(), equalTo(100l));
Max max = bucket.getAggregations().get("max");
assertThat(max, Matchers.notNullValue());
assertThat((long) max.getValue(), equalTo(IpFieldMapper.ipToLong("10.0.0.100")));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.100-10.0.0.200"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.100"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.100")));
assertThat(bucket.getToAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(bucket.getDocCount(), equalTo(101l));
max = bucket.getAggregations().get("max");
assertThat(max, Matchers.notNullValue());
assertThat((long) max.getValue(), equalTo(IpFieldMapper.ipToLong("10.0.0.200")));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("10.0.0.200-*"));
assertThat(bucket.getFromAsString(), equalTo("10.0.0.200"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo((double) IpFieldMapper.ipToLong("10.0.0.200")));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(56l));
max = bucket.getAggregations().get("max");
assertThat(max, Matchers.notNullValue());
assertThat((long) max.getValue(), equalTo(IpFieldMapper.ipToLong("10.0.0.255")));
}
}

View File

@ -1267,261 +1267,4 @@ public class LongTermsTests extends AbstractTermsTests {
public void otherDocCount() {
testOtherDocCount(SINGLE_VALUED_FIELD_NAME, MULTI_VALUED_FIELD_NAME);
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("_value + 1")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i + 1d)));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i + 1));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("_value - 1")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i - 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i - 1d)));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i - 1));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_NotUniqueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("floor(_value / 1000 + 1)")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(1));
Terms.Bucket bucket = terms.getBucketByKey("1.0");
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("1.0"));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(1));
assertThat(bucket.getDocCount(), equalTo(5l));
}
/*
*
* [1, 2] [2, 3] [3, 4] [4, 5] [5, 6]
*
* 1 - count: 1 - sum: 1 2 - count: 2 - sum: 4 3 - count: 2 - sum: 6 4 -
* count: 2 - sum: 8 5 - count: 2 - sum: 10 6 - count: 1 - sum: 6
*/
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))
.script("_value + 1").subAggregation(sum("sum"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i + 1d)));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i + 1));
final long count = i == 0 || i == 5 ? 1 : 2;
double s = 0;
for (int j = 0; j < NUM_DOCS; ++j) {
if (i == j || i == j + 1) {
s += j + 1;
s += j + 1 + 1;
}
}
assertThat(bucket.getDocCount(), equalTo(count));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getValue(), equalTo(s));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
"doc['" + SINGLE_VALUED_FIELD_NAME + "'].value")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
"doc['" + MULTI_VALUED_FIELD_NAME + "']")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInherited_NoExplicitTypeOldScriptAPI() throws Exception {
// since no type ie explicitly defined, es will assume all values returned by the script to be strings (bytes),
// so the aggregation should fail, since the "sum" aggregation can only operation on numeric values.
try {
client().prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values()))
.script("doc['" + MULTI_VALUED_FIELD_NAME + "']").subAggregation(sum("sum"))).execute().actionGet();
fail("expected to fail as sub-aggregation sum requires a numeric value source context, but there is none");
} catch (Exception e) {
// expected
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInherited_WithExplicitTypeOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values()))
.script("doc['" + MULTI_VALUED_FIELD_NAME + "']").valueType(Terms.ValueType.LONG)
.subAggregation(sum("sum"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
final long count = i == 0 || i == 5 ? 1 : 2;
double s = 0;
for (int j = 0; j < NUM_DOCS; ++j) {
if (i == j || i == j + 1) {
s += j;
s += j + 1;
}
}
assertThat(bucket.getDocCount(), equalTo(count));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getValue(), equalTo(s));
}
}
}

View File

@ -1105,391 +1105,4 @@ public class RangeTests extends ElasticsearchIntegrationTest {
assertThat(buckets.get(0).getDocCount(), equalTo(0l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").field(SINGLE_VALUED_FIELD_NAME).script("_value + 1").addUnboundedTo(3).addRange(3, 6)
.addUnboundedFrom(6)).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(1l)); // 2
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(3l)); // 3, 4, 5
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 4l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").field(MULTI_VALUED_FIELD_NAME).script("_value + 1").addUnboundedTo(3).addRange(3, 6)
.addUnboundedFrom(6)).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(1l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(4l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 3l));
}
/*
* [2, 3] [3, 4] [4, 5] [5, 6] [6, 7] [7, 8j [8, 9] [9, 10] [10, 11] [11,
* 12]
*
* r1: 2 r2: 3, 3, 4, 4, 5, 5 r3: 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12
*/
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").field(MULTI_VALUED_FIELD_NAME).script("_value + 1").addUnboundedTo(3).addRange(3, 6)
.addUnboundedFrom(6).subAggregation(sum("sum"))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(1l));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getName(), equalTo("sum"));
assertThat(sum.getValue(), equalTo(2d + 3d));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(4l));
sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getName(), equalTo("sum"));
assertThat(sum.getValue(), equalTo((double) 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 3L));
sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getName(), equalTo("sum"));
long total = 0;
for (int i = 3; i < numDocs; ++i) {
total += ((i + 1) + 1) + ((i + 1) + 2);
}
assertThat(sum.getValue(), equalTo((double) total));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value").addUnboundedTo(3).addRange(3, 6)
.addUnboundedFrom(6)).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(3l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 5l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_WithSubAggregator_InheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value").addUnboundedTo(3).addRange(3, 6)
.addUnboundedFrom(6).subAggregation(avg("avg"))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(2l));
Avg avg = bucket.getAggregations().get("avg");
assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo(1.5)); // (1 + 2) / 2
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(3l));
avg = bucket.getAggregations().get("avg");
assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo(4.0)); // (3 + 4 + 5) / 3
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 5l));
avg = bucket.getAggregations().get("avg");
assertThat(avg, notNullValue());
long total = 0;
for (int i = 5; i < numDocs; ++i) {
total += i + 1;
}
assertThat(avg.getValue(), equalTo((double) total / (numDocs - 5))); // (6 + 7 + 8 + 9 + 10) / 5
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").script("doc['" + MULTI_VALUED_FIELD_NAME + "'].values").addUnboundedTo(3).addRange(3, 6)
.addUnboundedFrom(6)).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(2l));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(4l));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 4l));
}
/*
* [1, 2] [2, 3] [3, 4] [4, 5] [5, 6] [6, 7] [7, 8j [8, 9] [9, 10] [10, 11]
*
* r1: 1, 2, 2 r2: 3, 3, 4, 4, 5, 5 r3: 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11
*/
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInheritedOldScript() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.addAggregation(
range("range").script("doc['" + MULTI_VALUED_FIELD_NAME + "'].values").addUnboundedTo("r1", 3).addRange("r2", 3, 6)
.addUnboundedFrom("r3", 6).subAggregation(sum("sum"))).execute().actionGet();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("r1"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(2l));
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getName(), equalTo("sum"));
assertThat(sum.getValue(), equalTo((double) 1 + 2 + 2 + 3));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("r2"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(4l));
sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getName(), equalTo("sum"));
assertThat(sum.getValue(), equalTo((double) 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("r3"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 4l));
sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
assertThat(sum.getName(), equalTo("sum"));
long total = 0;
for (int i = 4; i < numDocs; ++i) {
total += (i + 1) + (i + 2);
}
assertThat(sum.getValue(), equalTo((double) total));
}
}

View File

@ -508,30 +508,6 @@ public class SignificantTermsSignificanceScoreTests extends ElasticsearchIntegra
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testScriptScoreOldScriptAPI() throws ExecutionException, InterruptedException, IOException {
indexRandomFrequencies01(randomBoolean() ? "string" : "long");
ScriptHeuristic.ScriptHeuristicBuilder scriptHeuristicBuilder = getScriptSignificanceHeuristicBuilderOldScriptAPI();
ensureYellow();
SearchResponse response = client()
.prepareSearch(INDEX_NAME)
.addAggregation(
new TermsBuilder("class").field(CLASS_FIELD).subAggregation(
new SignificantTermsBuilder("mySignificantTerms").field(TEXT_FIELD).executionHint(randomExecutionHint())
.significanceHeuristic(scriptHeuristicBuilder).minDocCount(1).shardSize(2).size(2))).execute()
.actionGet();
assertSearchResponse(response);
for (Terms.Bucket classBucket : ((Terms) response.getAggregations().get("class")).getBuckets()) {
for (SignificantTerms.Bucket bucket : ((SignificantTerms) classBucket.getAggregations().get("mySignificantTerms")).getBuckets()) {
assertThat(bucket.getSignificanceScore(),
is((double) bucket.getSubsetDf() + bucket.getSubsetSize() + bucket.getSupersetDf() + bucket.getSupersetSize()));
}
}
}
@Test
public void testNoNumberFormatExceptionWithDefaultScriptingEngine() throws ExecutionException, InterruptedException, IOException {
assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1)));
@ -563,41 +539,6 @@ public class SignificantTermsSignificanceScoreTests extends ElasticsearchIntegra
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testNoNumberFormatExceptionWithDefaultScriptingEngineOldScriptAPI() throws ExecutionException, InterruptedException,
IOException {
assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1)));
index("test", "doc", "1", "{\"field\":\"a\"}");
index("test", "doc", "11", "{\"field\":\"a\"}");
index("test", "doc", "2", "{\"field\":\"b\"}");
index("test", "doc", "22", "{\"field\":\"b\"}");
index("test", "doc", "3", "{\"field\":\"a b\"}");
index("test", "doc", "33", "{\"field\":\"a b\"}");
ScriptHeuristic.ScriptHeuristicBuilder scriptHeuristicBuilder = new ScriptHeuristic.ScriptHeuristicBuilder();
scriptHeuristicBuilder.setScript("_subset_freq/(_superset_freq - _subset_freq + 1)");
ensureYellow();
refresh();
SearchResponse response = client().prepareSearch("test")
.addAggregation(new TermsBuilder("letters").field("field").subAggregation(new SignificantTermsBuilder("mySignificantTerms")
.field("field")
.executionHint(randomExecutionHint())
.significanceHeuristic(scriptHeuristicBuilder)
.minDocCount(1).shardSize(2).size(2)))
.execute()
.actionGet();
assertSearchResponse(response);
assertThat(((Terms) response.getAggregations().get("letters")).getBuckets().size(), equalTo(2));
for (Terms.Bucket classBucket : ((Terms) response.getAggregations().get("letters")).getBuckets()) {
assertThat(((SignificantStringTerms) classBucket.getAggregations().get("mySignificantTerms")).getBuckets().size(), equalTo(2));
for (SignificantTerms.Bucket bucket : ((SignificantTerms) classBucket.getAggregations().get("mySignificantTerms")).getBuckets()) {
assertThat(bucket.getSignificanceScore(), closeTo((double)bucket.getSubsetDf() /(bucket.getSupersetDf() - bucket.getSubsetDf()+ 1), 1.e-6));
}
}
}
private ScriptHeuristic.ScriptHeuristicBuilder getScriptSignificanceHeuristicBuilder() throws IOException {
Map<String, Object> params = null;
Script script = null;
@ -659,73 +600,6 @@ public class SignificantTermsSignificanceScoreTests extends ElasticsearchIntegra
return builder;
}
/*
* TODO Remove in 2.0
*/
private ScriptHeuristic.ScriptHeuristicBuilder getScriptSignificanceHeuristicBuilderOldScriptAPI() throws IOException {
Map<String, Object> params = null;
String script = null;
String lang = null;
String scriptId = null;
String scriptFile = null;
if (randomBoolean()) {
params = new HashMap<>();
params.put("param", randomIntBetween(1, 100));
}
int randomScriptKind = randomIntBetween(0, 3);
if (randomBoolean()) {
lang = "groovy";
}
switch (randomScriptKind) {
case 0: {
if (params == null) {
script = "return _subset_freq + _subset_size + _superset_freq + _superset_size";
} else {
script = "return param*(_subset_freq + _subset_size + _superset_freq + _superset_size)/param";
}
break;
}
case 1: {
if (params == null) {
script = "return _subset_freq + _subset_size + _superset_freq + _superset_size";
} else {
script = "return param*(_subset_freq + _subset_size + _superset_freq + _superset_size)/param";
}
client().prepareIndex().setIndex(ScriptService.SCRIPT_INDEX).setType(ScriptService.DEFAULT_LANG).setId("my_script")
.setSource(XContentFactory.jsonBuilder().startObject()
.field("script", script)
.endObject()).get();
refresh();
scriptId = "my_script";
script = null;
break;
}
case 2: {
if (params == null) {
scriptFile = "significance_script_no_params";
} else {
scriptFile = "significance_script_with_params";
}
break;
}
case 3: {
logger.info("NATIVE SCRIPT");
if (params == null) {
script = "native_significance_score_script_no_params";
} else {
script = "native_significance_score_script_with_params";
}
lang = "native";
if (randomBoolean()) {
}
break;
}
}
ScriptHeuristic.ScriptHeuristicBuilder builder = new ScriptHeuristic.ScriptHeuristicBuilder().setScript(script).setLang(lang).setParams(params).setScriptId(scriptId).setScriptFile(scriptFile);
return builder;
}
private void indexRandomFrequencies01(String type) throws ExecutionException, InterruptedException {
String mappings = "{\"" + DOC_TYPE + "\": {\"properties\":{\"" + TEXT_FIELD + "\": {\"type\":\"" + type + "\"}}}}";
assertAcked(prepareCreate(INDEX_NAME).addMapping(DOC_TYPE, mappings));
@ -744,4 +618,4 @@ public class SignificantTermsSignificanceScoreTests extends ElasticsearchIntegra
}
indexRandom(true, indexRequestBuilderList);
}
}
}

View File

@ -1584,322 +1584,4 @@ public class StringTermsTests extends AbstractTermsTests {
public void otherDocCount() {
testOtherDocCount(SINGLE_VALUED_FIELD_NAME, MULTI_VALUED_FIELD_NAME);
}
/*
* TODO Remove in 2.0
*/
@Test
public void singleValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values())).script("'foo_' + _value")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("foo_val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("foo_val" + i));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_NotUniqueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values())).script("_value.substring(0,3)")).execute()
.actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(1));
Terms.Bucket bucket = terms.getBucketByKey("val");
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val"));
assertThat(bucket.getDocCount(), equalTo(5l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).script("doc['" + MULTI_VALUED_FIELD_NAME + "']")
.collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScriptOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values())).script("'foo_' + _value")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("foo_val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("foo_val" + i));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
*
* [foo_val0, foo_val1] [foo_val1, foo_val2] [foo_val2, foo_val3] [foo_val3,
* foo_val4] [foo_val4, foo_val5]
*
*
* foo_val0 - doc_count: 1 - val_count: 2 foo_val1 - doc_count: 2 -
* val_count: 4 foo_val2 - doc_count: 2 - val_count: 4 foo_val3 - doc_count:
* 2 - val_count: 4 foo_val4 - doc_count: 2 - val_count: 4 foo_val5 -
* doc_count: 1 - val_count: 2
*/
/*
* TODO Remove in 2.0
*/
@Test
public void multiValuedField_WithValueScript_WithInheritedSubAggregatorOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values())).script("'foo_' + _value")
.subAggregation(count("count"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("foo_val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("foo_val" + i));
if (i == 0 | i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
ValueCount valueCount = bucket.getAggregations().get("count");
assertThat(valueCount, notNullValue());
assertThat(valueCount.getValue(), equalTo(2l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
ValueCount valueCount = bucket.getAggregations().get("count");
assertThat(valueCount, notNullValue());
assertThat("term[" + key(bucket) + "]", valueCount.getValue(), equalTo(4l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
.script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_ExplicitSingleValueOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
.script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_SingleValue_WithSubAggregator_InheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
.script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value").subAggregation(count("count"))).execute()
.actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(1l));
ValueCount valueCount = bucket.getAggregations().get("count");
assertThat(valueCount, notNullValue());
assertThat(valueCount.getValue(), equalTo(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValuedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
.script("doc['" + MULTI_VALUED_FIELD_NAME + "']")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void script_MultiValued_WithAggregatorInheritedOldScriptAPI() throws Exception {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.addAggregation(
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
.script("doc['" + MULTI_VALUED_FIELD_NAME + "']").subAggregation(count("count"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
if (i == 0 | i == 5) {
assertThat(bucket.getDocCount(), equalTo(1l));
ValueCount valueCount = bucket.getAggregations().get("count");
assertThat(valueCount, notNullValue());
assertThat(valueCount.getValue(), equalTo(2l));
} else {
assertThat(bucket.getDocCount(), equalTo(2l));
ValueCount valueCount = bucket.getAggregations().get("count");
assertThat(valueCount, notNullValue());
assertThat(valueCount.getValue(), equalTo(4l));
}
}
}
}

View File

@ -935,114 +935,4 @@ public class TopHitsTests extends ElasticsearchIntegrationTest {
}
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testFetchFeaturesOldScriptAPI() {
SearchResponse response = client()
.prepareSearch("idx")
.setTypes("type")
.setQuery(matchQuery("text", "text").queryName("test"))
.addAggregation(
terms("terms")
.executionHint(randomExecutionHint())
.field(TERMS_AGGS_FIELD)
.subAggregation(
topHits("hits").setSize(1).addHighlightedField("text").setExplain(true).addFieldDataField("field1")
.addScriptField("script", "doc['field1'].value").setFetchSource("text", null)
.setVersion(true))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (Terms.Bucket bucket : terms.getBuckets()) {
TopHits topHits = bucket.getAggregations().get("hits");
SearchHits hits = topHits.getHits();
assertThat(hits.totalHits(), equalTo(10l));
assertThat(hits.getHits().length, equalTo(1));
SearchHit hit = hits.getAt(0);
HighlightField highlightField = hit.getHighlightFields().get("text");
assertThat(highlightField.getFragments().length, equalTo(1));
assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em> to entertain"));
Explanation explanation = hit.explanation();
assertThat(explanation.toString(), containsString("text:text"));
long version = hit.version();
assertThat(version, equalTo(1l));
assertThat(hit.matchedQueries()[0], equalTo("test"));
SearchHitField field = hit.field("field1");
assertThat(field.getValue().toString(), equalTo("5"));
field = hit.field("script");
assertThat(field.getValue().toString(), equalTo("5"));
assertThat(hit.sourceAsMap().size(), equalTo(1));
assertThat(hit.sourceAsMap().get("text").toString(), equalTo("some text to entertain"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testNestedFetchFeaturesOldScriptAPI() {
String hlType = randomFrom("plain", "fvh", "postings");
HighlightBuilder.Field hlField = new HighlightBuilder.Field("comments.message")
.highlightQuery(matchQuery("comments.message", "comment")).forceSource(randomBoolean()) // randomly from stored field or _source
.highlighterType(hlType);
SearchResponse searchResponse = client()
.prepareSearch("articles")
.setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test")))
.addAggregation(
nested("to-comments").path("comments").subAggregation(
topHits("top-comments").setSize(1).addHighlightedField(hlField).setExplain(true)
.addFieldDataField("comments.user").addScriptField("script", "doc['comments.user'].value")
.setFetchSource("message", null).setVersion(true).addSort("comments.date", SortOrder.ASC))).get();
assertHitCount(searchResponse, 2);
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4l));
SearchHits hits = ((TopHits) nested.getAggregations().get("top-comments")).getHits();
assertThat(hits.totalHits(), equalTo(4l));
SearchHit searchHit = hits.getAt(0);
assertThat(searchHit.getId(), equalTo("1"));
assertThat(searchHit.getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHit.getNestedIdentity().getOffset(), equalTo(0));
HighlightField highlightField = searchHit.getHighlightFields().get("comments.message");
assertThat(highlightField.getFragments().length, equalTo(1));
assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>comment</em>"));
// Can't explain nested hit with the main query, since both are in a different scopes, also the nested doc may not even have matched with the main query
// If top_hits would have a query option then we can explain that query
Explanation explanation = searchHit.explanation();
assertFalse(explanation.isMatch());
// Returns the version of the root document. Nested docs don't have a separate version
long version = searchHit.version();
assertThat(version, equalTo(1l));
// Can't use named queries for the same reason explain doesn't work:
assertThat(searchHit.matchedQueries(), arrayContaining("test"));
SearchHitField field = searchHit.field("comments.user");
assertThat(field.getValue().toString(), equalTo("a"));
field = searchHit.field("script");
assertThat(field.getValue().toString(), equalTo("a"));
assertThat(searchHit.sourceAsMap().size(), equalTo(1));
assertThat(searchHit.sourceAsMap().get("message").toString(), equalTo("some comment"));
}
}

View File

@ -749,664 +749,4 @@ public class ScriptedMetricTests extends ElasticsearchIntegrationTest {
assertThat(scriptedMetric.getName(), equalTo("scripted"));
assertThat(scriptedMetric.aggregation(), nullValue());
}
/*
* TODO Remove in 2.0
*/
@Test
public void testMapOldScriptAPI() {
SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery())
.addAggregation(scriptedMetric("scripted").mapScript("_agg['count'] = 1")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
int numShardsRun = 0;
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(Map.class));
Map<String, Object> map = (Map<String, Object>) object;
assertThat(map.size(), lessThanOrEqualTo(1));
if (map.size() == 1) {
assertThat(map.get("count"), notNullValue());
assertThat(map.get("count"), instanceOf(Number.class));
assertThat((Number) map.get("count"), equalTo((Number) 1));
numShardsRun++;
}
}
// We don't know how many shards will have documents but we need to make
// sure that at least one shard ran the map script
assertThat(numShardsRun, greaterThan(0));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testMap_withParamsOldScriptAPI() {
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery())
.addAggregation(scriptedMetric("scripted").params(params).mapScript("_agg.add(1)")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
long totalCount = 0;
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(List.class));
List<?> list = (List<?>) object;
for (Object o : list) {
assertThat(o, notNullValue());
assertThat(o, instanceOf(Number.class));
Number numberValue = (Number) o;
assertThat(numberValue, equalTo((Number) 1));
totalCount += numberValue.longValue();
}
}
assertThat(totalCount, equalTo(numDocs));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMap_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted").params(params).initScript("vars.multiplier = 3").mapScript("_agg.add(vars.multiplier)"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
long totalCount = 0;
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(List.class));
List<?> list = (List<?>) object;
for (Object o : list) {
assertThat(o, notNullValue());
assertThat(o, instanceOf(Number.class));
Number numberValue = (Number) o;
assertThat(numberValue, equalTo((Number) 3));
totalCount += numberValue.longValue();
}
}
assertThat(totalCount, equalTo(numDocs * 3));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testMapCombine_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.mapScript("_agg.add(1)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
long totalCount = 0;
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(List.class));
List<?> list = (List<?>) object;
for (Object o : list) {
assertThat(o, notNullValue());
assertThat(o, instanceOf(Number.class));
Number numberValue = (Number) o;
// A particular shard may not have any documents stored on it so
// we have to assume the lower bound may be 0. The check at the
// bottom of the test method will make sure the count is correct
assertThat(numberValue.longValue(), allOf(greaterThanOrEqualTo(0l), lessThanOrEqualTo(numDocs)));
totalCount += numberValue.longValue();
}
}
assertThat(totalCount, equalTo(numDocs));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapCombine_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
long totalCount = 0;
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(List.class));
List<?> list = (List<?>) object;
for (Object o : list) {
assertThat(o, notNullValue());
assertThat(o, instanceOf(Number.class));
Number numberValue = (Number) o;
// A particular shard may not have any documents stored on it so
// we have to assume the lower bound may be 0. The check at the
// bottom of the test method will make sure the count is correct
assertThat(numberValue.longValue(), allOf(greaterThanOrEqualTo(0l), lessThanOrEqualTo(numDocs * 3)));
totalCount += numberValue.longValue();
}
}
assertThat(totalCount, equalTo(numDocs * 3));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapCombineReduce_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
}
/*
* TODO Remove in 2.0
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testInitMapCombineReduce_getPropertyOldScriptAPI() throws Exception {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse searchResponse = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
global("global")
.subAggregation(
scriptedMetric("scripted")
.params(params)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation")))
.execute().actionGet();
assertSearchResponse(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(numDocs));
Global global = searchResponse.getAggregations().get("global");
assertThat(global, notNullValue());
assertThat(global.getName(), equalTo("global"));
assertThat(global.getDocCount(), equalTo(numDocs));
assertThat(global.getAggregations(), notNullValue());
assertThat(global.getAggregations().asMap().size(), equalTo(1));
ScriptedMetric scriptedMetricAggregation = global.getAggregations().get("scripted");
assertThat(scriptedMetricAggregation, notNullValue());
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
assertThat((ScriptedMetric) global.getProperty("scripted"), sameInstance(scriptedMetricAggregation));
assertThat((List) global.getProperty("scripted.value"), sameInstance((List) aggregationList));
assertThat((List) scriptedMetricAggregation.getProperty("value"), sameInstance((List) aggregationList));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testMapCombineReduce_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapReduce_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testMapReduce_withParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.mapScript("_agg.add(vars.multiplier)")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapCombineReduce_withParamsAndReduceParamsOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
Map<String, Object> reduceParams = new HashMap<>();
reduceParams.put("multiplier", 4);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted")
.params(params)
.reduceParams(reduceParams)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum * multiplier); return newaggregation"))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 12));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapCombineReduce_withParams_IndexedOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted").params(params).initScriptId("initScript_indexed").mapScriptId("mapScript_indexed")
.combineScriptId("combineScript_indexed").reduceScriptId("reduceScript_indexed")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapCombineReduce_withParams_FileOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(
scriptedMetric("scripted").params(params).initScriptFile("init_script").mapScriptFile("map_script")
.combineScriptFile("combine_script").reduceScriptFile("reduce_script")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testInitMapCombineReduce_withParams_asSubAggOldScriptAPI() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse response = client()
.prepareSearch("idx")
.setQuery(matchAllQuery())
.setSize(1000)
.addAggregation(
histogram("histo")
.field("l_value")
.interval(1)
.subAggregation(
scriptedMetric("scripted")
.params(params)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation")))
.execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
Aggregation aggregation = response.getAggregations().get("histo");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(Histogram.class));
Histogram histoAgg = (Histogram) aggregation;
assertThat(histoAgg.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histoAgg.getBuckets();
assertThat(buckets, notNullValue());
for (Bucket b : buckets) {
assertThat(b, notNullValue());
assertThat(b.getDocCount(), equalTo(1l));
Aggregations subAggs = b.getAggregations();
assertThat(subAggs, notNullValue());
assertThat(subAggs.asList().size(), equalTo(1));
Aggregation subAgg = subAggs.get("scripted");
assertThat(subAgg, notNullValue());
assertThat(subAgg, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) subAgg;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(1));
Object object = aggregationList.get(0);
assertThat(object, notNullValue());
assertThat(object, instanceOf(Number.class));
assertThat(((Number) object).longValue(), equalTo(3l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testEmptyAggregationOldScriptAPI() throws Exception {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Map<String, Object> params = new HashMap<>();
params.put("_agg", new ArrayList<>());
params.put("vars", varsMap);
SearchResponse searchResponse = client()
.prepareSearch("empty_bucket_idx")
.setQuery(matchAllQuery())
.addAggregation(
histogram("histo")
.field("value")
.interval(1l)
.minDocCount(0)
.subAggregation(
scriptedMetric("scripted")
.params(params)
.initScript("vars.multiplier = 3")
.mapScript("_agg.add(vars.multiplier)")
.combineScript(
"newaggregation = []; sum = 0;for (a in _agg) { sum += a}; newaggregation.add(sum); return newaggregation")
.reduceScript(
"newaggregation = []; sum = 0;for (aggregation in _aggs) { for (a in aggregation) { sum += a} }; newaggregation.add(sum); return newaggregation")))
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
Histogram histo = searchResponse.getAggregations().get("histo");
assertThat(histo, notNullValue());
Histogram.Bucket bucket = histo.getBuckets().get(1);
assertThat(bucket, notNullValue());
ScriptedMetric scriptedMetric = bucket.getAggregations().get("scripted");
assertThat(scriptedMetric, notNullValue());
assertThat(scriptedMetric.getName(), equalTo("scripted"));
assertThat(scriptedMetric.aggregation(), nullValue());
}
}

View File

@ -35,6 +35,8 @@ import org.elasticsearch.client.Requests;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
@ -445,8 +447,8 @@ public class TransportTwoNodesSearchTests extends ElasticsearchIntegrationTest {
logger.info("Start Testing failed multi search with a wrong query");
MultiSearchResponse response = client().prepareMultiSearch()
// Add custom score query with missing script
.add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1)).add(new ScriptScoreFunctionBuilder())))
// Add custom score query with bogus script
.add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1)).add(new ScriptScoreFunctionBuilder(new Script("foo", ScriptService.ScriptType.INLINE, "bar", null)))))
.add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2)))
.add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
.execute().actionGet();

View File

@ -60,8 +60,8 @@ import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.factorFunction;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.weightFactorFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
@ -1583,9 +1583,9 @@ public class ChildQuerySearchTests extends ElasticsearchIntegrationTest {
.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(constantScoreQuery(QueryBuilders.termQuery("foo", "two"))).boostMode("replace").scoreMode("sum")
.add(QueryBuilders.matchAllQuery(), factorFunction(1))
.add(QueryBuilders.termQuery("foo", "three"), factorFunction(1))
.add(QueryBuilders.termQuery("foo", "four"), factorFunction(1))).scoreType(scoreType)
.add(QueryBuilders.matchAllQuery(), weightFactorFunction(1))
.add(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1))
.add(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1))).scoreType(scoreType)
.minChildren(minChildren).maxChildren(maxChildren).setShortCircuitCutoff(cutoff))
.addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).get();
}

View File

@ -610,219 +610,4 @@ public class SearchFieldsTests extends ElasticsearchIntegrationTest {
assertThat(fields.get("md").getValues(), equalTo(Arrays.<Object> asList((double) id, id + 1d)));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testScriptDocAndFieldsOldScriptAPI() throws Exception {
createIndex("test");
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("num1")
.field("type", "double").field("store", "yes").endObject().endObject().endObject().endObject().string();
client().admin().indices().preparePutMapping().setType("type1").setSource(mapping).execute().actionGet();
client().prepareIndex("test", "type1", "1")
.setSource(
jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).field("date", "1970-01-01T00:00:00")
.endObject()).execute().actionGet();
client().admin().indices().prepareFlush().execute().actionGet();
client().prepareIndex("test", "type1", "2")
.setSource(
jsonBuilder().startObject().field("test", "value beck").field("num1", 2.0f).field("date", "1970-01-01T00:00:25")
.endObject()).execute().actionGet();
client().admin().indices().prepareFlush().execute().actionGet();
client().prepareIndex("test", "type1", "3")
.setSource(
jsonBuilder().startObject().field("test", "value beck").field("num1", 3.0f).field("date", "1970-01-01T00:02:00")
.endObject()).execute().actionGet();
client().admin().indices().refresh(refreshRequest()).actionGet();
logger.info("running doc['num1'].value");
SearchResponse response = client().prepareSearch().setQuery(matchAllQuery()).addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "doc['num1'].value").addScriptField("sNum1_field", "_fields['num1'].value")
.addScriptField("date1", "doc['date'].date.millis").execute().actionGet();
assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo(3l));
assertThat(response.getHits().getAt(0).isSourceEmpty(), equalTo(true));
assertThat(response.getHits().getAt(0).id(), equalTo("1"));
assertThat(response.getHits().getAt(0).fields().size(), equalTo(3));
assertThat((Double) response.getHits().getAt(0).fields().get("sNum1").values().get(0), equalTo(1.0));
assertThat((Double) response.getHits().getAt(0).fields().get("sNum1_field").values().get(0), equalTo(1.0));
assertThat((Long) response.getHits().getAt(0).fields().get("date1").values().get(0), equalTo(0l));
assertThat(response.getHits().getAt(1).id(), equalTo("2"));
assertThat(response.getHits().getAt(1).fields().size(), equalTo(3));
assertThat((Double) response.getHits().getAt(1).fields().get("sNum1").values().get(0), equalTo(2.0));
assertThat((Double) response.getHits().getAt(1).fields().get("sNum1_field").values().get(0), equalTo(2.0));
assertThat((Long) response.getHits().getAt(1).fields().get("date1").values().get(0), equalTo(25000l));
assertThat(response.getHits().getAt(2).id(), equalTo("3"));
assertThat(response.getHits().getAt(2).fields().size(), equalTo(3));
assertThat((Double) response.getHits().getAt(2).fields().get("sNum1").values().get(0), equalTo(3.0));
assertThat((Double) response.getHits().getAt(2).fields().get("sNum1_field").values().get(0), equalTo(3.0));
assertThat((Long) response.getHits().getAt(2).fields().get("date1").values().get(0), equalTo(120000l));
logger.info("running doc['num1'].value * factor");
Map<String, Object> params = MapBuilder.<String, Object> newMapBuilder().put("factor", 2.0).map();
response = client().prepareSearch().setQuery(matchAllQuery()).addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "doc['num1'].value * factor", params).execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
assertThat(response.getHits().getAt(0).id(), equalTo("1"));
assertThat(response.getHits().getAt(0).fields().size(), equalTo(1));
assertThat((Double) response.getHits().getAt(0).fields().get("sNum1").values().get(0), equalTo(2.0));
assertThat(response.getHits().getAt(1).id(), equalTo("2"));
assertThat(response.getHits().getAt(1).fields().size(), equalTo(1));
assertThat((Double) response.getHits().getAt(1).fields().get("sNum1").values().get(0), equalTo(4.0));
assertThat(response.getHits().getAt(2).id(), equalTo("3"));
assertThat(response.getHits().getAt(2).fields().size(), equalTo(1));
assertThat((Double) response.getHits().getAt(2).fields().get("sNum1").values().get(0), equalTo(6.0));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUidBasedScriptFieldsOldScriptAPI() throws Exception {
prepareCreate("test").addMapping("type1", "num1", "type=long").execute().actionGet();
ensureYellow();
int numDocs = randomIntBetween(1, 30);
IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs];
for (int i = 0; i < numDocs; i++) {
indexRequestBuilders[i] = client().prepareIndex("test", "type1", Integer.toString(i)).setSource(
jsonBuilder().startObject().field("num1", i).endObject());
}
indexRandom(true, indexRequestBuilders);
SearchResponse response = client().prepareSearch().setQuery(matchAllQuery()).addSort("num1", SortOrder.ASC).setSize(numDocs)
.addScriptField("uid", "_fields._uid.value").get();
assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo((long) numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(response.getHits().getAt(i).fields().size(), equalTo(1));
assertThat((String) response.getHits().getAt(i).fields().get("uid").value(), equalTo("type1#" + Integer.toString(i)));
}
response = client().prepareSearch().setQuery(matchAllQuery()).addSort("num1", SortOrder.ASC).setSize(numDocs)
.addScriptField("id", "_fields._id.value").get();
assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo((long) numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(response.getHits().getAt(i).fields().size(), equalTo(1));
assertThat((String) response.getHits().getAt(i).fields().get("id").value(), equalTo(Integer.toString(i)));
}
response = client().prepareSearch().setQuery(matchAllQuery()).addSort("num1", SortOrder.ASC).setSize(numDocs)
.addScriptField("type", "_fields._type.value").get();
assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo((long) numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(response.getHits().getAt(i).fields().size(), equalTo(1));
assertThat((String) response.getHits().getAt(i).fields().get("type").value(), equalTo("type1"));
}
response = client().prepareSearch().setQuery(matchAllQuery()).addSort("num1", SortOrder.ASC).setSize(numDocs)
.addScriptField("id", "_fields._id.value").addScriptField("uid", "_fields._uid.value")
.addScriptField("type", "_fields._type.value").get();
assertNoFailures(response);
assertThat(response.getHits().totalHits(), equalTo((long) numDocs));
for (int i = 0; i < numDocs; i++) {
assertThat(response.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(response.getHits().getAt(i).fields().size(), equalTo(3));
assertThat((String) response.getHits().getAt(i).fields().get("uid").value(), equalTo("type1#" + Integer.toString(i)));
assertThat((String) response.getHits().getAt(i).fields().get("type").value(), equalTo("type1"));
assertThat((String) response.getHits().getAt(i).fields().get("id").value(), equalTo(Integer.toString(i)));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testScriptFieldUsingSourceOldScriptAPI() throws Exception {
createIndex("test");
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
client().prepareIndex("test", "type1", "1")
.setSource(
jsonBuilder().startObject().startObject("obj1").field("test", "something").endObject().startObject("obj2")
.startArray("arr2").value("arr_value1").value("arr_value2").endArray().endObject().startArray("arr3")
.startObject().field("arr3_field1", "arr3_value1").endObject().endArray().endObject()).execute()
.actionGet();
client().admin().indices().refresh(refreshRequest()).actionGet();
SearchResponse response = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("s_obj1", "_source.obj1")
.addScriptField("s_obj1_test", "_source.obj1.test").addScriptField("s_obj2", "_source.obj2")
.addScriptField("s_obj2_arr2", "_source.obj2.arr2").addScriptField("s_arr3", "_source.arr3").execute().actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
assertThat(response.getHits().getAt(0).field("s_obj1_test").value().toString(), equalTo("something"));
Map<String, Object> sObj1 = response.getHits().getAt(0).field("s_obj1").value();
assertThat(sObj1.get("test").toString(), equalTo("something"));
assertThat(response.getHits().getAt(0).field("s_obj1_test").value().toString(), equalTo("something"));
Map<String, Object> sObj2 = response.getHits().getAt(0).field("s_obj2").value();
List sObj2Arr2 = (List) sObj2.get("arr2");
assertThat(sObj2Arr2.size(), equalTo(2));
assertThat(sObj2Arr2.get(0).toString(), equalTo("arr_value1"));
assertThat(sObj2Arr2.get(1).toString(), equalTo("arr_value2"));
sObj2Arr2 = response.getHits().getAt(0).field("s_obj2_arr2").values();
assertThat(sObj2Arr2.size(), equalTo(2));
assertThat(sObj2Arr2.get(0).toString(), equalTo("arr_value1"));
assertThat(sObj2Arr2.get(1).toString(), equalTo("arr_value2"));
List sObj2Arr3 = response.getHits().getAt(0).field("s_arr3").values();
assertThat(((Map) sObj2Arr3.get(0)).get("arr3_field1").toString(), equalTo("arr3_value1"));
}
/*
* TODO Remove in 2.0
*/
public void testScriptFieldsOldScriptAPI() throws Exception {
assertAcked(prepareCreate("index").addMapping("type", "s", "type=string,index=not_analyzed", "l", "type=long", "d", "type=double",
"ms", "type=string,index=not_analyzed", "ml", "type=long", "md", "type=double").get());
final int numDocs = randomIntBetween(3, 8);
List<IndexRequestBuilder> reqs = new ArrayList<>();
for (int i = 0; i < numDocs; ++i) {
reqs.add(client().prepareIndex("index", "type", Integer.toString(i)).setSource("s", Integer.toString(i), "ms",
new String[] { Integer.toString(i), Integer.toString(i + 1) }, "l", i, "ml", new long[] { i, i + 1 }, "d", i, "md",
new double[] { i, i + 1 }));
}
indexRandom(true, reqs);
ensureSearchable();
SearchRequestBuilder req = client().prepareSearch("index");
for (String field : Arrays.asList("s", "ms", "l", "ml", "d", "md")) {
req.addScriptField(field, "doc['" + field + "'].values");
}
SearchResponse resp = req.get();
assertSearchResponse(resp);
for (SearchHit hit : resp.getHits().getHits()) {
final int id = Integer.parseInt(hit.getId());
Map<String, SearchHitField> fields = hit.getFields();
assertThat(fields.get("s").getValues(), equalTo(Collections.<Object> singletonList(Integer.toString(id))));
assertThat(fields.get("l").getValues(), equalTo(Collections.<Object> singletonList((long) id)));
assertThat(fields.get("d").getValues(), equalTo(Collections.<Object> singletonList((double) id)));
assertThat(fields.get("ms").getValues(), equalTo(Arrays.<Object> asList(Integer.toString(id), Integer.toString(id + 1))));
assertThat(fields.get("ml").getValues(), equalTo(Arrays.<Object> asList((long) id, id + 1L)));
assertThat(fields.get("md").getValues(), equalTo(Arrays.<Object> asList((double) id, id + 1d)));
}
}
}

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.Script;
import org.elasticsearch.test.ElasticsearchBackwardsCompatIntegrationTest;
import org.junit.Test;
@ -112,8 +113,8 @@ public class FunctionScoreBackwardCompatibilityTests extends ElasticsearchBackwa
searchSource().query(
functionScoreQuery(termQuery("text", "value"))
.add(gaussDecayFunction("loc", new GeoPoint(10, 20), "1000km"))
.add(scriptFunction("_index['text']['value'].tf()"))
.add(termQuery("text", "boosted"), factorFunction(5))
.add(scriptFunction(new Script("_index['text']['value'].tf()")))
.add(termQuery("text", "boosted"), weightFactorFunction(5))
))).actionGet();
assertSearchResponse(response);
assertOrderedSearchHits(response, ids);

View File

@ -180,7 +180,7 @@ public class FunctionScoreTests extends ElasticsearchIntegrationTest {
assertThat(
responseWithWeights.getHits().getAt(0).getExplanation().toString(),
equalTo("6.0 = function score, product of:\n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 6.0 = min of:\n 6.0 = function score, score mode [multiply]\n 1.0 = function score, product of:\n 1.0 = match filter: *:*\n 1.0 = Function for field geo_point_field:\n 1.0 = exp(-0.5*pow(MIN of: [Math.max(arcDistance([10.0, 20.0](=doc value),[10.0, 20.0](=origin)) - 0.0(=offset), 0)],2.0)/7.213475204444817E11)\n 2.0 = function score, product of:\n 1.0 = match filter: *:*\n 2.0 = product of:\n 1.0 = field value function: ln(doc['double_field'].value * factor=1.0)\n 2.0 = weight\n 3.0 = function score, product of:\n 1.0 = match filter: *:*\n 3.0 = product of:\n 1.0 = script score function, computed with script:\"[script: _index['text_field']['value'].tf(), type: inline, lang: null, params: {}]\" and parameters: \n{}\n 1.0 = _score: \n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 3.0 = weight\n 3.4028235E38 = maxBoost\n 1.0 = queryBoost\n"));
equalTo("6.0 = function score, product of:\n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 6.0 = min of:\n 6.0 = function score, score mode [multiply]\n 1.0 = function score, product of:\n 1.0 = match filter: *:*\n 1.0 = Function for field geo_point_field:\n 1.0 = exp(-0.5*pow(MIN of: [Math.max(arcDistance([10.0, 20.0](=doc value),[10.0, 20.0](=origin)) - 0.0(=offset), 0)],2.0)/7.213475204444817E11)\n 2.0 = function score, product of:\n 1.0 = match filter: *:*\n 2.0 = product of:\n 1.0 = field value function: ln(doc['double_field'].value * factor=1.0)\n 2.0 = weight\n 3.0 = function score, product of:\n 1.0 = match filter: *:*\n 3.0 = product of:\n 1.0 = script score function, computed with script:\"[script: _index['text_field']['value'].tf(), type: inline, lang: null, params: null]\n 1.0 = _score: \n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 3.0 = weight\n 3.4028235E38 = maxBoost\n 1.0 = queryBoost\n"));
responseWithWeights = client().search(
searchRequest().source(
searchSource().query(
@ -557,245 +557,5 @@ functionScoreQuery().add(scriptFunction(new Script("1")))).add(
assertSearchResponse(response);
assertThat(response.getHits().totalHits(), equalTo(0l));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testExplainOldScriptAPI() throws IOException, ExecutionException, InterruptedException {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, MAPPING_WITH_DOUBLE_AND_GEO_POINT_AND_TEXT_FIELD));
ensureYellow();
index(INDEX, TYPE, "1", SIMPLE_DOC);
refresh();
SearchResponse responseWithWeights = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery(constantScoreQuery(termQuery(TEXT_FIELD, "value")))
.add(gaussDecayFunction(GEO_POINT_FIELD, new GeoPoint(10, 20), "1000km"))
.add(fieldValueFactorFunction(DOUBLE_FIELD).modifier(FieldValueFactorFunction.Modifier.LN)
.setWeight(2))
.add(scriptFunction("_index['" + TEXT_FIELD + "']['value'].tf()").setWeight(3))).explain(true)))
.actionGet();
assertThat(
responseWithWeights.getHits().getAt(0).getExplanation().toString(),
equalTo("6.0 = function score, product of:\n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 6.0 = min of:\n 6.0 = function score, score mode [multiply]\n 1.0 = function score, product of:\n 1.0 = match filter: *:*\n 1.0 = Function for field geo_point_field:\n 1.0 = exp(-0.5*pow(MIN of: [Math.max(arcDistance([10.0, 20.0](=doc value),[10.0, 20.0](=origin)) - 0.0(=offset), 0)],2.0)/7.213475204444817E11)\n 2.0 = function score, product of:\n 1.0 = match filter: *:*\n 2.0 = product of:\n 1.0 = field value function: ln(doc['double_field'].value * factor=1.0)\n 2.0 = weight\n 3.0 = function score, product of:\n 1.0 = match filter: *:*\n 3.0 = product of:\n 1.0 = script score function, computed with script:\"[script: _index['text_field']['value'].tf(), type: inline, lang: null, params: {}]\" and parameters: \n{}\n 1.0 = _score: \n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 3.0 = weight\n 3.4028235E38 = maxBoost\n 1.0 = queryBoost\n"));
responseWithWeights = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery(constantScoreQuery(termQuery(TEXT_FIELD, "value"))).add(weightFactorFunction(4.0f)))
.explain(true))).actionGet();
assertThat(
responseWithWeights.getHits().getAt(0).getExplanation().toString(),
equalTo("4.0 = function score, product of:\n 1.0 = ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 4.0 = min of:\n 4.0 = product of:\n 1.0 = constant score 1.0 - no function provided\n 4.0 = weight\n 3.4028235E38 = maxBoost\n 1.0 = queryBoost\n"));
}
/*
* TODO Remove in 2.0
*/
@Test
public void simpleWeightedFunctionsTestOldScriptAPI() throws IOException, ExecutionException, InterruptedException {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, MAPPING_WITH_DOUBLE_AND_GEO_POINT_AND_TEXT_FIELD));
ensureYellow();
index(INDEX, TYPE, "1", SIMPLE_DOC);
refresh();
SearchResponse response = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery(constantScoreQuery(termQuery(TEXT_FIELD, "value")))
.add(gaussDecayFunction(GEO_POINT_FIELD, new GeoPoint(10, 20), "1000km"))
.add(fieldValueFactorFunction(DOUBLE_FIELD).modifier(FieldValueFactorFunction.Modifier.LN))
.add(scriptFunction("_index['" + TEXT_FIELD + "']['value'].tf()"))))).actionGet();
SearchResponse responseWithWeights = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery(constantScoreQuery(termQuery(TEXT_FIELD, "value")))
.add(gaussDecayFunction(GEO_POINT_FIELD, new GeoPoint(10, 20), "1000km").setWeight(2))
.add(fieldValueFactorFunction(DOUBLE_FIELD).modifier(FieldValueFactorFunction.Modifier.LN)
.setWeight(2))
.add(scriptFunction("_index['" + TEXT_FIELD + "']['value'].tf()").setWeight(2))))).actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getAt(0).getScore(), is(1.0f));
assertThat(responseWithWeights.getHits().getAt(0).getScore(), is(8.0f));
}
/*
* TODO Remove in 2.0
*/
@Test
public void simpleWeightedFunctionsTestWithRandomWeightsAndRandomCombineModeOldScriptAPI() throws IOException, ExecutionException,
InterruptedException {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, MAPPING_WITH_DOUBLE_AND_GEO_POINT_AND_TEXT_FIELD));
ensureYellow();
XContentBuilder doc = SIMPLE_DOC;
index(INDEX, TYPE, "1", doc);
refresh();
ScoreFunctionBuilder[] scoreFunctionBuilders = getScoreFunctionBuildersOldScriptAPI();
float[] weights = createRandomWeights(scoreFunctionBuilders.length);
float[] scores = getScores(scoreFunctionBuilders);
String scoreMode = getRandomScoreMode();
FunctionScoreQueryBuilder withWeights = functionScoreQuery(constantScoreQuery(termQuery(TEXT_FIELD, "value"))).scoreMode(scoreMode);
int weightscounter = 0;
for (ScoreFunctionBuilder builder : scoreFunctionBuilders) {
withWeights.add(builder.setWeight(weights[weightscounter]));
weightscounter++;
}
SearchResponse responseWithWeights = client().search(searchRequest().source(searchSource().query(withWeights))).actionGet();
double expectedScore = computeExpectedScore(weights, scores, scoreMode);
assertThat((float) expectedScore / responseWithWeights.getHits().getAt(0).getScore(), is(1.0f));
}
/*
* TODO Remove in 2.0
*/
@Test
public void simpleWeightedFunctionsTestSingleFunctionOldScriptAPI() throws IOException, ExecutionException, InterruptedException {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, MAPPING_WITH_DOUBLE_AND_GEO_POINT_AND_TEXT_FIELD));
ensureYellow();
XContentBuilder doc = jsonBuilder().startObject().field(TEXT_FIELD, "value").startObject(GEO_POINT_FIELD).field("lat", 12)
.field("lon", 21).endObject().field(DOUBLE_FIELD, 10).endObject();
index(INDEX, TYPE, "1", doc);
refresh();
ScoreFunctionBuilder[] scoreFunctionBuilders = getScoreFunctionBuildersOldScriptAPI();
ScoreFunctionBuilder scoreFunctionBuilder = scoreFunctionBuilders[randomInt(3)];
float[] weights = createRandomWeights(1);
float[] scores = getScores(scoreFunctionBuilder);
FunctionScoreQueryBuilder withWeights = functionScoreQuery(constantScoreQuery(termQuery(TEXT_FIELD, "value")));
withWeights.add(scoreFunctionBuilder.setWeight(weights[0]));
SearchResponse responseWithWeights = client().search(searchRequest().source(searchSource().query(withWeights))).actionGet();
assertThat((double) scores[0] * weights[0] / responseWithWeights.getHits().getAt(0).getScore(), closeTo(1.0, 1.e-6));
}
/*
* TODO Remove in 2.0
*/
public ScoreFunctionBuilder[] getScoreFunctionBuildersOldScriptAPI() {
ScoreFunctionBuilder[] builders = new ScoreFunctionBuilder[4];
builders[0] = gaussDecayFunction(GEO_POINT_FIELD, new GeoPoint(10, 20), "1000km");
builders[1] = randomFunction(10);
builders[2] = fieldValueFactorFunction(DOUBLE_FIELD).modifier(FieldValueFactorFunction.Modifier.LN);
builders[3] = scriptFunction("_index['" + TEXT_FIELD + "']['value'].tf()");
return builders;
}
/*
* TODO Remove in 2.0
*/
@Test
public void testScriptScoresNestedOldScriptAPI() throws IOException {
createIndex(INDEX);
ensureYellow();
index(INDEX, TYPE, "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
refresh();
SearchResponse response = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery(
functionScoreQuery(functionScoreQuery().add(scriptFunction("1"))).add(
scriptFunction("_score.doubleValue()"))).add(scriptFunction("_score.doubleValue()")))))
.actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getAt(0).score(), equalTo(1.0f));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testScriptScoresWithAggOldScriptAPI() throws IOException {
createIndex(INDEX);
ensureYellow();
index(INDEX, TYPE, "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
refresh();
SearchResponse response = client().search(
searchRequest().source(
searchSource().query(functionScoreQuery().add(scriptFunction("_score.doubleValue()"))).aggregation(
terms("score_agg").script("_score.doubleValue()")))).actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getAt(0).score(), equalTo(1.0f));
assertThat(((Terms) response.getAggregations().asMap().get("score_agg")).getBuckets().get(0).getKeyAsString(), equalTo("1.0"));
assertThat(((Terms) response.getAggregations().asMap().get("score_agg")).getBuckets().get(0).getDocCount(), is(1l));
}
/*
* TODO Remove in 2.0
*/
public void testMinScoreFunctionScoreBasicOldScriptAPI() throws IOException {
index(INDEX, TYPE, jsonBuilder().startObject().field("num", 2).endObject());
refresh();
ensureYellow();
float score = randomFloat();
float minScore = randomFloat();
SearchResponse searchResponse = client().search(
searchRequest().source(
searchSource().query(functionScoreQuery().add(scriptFunction(Float.toString(score))).setMinScore(minScore))))
.actionGet();
if (score < minScore) {
assertThat(searchResponse.getHits().getTotalHits(), is(0l));
} else {
assertThat(searchResponse.getHits().getTotalHits(), is(1l));
}
searchResponse = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery().add(scriptFunction(Float.toString(score))).add(scriptFunction(Float.toString(score)))
.scoreMode("avg").setMinScore(minScore)))).actionGet();
if (score < minScore) {
assertThat(searchResponse.getHits().getTotalHits(), is(0l));
} else {
assertThat(searchResponse.getHits().getTotalHits(), is(1l));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testMinScoreFunctionScoreManyDocsAndRandomMinScoreOldScriptAPI() throws IOException, ExecutionException,
InterruptedException {
List<IndexRequestBuilder> docs = new ArrayList<>();
int numDocs = randomIntBetween(1, 100);
int scoreOffset = randomIntBetween(-2 * numDocs, 2 * numDocs);
int minScore = randomIntBetween(-2 * numDocs, 2 * numDocs);
for (int i = 0; i < numDocs; i++) {
docs.add(client().prepareIndex(INDEX, TYPE, Integer.toString(i)).setSource("num", i + scoreOffset));
}
indexRandom(true, docs);
ensureYellow();
String script = "return (doc['num'].value)";
int numMatchingDocs = numDocs + scoreOffset - minScore;
if (numMatchingDocs < 0) {
numMatchingDocs = 0;
}
if (numMatchingDocs > numDocs) {
numMatchingDocs = numDocs;
}
SearchResponse searchResponse = client().search(
searchRequest().source(
searchSource().query(functionScoreQuery().add(scriptFunction(script)).setMinScore(minScore)).size(numDocs)))
.actionGet();
assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
searchResponse = client().search(
searchRequest().source(
searchSource().query(
functionScoreQuery().add(scriptFunction(script)).add(scriptFunction(script)).scoreMode("avg")
.setMinScore(minScore)).size(numDocs))).actionGet();
assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
}
}

View File

@ -28,10 +28,7 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.CoreMatchers;
import org.junit.Ignore;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import static com.google.common.collect.Lists.newArrayList;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -115,75 +112,6 @@ public class RandomScoreFunctionTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
public void testScoreAccessWithinScriptOldScriptAPI() throws Exception {
assertAcked(prepareCreate("test")
.addMapping("type", "body", "type=string", "index", "type=" + randomFrom(new String[]{"short", "float", "long", "integer", "double"})));
ensureYellow();
int docCount = randomIntBetween(100, 200);
for (int i = 0; i < docCount; i++) {
client().prepareIndex("test", "type", "" + i).setSource("body", randomFrom(newArrayList("foo", "bar", "baz")), "index", i).get();
}
refresh();
// Test for accessing _score
SearchResponse resp = client().prepareSearch("test")
.setQuery(functionScoreQuery(matchQuery("body", "foo"))
.add(fieldValueFactorFunction("index").factor(2))
.add(scriptFunction("log(doc['index'].value + (factor * _score))").param("factor", randomIntBetween(2, 4))))
.get();
assertNoFailures(resp);
SearchHit firstHit = resp.getHits().getAt(0);
assertThat(firstHit.getScore(), greaterThan(1f));
// Test for accessing _score.intValue()
resp = client().prepareSearch("test")
.setQuery(functionScoreQuery(matchQuery("body", "foo"))
.add(fieldValueFactorFunction("index").factor(2))
.add(scriptFunction("log(doc['index'].value + (factor * _score.intValue()))")
.param("factor", randomIntBetween(2, 4))))
.get();
assertNoFailures(resp);
firstHit = resp.getHits().getAt(0);
assertThat(firstHit.getScore(), greaterThan(1f));
// Test for accessing _score.longValue()
resp = client().prepareSearch("test")
.setQuery(functionScoreQuery(matchQuery("body", "foo"))
.add(fieldValueFactorFunction("index").factor(2))
.add(scriptFunction("log(doc['index'].value + (factor * _score.longValue()))")
.param("factor", randomIntBetween(2, 4))))
.get();
assertNoFailures(resp);
firstHit = resp.getHits().getAt(0);
assertThat(firstHit.getScore(), greaterThan(1f));
// Test for accessing _score.floatValue()
resp = client().prepareSearch("test")
.setQuery(functionScoreQuery(matchQuery("body", "foo"))
.add(fieldValueFactorFunction("index").factor(2))
.add(scriptFunction("log(doc['index'].value + (factor * _score.floatValue()))")
.param("factor", randomIntBetween(2, 4))))
.get();
assertNoFailures(resp);
firstHit = resp.getHits().getAt(0);
assertThat(firstHit.getScore(), greaterThan(1f));
// Test for accessing _score.doubleValue()
resp = client().prepareSearch("test")
.setQuery(functionScoreQuery(matchQuery("body", "foo"))
.add(fieldValueFactorFunction("index").factor(2))
.add(scriptFunction("log(doc['index'].value + (factor * _score.doubleValue()))")
.param("factor", randomIntBetween(2, 4))))
.get();
assertNoFailures(resp);
firstHit = resp.getHits().getAt(0);
assertThat(firstHit.getScore(), greaterThan(1f));
}
public void testScoreAccessWithinScript() throws Exception {
assertAcked(prepareCreate("test").addMapping("type", "body", "type=string", "index",
"type=" + randomFrom(new String[] { "short", "float", "long", "integer", "double" })));

View File

@ -470,81 +470,6 @@ public class GeoDistanceTests extends ElasticsearchIntegrationTest {
closeTo(GeoDistance.PLANE.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.MILES), 0.0001d));
}
/*
* Remove in 2.0
*/
@Test
public void distanceScriptTestsOldScriptAPI() throws Exception {
double source_lat = 32.798;
double source_long = -117.151;
double target_lat = 32.81;
double target_long = -117.21;
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("location").field("type", "geo_point").field("lat_lon", true).endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").addMapping("type1", xContentBuilder));
ensureGreen();
client().prepareIndex("test", "type1", "1")
.setSource(
jsonBuilder().startObject().field("name", "TestPosition").startObject("location").field("lat", source_lat)
.field("lon", source_long).endObject().endObject()).execute().actionGet();
refresh();
SearchResponse searchResponse1 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].arcDistance(" + target_lat + "," + target_long + ")").execute().actionGet();
Double resultDistance1 = searchResponse1.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultDistance1,
closeTo(GeoDistance.ARC.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.DEFAULT), 0.0001d));
SearchResponse searchResponse2 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].distance(" + target_lat + "," + target_long + ")").execute().actionGet();
Double resultDistance2 = searchResponse2.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultDistance2,
closeTo(GeoDistance.PLANE.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.DEFAULT), 0.0001d));
SearchResponse searchResponse3 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].arcDistanceInKm(" + target_lat + "," + target_long + ")").execute()
.actionGet();
Double resultArcDistance3 = searchResponse3.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultArcDistance3,
closeTo(GeoDistance.ARC.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.KILOMETERS), 0.0001d));
SearchResponse searchResponse4 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].distanceInKm(" + target_lat + "," + target_long + ")").execute().actionGet();
Double resultDistance4 = searchResponse4.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultDistance4,
closeTo(GeoDistance.PLANE.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.KILOMETERS), 0.0001d));
SearchResponse searchResponse5 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].arcDistanceInKm(" + (target_lat) + "," + (target_long + 360) + ")").execute()
.actionGet();
Double resultArcDistance5 = searchResponse5.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultArcDistance5,
closeTo(GeoDistance.ARC.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.KILOMETERS), 0.0001d));
SearchResponse searchResponse6 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].arcDistanceInKm(" + (target_lat + 360) + "," + (target_long) + ")").execute()
.actionGet();
Double resultArcDistance6 = searchResponse6.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultArcDistance6,
closeTo(GeoDistance.ARC.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.KILOMETERS), 0.0001d));
SearchResponse searchResponse7 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].arcDistanceInMiles(" + target_lat + "," + target_long + ")").execute()
.actionGet();
Double resultDistance7 = searchResponse7.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultDistance7,
closeTo(GeoDistance.ARC.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.MILES), 0.0001d));
SearchResponse searchResponse8 = client().prepareSearch().addField("_source")
.addScriptField("distance", "doc['location'].distanceInMiles(" + target_lat + "," + target_long + ")").execute()
.actionGet();
Double resultDistance8 = searchResponse8.getHits().getHits()[0].getFields().get("distance").getValue();
assertThat(resultDistance8,
closeTo(GeoDistance.PLANE.calculate(source_lat, source_long, target_lat, target_long, DistanceUnit.MILES), 0.0001d));
}
@Test
public void testDistanceSortingNestedFields() throws Exception {

View File

@ -931,216 +931,6 @@ public class InnerHitsTests extends ElasticsearchIntegrationTest {
assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getChild(), nullValue());
}
/*
* TODO Remove in 2.0
*/
@Test
public void testSimpleNestedOldScriptAPI() throws Exception {
assertAcked(prepareCreate("articles").addMapping(
"article",
jsonBuilder().startObject().startObject("article").startObject("properties").startObject("comments")
.field("type", "nested").startObject("properties").startObject("message").field("type", "string").endObject()
.endObject().endObject().startObject("title").field("type", "string").endObject().endObject().endObject()
.endObject()));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("articles", "article", "1").setSource(
jsonBuilder().startObject().field("title", "quick brown fox").startArray("comments").startObject()
.field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject()
.startObject().field("message", "rabbit got away").endObject().endArray().endObject()));
requests.add(client().prepareIndex("articles", "article", "2").setSource(
jsonBuilder().startObject().field("title", "big gray elephant").startArray("comments").startObject()
.field("message", "elephant captured").endObject().startObject().field("message", "mice squashed by elephant x")
.endObject().startObject().field("message", "elephant scared by mice x y").endObject().endArray().endObject()));
indexRandom(true, requests);
// Inner hits can be defined in two ways: 1) with the query 2) as seperate inner_hit definition
SearchRequest[] searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
.setQuery(
nestedQuery("comments", matchQuery("comments.message", "fox")).innerHit(
new QueryInnerHitBuilder().setName("comment"))).request(),
client().prepareSearch("articles")
.setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")))
.addInnerHit("comment",
new InnerHitsBuilder.InnerHit().setPath("comments").setQuery(matchQuery("comments.message", "fox")))
.request() };
for (SearchRequest searchRequest : searchRequests) {
SearchResponse response = client().search(searchRequest).actionGet();
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("1"));
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.totalHits(), equalTo(2l));
assertThat(innerHits.getHits().length, equalTo(2));
assertThat(innerHits.getAt(0).getId(), equalTo("1"));
assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(innerHits.getAt(1).getId(), equalTo("1"));
assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
}
searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
.setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant")))
.addInnerHit("comment",
new InnerHitsBuilder.InnerHit().setPath("comments").setQuery(matchQuery("comments.message", "elephant")))
.request(),
client().prepareSearch("articles")
.setQuery(
nestedQuery("comments", matchQuery("comments.message", "elephant")).innerHit(
new QueryInnerHitBuilder().setName("comment"))).request(),
client().prepareSearch("articles")
.setQuery(
nestedQuery("comments", matchQuery("comments.message", "elephant")).innerHit(
new QueryInnerHitBuilder().setName("comment").addSort("_doc", SortOrder.DESC))).request() };
for (SearchRequest searchRequest : searchRequests) {
SearchResponse response = client().search(searchRequest).actionGet();
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("2"));
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.totalHits(), equalTo(3l));
assertThat(innerHits.getHits().length, equalTo(3));
assertThat(innerHits.getAt(0).getId(), equalTo("2"));
assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(innerHits.getAt(1).getId(), equalTo("2"));
assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
assertThat(innerHits.getAt(2).getId(), equalTo("2"));
assertThat(innerHits.getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(2).getNestedIdentity().getOffset(), equalTo(2));
}
searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
.setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")))
.addInnerHit(
"comments",
new InnerHitsBuilder.InnerHit().setPath("comments").setQuery(matchQuery("comments.message", "fox"))
.addHighlightedField("comments.message").setExplain(true).addFieldDataField("comments.message")
.addScriptField("script", "doc['comments.message'].value").setSize(1)).request(),
client().prepareSearch("articles")
.setQuery(
nestedQuery("comments", matchQuery("comments.message", "fox")).innerHit(
new QueryInnerHitBuilder().addHighlightedField("comments.message").setExplain(true)
.addFieldDataField("comments.message")
.addScriptField("script", "doc['comments.message'].value").setSize(1))).request() };
for (SearchRequest searchRequest : searchRequests) {
SearchResponse response = client().search(searchRequest).actionGet();
assertNoFailures(response);
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
assertThat(innerHits.getTotalHits(), equalTo(2l));
assertThat(innerHits.getHits().length, equalTo(1));
assertThat(innerHits.getAt(0).getHighlightFields().get("comments.message").getFragments()[0].string(),
equalTo("<em>fox</em> eat quick"));
assertThat(innerHits.getAt(0).explanation().toString(), containsString("weight(comments.message:fox in"));
assertThat(innerHits.getAt(0).getFields().get("comments.message").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("eat"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testSimpleParentChildOldScriptAPI() throws Exception {
assertAcked(prepareCreate("articles").addMapping("article", "title", "type=string").addMapping("comment", "_parent",
"type=article", "message", "type=string"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("articles", "article", "1").setSource("title", "quick brown fox"));
requests.add(client().prepareIndex("articles", "comment", "1").setParent("1").setSource("message", "fox eat quick"));
requests.add(client().prepareIndex("articles", "comment", "2").setParent("1").setSource("message", "fox ate rabbit x y z"));
requests.add(client().prepareIndex("articles", "comment", "3").setParent("1").setSource("message", "rabbit got away"));
requests.add(client().prepareIndex("articles", "article", "2").setSource("title", "big gray elephant"));
requests.add(client().prepareIndex("articles", "comment", "4").setParent("2").setSource("message", "elephant captured"));
requests.add(client().prepareIndex("articles", "comment", "5").setParent("2").setSource("message", "mice squashed by elephant x"));
requests.add(client().prepareIndex("articles", "comment", "6").setParent("2").setSource("message", "elephant scared by mice x y"));
indexRandom(true, requests);
SearchRequest[] searchRequests = new SearchRequest[] {
client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "fox")))
.addInnerHit("comment", new InnerHitsBuilder.InnerHit().setType("comment").setQuery(matchQuery("message", "fox")))
.request(),
client().prepareSearch("articles")
.setQuery(
hasChildQuery("comment", matchQuery("message", "fox")).innerHit(
new QueryInnerHitBuilder().setName("comment"))).request() };
for (SearchRequest searchRequest : searchRequests) {
SearchResponse response = client().search(searchRequest).actionGet();
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("1"));
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.totalHits(), equalTo(2l));
assertThat(innerHits.getAt(0).getId(), equalTo("1"));
assertThat(innerHits.getAt(0).type(), equalTo("comment"));
assertThat(innerHits.getAt(1).getId(), equalTo("2"));
assertThat(innerHits.getAt(1).type(), equalTo("comment"));
}
searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
.setQuery(hasChildQuery("comment", matchQuery("message", "elephant")))
.addInnerHit("comment",
new InnerHitsBuilder.InnerHit().setType("comment").setQuery(matchQuery("message", "elephant"))).request(),
client().prepareSearch("articles")
.setQuery(hasChildQuery("comment", matchQuery("message", "elephant")).innerHit(new QueryInnerHitBuilder()))
.request() };
for (SearchRequest searchRequest : searchRequests) {
SearchResponse response = client().search(searchRequest).actionGet();
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("2"));
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.totalHits(), equalTo(3l));
assertThat(innerHits.getAt(0).getId(), equalTo("4"));
assertThat(innerHits.getAt(0).type(), equalTo("comment"));
assertThat(innerHits.getAt(1).getId(), equalTo("5"));
assertThat(innerHits.getAt(1).type(), equalTo("comment"));
assertThat(innerHits.getAt(2).getId(), equalTo("6"));
assertThat(innerHits.getAt(2).type(), equalTo("comment"));
}
searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
.setQuery(hasChildQuery("comment", matchQuery("message", "fox")))
.addInnerHit(
"comment",
new InnerHitsBuilder.InnerHit().setType("comment").setQuery(matchQuery("message", "fox"))
.addHighlightedField("message").setExplain(true).addFieldDataField("message")
.addScriptField("script", "doc['message'].value").setSize(1)).request(),
client().prepareSearch("articles")
.setQuery(
hasChildQuery("comment", matchQuery("message", "fox")).innerHit(
new QueryInnerHitBuilder().addHighlightedField("message").setExplain(true)
.addFieldDataField("message").addScriptField("script", "doc['message'].value").setSize(1)))
.request() };
for (SearchRequest searchRequest : searchRequests) {
SearchResponse response = client().search(searchRequest).actionGet();
assertNoFailures(response);
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.getHits().length, equalTo(1));
assertThat(innerHits.getAt(0).getHighlightFields().get("message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
assertThat(innerHits.getAt(0).explanation().toString(), containsString("weight(message:fox"));
assertThat(innerHits.getAt(0).getFields().get("message").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("eat"));
}
}
@Test
public void testRoyals() throws Exception {
assertAcked(

View File

@ -84,7 +84,7 @@ public class QueryRescorerTests extends ElasticsearchIntegrationTest {
.setQuery(QueryBuilders.matchAllQuery())
.setRescorer(RescoreBuilder.queryRescorer(
QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery())
.boostMode("replace").add(ScoreFunctionBuilders.factorFunction(100))).setQueryWeight(0.0f).setRescoreQueryWeight(1.0f))
.boostMode("replace").add(ScoreFunctionBuilders.weightFactorFunction(100))).setQueryWeight(0.0f).setRescoreQueryWeight(1.0f))
.setRescoreWindow(1).setSize(randomIntBetween(2,10)).execute().actionGet();
assertSearchResponse(searchResponse);
assertFirstHit(searchResponse, hasScore(100.f));

View File

@ -30,6 +30,7 @@ import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@ -88,9 +89,7 @@ public class ScriptQuerySearchTests extends ElasticsearchIntegrationTest {
logger.info("running doc['num1'].value > param1");
response = client()
.prepareSearch()
.setQuery(
filteredQuery(matchAllQuery(),
scriptQuery(new Script("doc['num1'].value > param1", ScriptType.INLINE, null, params))))
.setQuery(scriptQuery(new Script("doc['num1'].value > param1", ScriptType.INLINE, null, params)))
.addSort("num1", SortOrder.ASC).addScriptField("sNum1", new Script("doc['num1'].value")).execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(1l));
@ -116,55 +115,6 @@ public class ScriptQuerySearchTests extends ElasticsearchIntegrationTest {
assertThat((Double) response.getHits().getAt(2).fields().get("sNum1").values().get(0), equalTo(3.0));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testCustomScriptBoostOldScriptAPI() throws Exception {
createIndex("test");
client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).endObject()).execute().actionGet();
flush();
client().prepareIndex("test", "type1", "2")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 2.0f).endObject()).execute().actionGet();
flush();
client().prepareIndex("test", "type1", "3")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 3.0f).endObject()).execute().actionGet();
refresh();
logger.info("running doc['num1'].value > 1");
SearchResponse response = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > 1")))
.addSort("num1", SortOrder.ASC).addScriptField("sNum1", "doc['num1'].value").execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
assertThat((Double) response.getHits().getAt(0).fields().get("sNum1").values().get(0), equalTo(2.0));
assertThat(response.getHits().getAt(1).id(), equalTo("3"));
assertThat((Double) response.getHits().getAt(1).fields().get("sNum1").values().get(0), equalTo(3.0));
logger.info("running doc['num1'].value > param1");
response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > param1").addParam("param1", 2)))
.addSort("num1", SortOrder.ASC).addScriptField("sNum1", "doc['num1'].value").execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(1l));
assertThat(response.getHits().getAt(0).id(), equalTo("3"));
assertThat((Double) response.getHits().getAt(0).fields().get("sNum1").values().get(0), equalTo(3.0));
logger.info("running doc['num1'].value > param1");
response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > param1").addParam("param1", -1)))
.addSort("num1", SortOrder.ASC).addScriptField("sNum1", "doc['num1'].value").execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
assertThat(response.getHits().getAt(0).id(), equalTo("1"));
assertThat((Double) response.getHits().getAt(0).fields().get("sNum1").values().get(0), equalTo(1.0));
assertThat(response.getHits().getAt(1).id(), equalTo("2"));
assertThat((Double) response.getHits().getAt(1).fields().get("sNum1").values().get(0), equalTo(2.0));
assertThat(response.getHits().getAt(2).id(), equalTo("3"));
assertThat((Double) response.getHits().getAt(2).fields().get("sNum1").values().get(0), equalTo(3.0));
}
private static AtomicInteger scriptCounter = new AtomicInteger(0);
public static int incrementScriptCounter() {

View File

@ -767,315 +767,6 @@ public class SimpleSortTests extends ElasticsearchIntegrationTest {
assertNoFailures(searchResponse);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testSimpleSortsOldScriptAPI() throws Exception {
Random random = getRandom();
assertAcked(prepareCreate("test").addMapping(
"type1",
XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("str_value")
.field("type", "string").field("index", "not_analyzed").startObject("fielddata")
.field("format", random().nextBoolean() ? "doc_values" : null).endObject().endObject().startObject("boolean_value")
.field("type", "boolean").endObject().startObject("byte_value").field("type", "byte").startObject("fielddata")
.field("format", random().nextBoolean() ? "doc_values" : null).endObject().endObject().startObject("short_value")
.field("type", "short").startObject("fielddata").field("format", random().nextBoolean() ? "doc_values" : null)
.endObject().endObject().startObject("integer_value").field("type", "integer").startObject("fielddata")
.field("format", random().nextBoolean() ? "doc_values" : null).endObject().endObject().startObject("long_value")
.field("type", "long").startObject("fielddata").field("format", random().nextBoolean() ? "doc_values" : null)
.endObject().endObject().startObject("float_value").field("type", "float").startObject("fielddata")
.field("format", random().nextBoolean() ? "doc_values" : null).endObject().endObject().startObject("double_value")
.field("type", "double").startObject("fielddata").field("format", random().nextBoolean() ? "doc_values" : null)
.endObject().endObject().endObject().endObject().endObject()));
ensureGreen();
List<IndexRequestBuilder> builders = new ArrayList<>();
for (int i = 0; i < 10; i++) {
IndexRequestBuilder builder = client().prepareIndex("test", "type1", Integer.toString(i)).setSource(
jsonBuilder().startObject().field("str_value", new String(new char[] { (char) (97 + i), (char) (97 + i) }))
.field("boolean_value", true).field("byte_value", i).field("short_value", i).field("integer_value", i)
.field("long_value", i).field("float_value", 0.1 * i).field("double_value", 0.1 * i).endObject());
builders.add(builder);
}
Collections.shuffle(builders, random);
for (IndexRequestBuilder builder : builders) {
builder.execute().actionGet();
if (random.nextBoolean()) {
if (random.nextInt(5) != 0) {
refresh();
} else {
client().admin().indices().prepareFlush().execute().actionGet();
}
}
}
refresh();
// STRING
int size = 1 + random.nextInt(10);
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size)
.addSort("str_value", SortOrder.ASC).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(searchResponse.getHits().getAt(i).sortValues()[0].toString(), equalTo(new String(new char[] { (char) (97 + i),
(char) (97 + i) })));
}
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.DESC).execute()
.actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(searchResponse.getHits().getAt(i).sortValues()[0].toString(), equalTo(new String(new char[] { (char) (97 + (9 - i)),
(char) (97 + (9 - i)) })));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// STRING script
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size)
.addSort(new ScriptSortBuilder("doc['str_value'].value", "string"))
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(searchResponse.getHits().getAt(i).sortValues()[0].toString(), equalTo(new String(new char[]{(char) (97 + i), (char) (97 + i)})));
}
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("str_value", SortOrder.DESC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(searchResponse.getHits().getAt(i).sortValues()[0].toString(), equalTo(new String(new char[]{(char) (97 + (9 - i)), (char) (97 + (9 - i))})));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// BYTE
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("byte_value", SortOrder.ASC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).byteValue(), equalTo((byte) i));
}
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("byte_value", SortOrder.DESC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).byteValue(), equalTo((byte) (9 - i)));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// SHORT
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("short_value", SortOrder.ASC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).shortValue(), equalTo((short) i));
}
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("short_value", SortOrder.DESC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).shortValue(), equalTo((short) (9 - i)));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// INTEGER
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("integer_value", SortOrder.ASC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).intValue(), equalTo(i));
}
assertThat(searchResponse.toString(), not(containsString("error")));
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("integer_value", SortOrder.DESC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).intValue(), equalTo((9 - i)));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// LONG
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("long_value", SortOrder.ASC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).longValue(), equalTo((long) i));
}
assertThat(searchResponse.toString(), not(containsString("error")));
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("long_value", SortOrder.DESC)
.execute().actionGet();
assertHitCount(searchResponse, 10l);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).longValue(), equalTo((long) (9 - i)));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// FLOAT
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("float_value", SortOrder.ASC)
.execute().actionGet();
assertHitCount(searchResponse, 10l);
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d));
}
assertThat(searchResponse.toString(), not(containsString("error")));
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("float_value", SortOrder.DESC)
.execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(10l));
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d));
}
assertThat(searchResponse.toString(), not(containsString("error")));
// DOUBLE
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("double_value", SortOrder.ASC)
.execute().actionGet();
assertHitCount(searchResponse, 10l);
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d));
}
assertThat(searchResponse.toString(), not(containsString("error")));
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.setSize(size)
.addSort("double_value", SortOrder.DESC)
.execute().actionGet();
assertHitCount(searchResponse, 10l);
assertThat(searchResponse.getHits().hits().length, equalTo(size));
for (int i = 0; i < size; i++) {
assertThat(searchResponse.getHits().getAt(i).id(), equalTo(Integer.toString(9 - i)));
assertThat(((Number) searchResponse.getHits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d));
}
assertNoFailures(searchResponse);
}
/*
* TODO Remove in 2.0
*/
@Test
public void test2920OldScriptAPI() throws IOException {
assertAcked(prepareCreate("test").addMapping("test",
jsonBuilder().startObject().startObject("test").startObject("properties")
.startObject("value").field("type", "string").endObject()
.endObject().endObject().endObject()));
ensureGreen();
for (int i = 0; i < 10; i++) {
client().prepareIndex("test", "test", Integer.toString(i)).setSource(jsonBuilder().startObject()
.field("value", "" + i).endObject()).execute().actionGet();
}
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.addSort(SortBuilders.scriptSort("\u0027\u0027", "string")).setSize(10)
.execute().actionGet();
assertNoFailures(searchResponse);
}
@Test
public void testSortMinValueScript() throws IOException {
String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties")
@ -1258,184 +949,6 @@ public class SimpleSortTests extends ElasticsearchIntegrationTest {
assertThat((String) searchResponse.getHits().getAt(0).field("id").value(), equalTo("2"));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testSortMinValueScriptOldScriptAPI() throws IOException {
String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("lvalue")
.field("type", "long").endObject().startObject("dvalue").field("type", "double").endObject().startObject("svalue")
.field("type", "string").endObject().startObject("gvalue").field("type", "geo_point").endObject().endObject().endObject()
.endObject().string();
assertAcked(prepareCreate("test").addMapping("type1", mapping));
ensureGreen();
for (int i = 0; i < 10; i++) {
IndexRequestBuilder req = client().prepareIndex("test", "type1", "" + i).setSource(
jsonBuilder().startObject().field("ord", i).field("svalue", new String[] { "" + i, "" + (i + 1), "" + (i + 2) })
.field("lvalue", new long[] { i, i + 1, i + 2 }).field("dvalue", new double[] { i, i + 1, i + 2 })
.startObject("gvalue").field("lat", (double) i + 1).field("lon", (double) i).endObject().endObject());
req.execute().actionGet();
}
for (int i = 10; i < 20; i++) { // add some docs that don't have values in those fields
client().prepareIndex("test", "type1", "" + i).setSource(jsonBuilder().startObject().field("ord", i).endObject()).execute()
.actionGet();
}
client().admin().indices().prepareRefresh("test").execute().actionGet();
// test the long values
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery())
.addScriptField("min", "retval = Long.MAX_VALUE; for (v in doc['lvalue'].values){ retval = min(v, retval) }; retval")
.addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(20l));
for (int i = 0; i < 10; i++) {
assertThat("res: " + i + " id: " + searchResponse.getHits().getAt(i).getId(),
(Long) searchResponse.getHits().getAt(i).field("min").value(), equalTo((long) i));
}
// test the double values
searchResponse = client().prepareSearch().setQuery(matchAllQuery())
.addScriptField("min", "retval = Double.MAX_VALUE; for (v in doc['dvalue'].values){ retval = min(v, retval) }; retval")
.addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(20l));
for (int i = 0; i < 10; i++) {
assertThat("res: " + i + " id: " + searchResponse.getHits().getAt(i).getId(),
(Double) searchResponse.getHits().getAt(i).field("min").value(), equalTo((double) i));
}
// test the string values
searchResponse = client()
.prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("min",
"retval = Integer.MAX_VALUE; for (v in doc['svalue'].values){ retval = min(Integer.parseInt(v), retval) }; retval")
.addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(20l));
for (int i = 0; i < 10; i++) {
assertThat("res: " + i + " id: " + searchResponse.getHits().getAt(i).getId(), (Integer) searchResponse.getHits().getAt(i)
.field("min").value(), equalTo(i));
}
// test the geopoint values
searchResponse = client().prepareSearch().setQuery(matchAllQuery())
.addScriptField("min", "retval = Double.MAX_VALUE; for (v in doc['gvalue'].values){ retval = min(v.lon, retval) }; retval")
.addSort(SortBuilders.fieldSort("ord").order(SortOrder.ASC).unmappedType("long")).setSize(10).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(20l));
for (int i = 0; i < 10; i++) {
assertThat("res: " + i + " id: " + searchResponse.getHits().getAt(i).getId(),
(Double) searchResponse.getHits().getAt(i).field("min").value(), equalTo((double) i));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testDocumentsWithNullValueOldScriptAPI() throws Exception {
// TODO: sort shouldn't fail when sort field is mapped dynamically
// We have to specify mapping explicitly because by the time search is performed dynamic mapping might not
// be propagated to all nodes yet and sort operation fail when the sort field is not defined
String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("svalue").field("type", "string").field("index", "not_analyzed").startObject("fielddata").field("format", random().nextBoolean() ? "doc_values" : null).endObject().endObject()
.endObject().endObject().endObject().string();
assertAcked(prepareCreate("test").addMapping("type1", mapping));
ensureGreen();
client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("id", "1")
.field("svalue", "aaa")
.endObject()).execute().actionGet();
client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("id", "2")
.nullField("svalue")
.endObject()).execute().actionGet();
client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("id", "3")
.field("svalue", "bbb")
.endObject()).execute().actionGet();
flush();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("id", "doc['id'].value")
.addSort("svalue", SortOrder.ASC)
.execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(3l));
assertThat((String) searchResponse.getHits().getAt(0).field("id").value(), equalTo("1"));
assertThat((String) searchResponse.getHits().getAt(1).field("id").value(), equalTo("3"));
assertThat((String) searchResponse.getHits().getAt(2).field("id").value(), equalTo("2"));
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("id", "doc['id'].values[0]")
.addSort("svalue", SortOrder.ASC)
.execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(3l));
assertThat((String) searchResponse.getHits().getAt(0).field("id").value(), equalTo("1"));
assertThat((String) searchResponse.getHits().getAt(1).field("id").value(), equalTo("3"));
assertThat((String) searchResponse.getHits().getAt(2).field("id").value(), equalTo("2"));
searchResponse = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("id", "doc['id'].value")
.addSort("svalue", SortOrder.DESC)
.execute().actionGet();
if (searchResponse.getFailedShards() > 0) {
logger.warn("Failed shards:");
for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
logger.warn("-> {}", shardSearchFailure);
}
}
assertThat(searchResponse.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().getTotalHits(), equalTo(3l));
assertThat((String) searchResponse.getHits().getAt(0).field("id").value(), equalTo("3"));
assertThat((String) searchResponse.getHits().getAt(1).field("id").value(), equalTo("1"));
assertThat((String) searchResponse.getHits().getAt(2).field("id").value(), equalTo("2"));
// a query with docs just with null values
searchResponse = client().prepareSearch()
.setQuery(termQuery("id", "2"))
.addScriptField("id", "doc['id'].value")
.addSort("svalue", SortOrder.DESC)
.execute().actionGet();
if (searchResponse.getFailedShards() > 0) {
logger.warn("Failed shards:");
for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
logger.warn("-> {}", shardSearchFailure);
}
}
assertThat(searchResponse.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l));
assertThat((String) searchResponse.getHits().getAt(0).field("id").value(), equalTo("2"));
}
@Test
public void testSortMissingNumbers() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1",

View File

@ -69,27 +69,6 @@ public class UpdateByNativeScriptTests extends ElasticsearchIntegrationTest {
assertThat(data.get("foo").toString(), is("SETVALUE"));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testThatUpdateUsingNativeScriptWorksOldScriptAPI() throws Exception {
createIndex("test");
ensureYellow();
index("test", "type", "1", "text", "value");
Map<String, Object> params = Maps.newHashMap();
params.put("foo", "SETVALUE");
client().prepareUpdate("test", "type", "1")
.setScript("custom", ScriptService.ScriptType.INLINE)
.setScriptLang(NativeScriptEngineService.NAME).setScriptParams(params).get();
Map<String, Object> data = client().prepareGet("test", "type", "1").get().getSource();
assertThat(data, hasKey("foo"));
assertThat(data.get("foo").toString(), is("SETVALUE"));
}
static class CustomNativeScriptFactory implements NativeScriptFactory {
@Override
public ExecutableScript newScript(@Nullable Map<String, Object> params) {

View File

@ -846,738 +846,6 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUpsertOldScriptAPI() throws Exception {
createTestIndex();
ensureGreen();
UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet();
assertTrue(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
}
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet();
assertFalse(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testScriptedUpsertOldScriptAPI() throws Exception {
createTestIndex();
ensureGreen();
// Script logic is
// 1) New accounts take balance from "balance" in upsert doc and first payment is charged at 50%
// 2) Existing accounts subtract full payment from balance stored in elasticsearch
String script = "int oldBalance=ctx._source.balance;" + "int deduction=ctx.op == \"create\" ? (payment/2) : payment;"
+ "ctx._source.balance=oldBalance-deduction;";
int openingBalance = 10;
// Pay money from what will be a new account and opening balance comes from upsert doc
// provided by client
UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("balance", openingBalance).endObject())
.setScriptedUpsert(true).addScriptParam("payment", 2).setScript(script, ScriptService.ScriptType.INLINE).execute()
.actionGet();
assertTrue(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("9"));
}
// Now pay money for an existing account where balance is stored in es
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("balance", openingBalance).endObject())
.setScriptedUpsert(true).addScriptParam("payment", 2).setScript(script, ScriptService.ScriptType.INLINE).execute()
.actionGet();
assertFalse(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("7"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUpsertFieldsOldScriptAPI() throws Exception {
createTestIndex();
ensureGreen();
UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
.setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE).setFields("_source").execute().actionGet();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue());
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
.setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE).setFields("_source").execute().actionGet();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("extra").toString(), equalTo("foo"));
}
/*
* TODO Remove in 2.0
*/
@Test
public void testVersionedUpdateOldScriptAPI() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
ensureGreen();
index("test", "type", "1", "text", "value"); // version is now 1
assertThrows(
client().prepareUpdate(indexOrAlias(), "type", "1").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
.setVersion(2).execute(), VersionConflictEngineException.class);
client().prepareUpdate(indexOrAlias(), "type", "1").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
.setVersion(1).get();
assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(2l));
// and again with a higher version..
client().prepareUpdate(indexOrAlias(), "type", "1").setScript("ctx._source.text = 'v3'", ScriptService.ScriptType.INLINE)
.setVersion(2).get();
assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(3l));
// after delete
client().prepareDelete("test", "type", "1").get();
assertThrows(client().prepareUpdate("test", "type", "1").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
.setVersion(3).execute(), DocumentMissingException.class);
// external versioning
client().prepareIndex("test", "type", "2").setSource("text", "value").setVersion(10).setVersionType(VersionType.EXTERNAL).get();
assertThrows(
client().prepareUpdate(indexOrAlias(), "type", "2").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
.setVersion(2).setVersionType(VersionType.EXTERNAL).execute(), ActionRequestValidationException.class);
// upserts - the combination with versions is a bit weird. Test are here to ensure we do not change our behavior unintentionally
// With internal versions, tt means "if object is there with version X, update it or explode. If it is not there, index.
client().prepareUpdate(indexOrAlias(), "type", "3").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
.setVersion(10).setUpsert("{ \"text\": \"v0\" }").get();
GetResponse get = get("test", "type", "3");
assertThat(get.getVersion(), equalTo(1l));
assertThat((String) get.getSource().get("text"), equalTo("v0"));
// With force version
client().prepareUpdate(indexOrAlias(), "type", "4").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
.setVersion(10).setVersionType(VersionType.FORCE).setUpsert("{ \"text\": \"v0\" }").get();
get = get("test", "type", "4");
assertThat(get.getVersion(), equalTo(10l));
assertThat((String) get.getSource().get("text"), equalTo("v0"));
// retry on conflict is rejected:
assertThrows(client().prepareUpdate(indexOrAlias(), "type", "1").setVersion(10).setRetryOnConflict(5),
ActionRequestValidationException.class);
}
/*
* TODO Remove in 2.0
*/
@Test
public void testIndexAutoCreationOldScriptAPI() throws Exception {
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
.setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE).setFields("_source").execute().actionGet();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue());
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUpdateOldScriptAPI() throws Exception {
createTestIndex();
ensureGreen();
try {
client().prepareUpdate(indexOrAlias(), "type1", "1").setScript("ctx._source.field++", ScriptService.ScriptType.INLINE)
.execute().actionGet();
fail();
} catch (DocumentMissingException e) {
// all is well
}
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet();
assertThat(updateResponse.getVersion(), equalTo(2L));
assertFalse(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
}
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript("ctx._source.field += count", ScriptService.ScriptType.INLINE).addScriptParam("count", 3).execute().actionGet();
assertThat(updateResponse.getVersion(), equalTo(3L));
assertFalse(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
}
// check noop
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript("ctx.op = 'none'", ScriptService.ScriptType.INLINE)
.execute().actionGet();
assertThat(updateResponse.getVersion(), equalTo(3L));
assertFalse(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
}
// check delete
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript("ctx.op = 'delete'", ScriptService.ScriptType.INLINE).execute().actionGet();
assertThat(updateResponse.getVersion(), equalTo(4L));
assertFalse(updateResponse.isCreated());
assertThat(updateResponse.getIndex(), equalTo("test"));
for (int i = 0; i < 5; i++) {
GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(false));
}
// check TTL is kept after an update without TTL
client().prepareIndex("test", "type1", "2").setSource("field", 1).setTTL(86400000L).setRefresh(true).execute().actionGet();
GetResponse getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
long ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
assertThat(ttl, greaterThan(0L));
client().prepareUpdate(indexOrAlias(), "type1", "2").setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute()
.actionGet();
getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
assertThat(ttl, greaterThan(0L));
// check TTL update
client().prepareUpdate(indexOrAlias(), "type1", "2").setScript("ctx._ttl = 3600000", ScriptService.ScriptType.INLINE).execute()
.actionGet();
getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
assertThat(ttl, greaterThan(0L));
assertThat(ttl, lessThanOrEqualTo(3600000L));
// check timestamp update
client().prepareIndex("test", "type1", "3").setSource("field", 1).setRefresh(true).execute().actionGet();
client().prepareUpdate(indexOrAlias(), "type1", "3")
.setScript("ctx._timestamp = \"2009-11-15T14:12:12\"", ScriptService.ScriptType.INLINE).execute().actionGet();
getResponse = client().prepareGet("test", "type1", "3").setFields("_timestamp").execute().actionGet();
long timestamp = ((Number) getResponse.getField("_timestamp").getValue()).longValue();
assertThat(timestamp, equalTo(1258294332000L));
// check fields parameter
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setFields("_source", "field").execute().actionGet();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());
// check updates without script
// add new field
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject()).execute().actionGet();
for (int i = 0; i < 5; i++) {
getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
}
// change existing field
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject()).execute().actionGet();
for (int i = 0; i < 5; i++) {
getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("3"));
assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
}
// recursive map
Map<String, Object> testMap = new HashMap<>();
Map<String, Object> testMap2 = new HashMap<>();
Map<String, Object> testMap3 = new HashMap<>();
testMap3.put("commonkey", testMap);
testMap3.put("map3", 5);
testMap2.put("map2", 6);
testMap.put("commonkey", testMap2);
testMap.put("map1", 8);
client().prepareIndex("test", "type1", "1").setSource("map", testMap).execute().actionGet();
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject()).execute().actionGet();
for (int i = 0; i < 5; i++) {
getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
Map map1 = (Map) getResponse.getSourceAsMap().get("map");
assertThat(map1.size(), equalTo(3));
assertThat(map1.containsKey("map1"), equalTo(true));
assertThat(map1.containsKey("map3"), equalTo(true));
assertThat(map1.containsKey("commonkey"), equalTo(true));
Map map2 = (Map) map1.get("commonkey");
assertThat(map2.size(), equalTo(3));
assertThat(map2.containsKey("map1"), equalTo(true));
assertThat(map2.containsKey("map2"), equalTo(true));
assertThat(map2.containsKey("commonkey"), equalTo(true));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUpdateRequestWithBothScriptAndDocOldScriptAPI() throws Exception {
createTestIndex();
ensureGreen();
try {
client().prepareUpdate(indexOrAlias(), "type1", "1")
.setDoc(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet();
fail("Should have thrown ActionRequestValidationException");
} catch (ActionRequestValidationException e) {
assertThat(e.validationErrors().size(), equalTo(1));
assertThat(e.validationErrors().get(0), containsString("can't provide both script and doc"));
assertThat(e.getMessage(), containsString("can't provide both script and doc"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testUpdateRequestWithScriptAndShouldUpsertDocOldScriptAPI() throws Exception {
createTestIndex();
ensureGreen();
try {
client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null)).setDocAsUpsert(true)
.execute().actionGet();
fail("Should have thrown ActionRequestValidationException");
} catch (ActionRequestValidationException e) {
assertThat(e.validationErrors().size(), equalTo(1));
assertThat(e.validationErrors().get(0), containsString("doc must be specified if doc_as_upsert is enabled"));
assertThat(e.getMessage(), containsString("doc must be specified if doc_as_upsert is enabled"));
}
}
/*
* TODO Remove in 2.0
*/
@Test
public void testContextVariablesOldScriptAPI() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias"))
.addMapping("type1", XContentFactory.jsonBuilder()
.startObject()
.startObject("type1")
.startObject("_timestamp").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).endObject()
.endObject()
.endObject())
.addMapping("subtype1", XContentFactory.jsonBuilder()
.startObject()
.startObject("subtype1")
.startObject("_parent").field("type", "type1").endObject()
.startObject("_timestamp").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).endObject()
.endObject()
.endObject())
);
ensureGreen();
// Index some documents
long timestamp = System.currentTimeMillis();
client().prepareIndex().setIndex("test").setType("type1").setId("parentId1").setTimestamp(String.valueOf(timestamp - 1))
.setSource("field1", 0, "content", "bar").execute().actionGet();
long ttl = 10000;
client().prepareIndex().setIndex("test").setType("subtype1").setId("id1").setParent("parentId1").setRouting("routing1")
.setTimestamp(String.valueOf(timestamp)).setTTL(ttl).setSource("field1", 1, "content", "foo").execute().actionGet();
// Update the first object and note context variables values
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("delim", "_");
UpdateResponse updateResponse = client()
.prepareUpdate("test", "subtype1", "id1")
.setRouting("routing1")
.setScript(
"assert ctx._index == \"test\" : \"index should be \\\"test\\\"\"\n"
+ "assert ctx._type == \"subtype1\" : \"type should be \\\"subtype1\\\"\"\n"
+ "assert ctx._id == \"id1\" : \"id should be \\\"id1\\\"\"\n"
+ "assert ctx._version == 1 : \"version should be 1\"\n"
+ "assert ctx._parent == \"parentId1\" : \"parent should be \\\"parentId1\\\"\"\n"
+ "assert ctx._routing == \"routing1\" : \"routing should be \\\"routing1\\\"\"\n"
+ "assert ctx._timestamp == " + timestamp + " : \"timestamp should be "
+ timestamp
+ "\"\n"
+
// ttl has a 3-second leeway, because it's always counting down
"assert ctx._ttl <= " + ttl + " : \"ttl should be <= " + ttl + " but was \" + ctx._ttl\n"
+ "assert ctx._ttl >= " + (ttl - 3000) + " : \"ttl should be <= " + (ttl - 3000)
+ " but was \" + ctx._ttl\n" + "ctx._source.content = ctx._source.content + delim + ctx._source.content;\n"
+ "ctx._source.field1 += 1;\n", ScriptService.ScriptType.INLINE).setScriptParams(scriptParams).execute()
.actionGet();
assertEquals(2, updateResponse.getVersion());
GetResponse getResponse = client().prepareGet("test", "subtype1", "id1").setRouting("routing1").execute().actionGet();
assertEquals(2, getResponse.getSourceAsMap().get("field1"));
assertEquals("foo_foo", getResponse.getSourceAsMap().get("content"));
// Idem with the second object
scriptParams = new HashMap<>();
scriptParams.put("delim", "_");
updateResponse = client()
.prepareUpdate("test", "type1", "parentId1")
.setScript(
"assert ctx._index == \"test\" : \"index should be \\\"test\\\"\"\n"
+ "assert ctx._type == \"type1\" : \"type should be \\\"type1\\\"\"\n"
+ "assert ctx._id == \"parentId1\" : \"id should be \\\"parentId1\\\"\"\n"
+ "assert ctx._version == 1 : \"version should be 1\"\n"
+ "assert ctx._parent == null : \"parent should be null\"\n"
+ "assert ctx._routing == null : \"routing should be null\"\n" + "assert ctx._timestamp == "
+ (timestamp - 1) + " : \"timestamp should be " + (timestamp - 1) + "\"\n"
+ "assert ctx._ttl == null : \"ttl should be null\"\n"
+ "ctx._source.content = ctx._source.content + delim + ctx._source.content;\n"
+ "ctx._source.field1 += 1;\n", ScriptService.ScriptType.INLINE).setScriptParams(scriptParams).execute()
.actionGet();
assertEquals(2, updateResponse.getVersion());
getResponse = client().prepareGet("test", "type1", "parentId1").execute().actionGet();
assertEquals(1, getResponse.getSourceAsMap().get("field1"));
assertEquals("bar_bar", getResponse.getSourceAsMap().get("content"));
}
/*
* TODO Remove in 2.0
*/
@Test
@Slow
public void testConcurrentUpdateWithRetryOnConflictOldScriptAPI() throws Exception {
final boolean useBulkApi = randomBoolean();
createTestIndex();
ensureGreen();
int numberOfThreads = scaledRandomIntBetween(2, 5);
final CountDownLatch latch = new CountDownLatch(numberOfThreads);
final CountDownLatch startLatch = new CountDownLatch(1);
final int numberOfUpdatesPerThread = scaledRandomIntBetween(100, 10000);
final List<Throwable> failures = new CopyOnWriteArrayList<>();
for (int i = 0; i < numberOfThreads; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
startLatch.await();
for (int i = 0; i < numberOfUpdatesPerThread; i++) {
if (useBulkApi) {
UpdateRequestBuilder updateRequestBuilder = client()
.prepareUpdate(indexOrAlias(), "type1", Integer.toString(i))
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
.setRetryOnConflict(Integer.MAX_VALUE)
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject());
client().prepareBulk().add(updateRequestBuilder).execute().actionGet();
} else {
client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i))
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
.setRetryOnConflict(Integer.MAX_VALUE)
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject()).execute().actionGet();
}
}
} catch (Throwable e) {
failures.add(e);
} finally {
latch.countDown();
}
}
};
new Thread(r).start();
}
startLatch.countDown();
latch.await();
for (Throwable throwable : failures) {
logger.info("Captured failure on concurrent update:", throwable);
}
assertThat(failures.size(), equalTo(0));
for (int i = 0; i < numberOfUpdatesPerThread; i++) {
GetResponse response = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
assertThat(response.getId(), equalTo(Integer.toString(i)));
assertThat(response.isExists(), equalTo(true));
assertThat(response.getVersion(), equalTo((long) numberOfThreads));
assertThat((Integer) response.getSource().get("field"), equalTo(numberOfThreads));
}
}
/*
* TODO Remove in 2.0
*/
@Test
@Slow
public void stressUpdateDeleteConcurrencyOldScriptAPI() throws Exception {
//We create an index with merging disabled so that deletes don't get merged away
assertAcked(prepareCreate("test").addMapping(
"type1",
XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_timestamp").field("enabled", true)
.field("store", "yes").endObject().startObject("_ttl").field("enabled", true).endObject().endObject().endObject())
.setSettings(Settings.builder().put(MergePolicyConfig.INDEX_MERGE_ENABLED, false)));
ensureGreen();
final int numberOfThreads = scaledRandomIntBetween(3, 5);
final int numberOfIdsPerThread = scaledRandomIntBetween(3, 10);
final int numberOfUpdatesPerId = scaledRandomIntBetween(10, 100);
final int retryOnConflict = randomIntBetween(0, 1);
final CountDownLatch latch = new CountDownLatch(numberOfThreads);
final CountDownLatch startLatch = new CountDownLatch(1);
final List<Throwable> failures = new CopyOnWriteArrayList<>();
final class UpdateThread extends Thread {
final Map<Integer, Integer> failedMap = new HashMap<>();
final int numberOfIds;
final int updatesPerId;
final int maxUpdateRequests = numberOfIdsPerThread * numberOfUpdatesPerId;
final int maxDeleteRequests = numberOfIdsPerThread * numberOfUpdatesPerId;
private final Semaphore updateRequestsOutstanding = new Semaphore(maxUpdateRequests);
private final Semaphore deleteRequestsOutstanding = new Semaphore(maxDeleteRequests);
public UpdateThread(int numberOfIds, int updatesPerId) {
this.numberOfIds = numberOfIds;
this.updatesPerId = updatesPerId;
}
final class UpdateListener implements ActionListener<UpdateResponse> {
int id;
public UpdateListener(int id) {
this.id = id;
}
@Override
public void onResponse(UpdateResponse updateResponse) {
updateRequestsOutstanding.release(1);
}
@Override
public void onFailure(Throwable e) {
synchronized (failedMap) {
incrementMapValue(id, failedMap);
}
updateRequestsOutstanding.release(1);
}
}
final class DeleteListener implements ActionListener<DeleteResponse> {
int id;
public DeleteListener(int id) {
this.id = id;
}
@Override
public void onResponse(DeleteResponse deleteResponse) {
deleteRequestsOutstanding.release(1);
}
@Override
public void onFailure(Throwable e) {
synchronized (failedMap) {
incrementMapValue(id, failedMap);
}
deleteRequestsOutstanding.release(1);
}
}
@Override
public void run() {
try {
startLatch.await();
boolean hasWaitedForNoNode = false;
for (int j = 0; j < numberOfIds; j++) {
for (int k = 0; k < numberOfUpdatesPerId; ++k) {
updateRequestsOutstanding.acquire();
try {
UpdateRequest ur = client().prepareUpdate("test", "type1", Integer.toString(j))
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
.setRetryOnConflict(retryOnConflict)
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject()).request();
client().update(ur, new UpdateListener(j));
} catch (NoNodeAvailableException nne) {
updateRequestsOutstanding.release();
synchronized (failedMap) {
incrementMapValue(j, failedMap);
}
if (hasWaitedForNoNode) {
throw nne;
}
logger.warn("Got NoNodeException waiting for 1 second for things to recover.");
hasWaitedForNoNode = true;
Thread.sleep(1000);
}
try {
deleteRequestsOutstanding.acquire();
DeleteRequest dr = client().prepareDelete("test", "type1", Integer.toString(j)).request();
client().delete(dr, new DeleteListener(j));
} catch (NoNodeAvailableException nne) {
deleteRequestsOutstanding.release();
synchronized (failedMap) {
incrementMapValue(j, failedMap);
}
if (hasWaitedForNoNode) {
throw nne;
}
logger.warn("Got NoNodeException waiting for 1 second for things to recover.");
hasWaitedForNoNode = true;
Thread.sleep(1000); //Wait for no-node to clear
}
}
}
} catch (Throwable e) {
logger.error("Something went wrong", e);
failures.add(e);
} finally {
try {
waitForOutstandingRequests(TimeValue.timeValueSeconds(60), updateRequestsOutstanding, maxUpdateRequests, "Update");
waitForOutstandingRequests(TimeValue.timeValueSeconds(60), deleteRequestsOutstanding, maxDeleteRequests, "Delete");
} catch (ElasticsearchTimeoutException ete) {
failures.add(ete);
}
latch.countDown();
}
}
private void incrementMapValue(int j, Map<Integer, Integer> map) {
if (!map.containsKey(j)) {
map.put(j, 0);
}
map.put(j, map.get(j) + 1);
}
private void waitForOutstandingRequests(TimeValue timeOut, Semaphore requestsOutstanding, int maxRequests, String name) {
long start = System.currentTimeMillis();
do {
long msRemaining = timeOut.getMillis() - (System.currentTimeMillis() - start);
logger.info("[{}] going to try and acquire [{}] in [{}]ms [{}] available to acquire right now", name, maxRequests,
msRemaining, requestsOutstanding.availablePermits());
try {
requestsOutstanding.tryAcquire(maxRequests, msRemaining, TimeUnit.MILLISECONDS);
return;
} catch (InterruptedException ie) {
//Just keep swimming
}
} while ((System.currentTimeMillis() - start) < timeOut.getMillis());
throw new ElasticsearchTimeoutException("Requests were still outstanding after the timeout [" + timeOut + "] for type ["
+ name + "]");
}
}
final List<UpdateThread> threads = new ArrayList<>();
for (int i = 0; i < numberOfThreads; i++) {
UpdateThread ut = new UpdateThread(numberOfIdsPerThread, numberOfUpdatesPerId);
ut.start();
threads.add(ut);
}
startLatch.countDown();
latch.await();
for (UpdateThread ut : threads) {
ut.join(); //Threads should have finished because of the latch.await
}
//If are no errors every request received a response otherwise the test would have timedout
//aquiring the request outstanding semaphores.
for (Throwable throwable : failures) {
logger.info("Captured failure on concurrent update:", throwable);
}
assertThat(failures.size(), equalTo(0));
//Upsert all the ids one last time to make sure they are available at get time
//This means that we add 1 to the expected versions and attempts
//All the previous operations should be complete or failed at this point
for (int i = 0; i < numberOfIdsPerThread; ++i) {
UpdateResponse ur = client().prepareUpdate("test", "type1", Integer.toString(i))
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setRetryOnConflict(Integer.MAX_VALUE)
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject()).execute().actionGet();
}
refresh();
for (int i = 0; i < numberOfIdsPerThread; ++i) {
int totalFailures = 0;
GetResponse response = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
if (response.isExists()) {
assertThat(response.getId(), equalTo(Integer.toString(i)));
int expectedVersion = (numberOfThreads * numberOfUpdatesPerId * 2) + 1;
for (UpdateThread ut : threads) {
if (ut.failedMap.containsKey(i)) {
totalFailures += ut.failedMap.get(i);
}
}
expectedVersion -= totalFailures;
logger.error("Actual version [{}] Expected version [{}] Total failures [{}]", response.getVersion(), expectedVersion,
totalFailures);
assertThat(response.getVersion(), equalTo((long) expectedVersion));
assertThat(response.getVersion() + totalFailures, equalTo((long) ((numberOfUpdatesPerId * numberOfThreads * 2) + 1)));
}
}
}
private static String indexOrAlias() {
return randomBoolean() ? "test" : "alias";
}

View File

@ -21,9 +21,12 @@ package org.elasticsearch.script.javascript;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.plugins.PluginsService;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
@ -31,6 +34,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -71,10 +75,10 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value > 1");
SearchResponse response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > 1").lang("js")))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "js", "doc['num1'].value", null)
.execute().actionGet();
.setQuery(scriptQuery(new Script("doc['num1'].value > 1", ScriptService.ScriptType.INLINE, "JS", null)))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "JS", null))
.execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
@ -84,10 +88,10 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value > param1");
response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > param1").lang("js").addParam("param1", 2)))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "js", "doc['num1'].value", null)
.execute().actionGet();
.setQuery(scriptQuery(new Script("doc['num1'].value > param1", ScriptService.ScriptType.INLINE, "js", Collections.singletonMap("param1", 2))))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "js", null))
.execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(1l));
assertThat(response.getHits().getAt(0).id(), equalTo("3"));
@ -95,10 +99,10 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value > param1");
response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > param1").lang("js").addParam("param1", -1)))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "js", "doc['num1'].value", null)
.execute().actionGet();
.setQuery(scriptQuery(new Script("doc['num1'].value > param1", ScriptService.ScriptType.INLINE, "js", Collections.singletonMap("param1", -1))))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "js", null))
.execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
assertThat(response.getHits().getAt(0).id(), equalTo("1"));
@ -121,10 +125,10 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("s_obj1", "js", "_source.obj1", null)
.addScriptField("s_obj1_test", "js", "_source.obj1.test", null)
.addScriptField("s_obj2", "js", "_source.obj2", null)
.addScriptField("s_obj2_arr2", "js", "_source.obj2.arr2", null)
.addScriptField("s_obj1", new Script("_source.obj1", ScriptService.ScriptType.INLINE, "js", null))
.addScriptField("s_obj1_test", new Script("_source.obj1.test", ScriptService.ScriptType.INLINE, "js", null))
.addScriptField("s_obj2", new Script("_source.obj2", ScriptService.ScriptType.INLINE, "js", null))
.addScriptField("s_obj2_arr2", new Script("_source.obj2.arr2", ScriptService.ScriptType.INLINE, "js", null))
.execute().actionGet();
Map<String, Object> sObj1 = (Map<String, Object>) response.getHits().getAt(0).field("s_obj1").value();
@ -156,7 +160,7 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
SearchResponse response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("doc['num1'].value").lang("js"))))
.add(ScoreFunctionBuilders.scriptFunction(new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "js", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -171,7 +175,7 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("-doc['num1'].value").lang("js"))))
.add(ScoreFunctionBuilders.scriptFunction(new Script("-doc['num1'].value", ScriptService.ScriptType.INLINE, "js", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -187,7 +191,7 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("Math.pow(doc['num1'].value, 2)").lang("js"))))
.add(ScoreFunctionBuilders.scriptFunction(new Script("Math.pow(doc['num1'].value, 2)", ScriptService.ScriptType.INLINE, "js", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -202,7 +206,7 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("Math.max(doc['num1'].value, 1)").lang("js"))))
.add(ScoreFunctionBuilders.scriptFunction(new Script("Math.max(doc['num1'].value, 1)", ScriptService.ScriptType.INLINE, "js", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -217,7 +221,7 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("doc['num1'].value * _score").lang("js"))))
.add(ScoreFunctionBuilders.scriptFunction(new Script("doc['num1'].value * _score", ScriptService.ScriptType.INLINE, "js", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -230,9 +234,9 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running param1 * param2 * _score");
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("param1 * param2 * _score").param("param1", 2).param("param2", 2).lang("js"))))
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction(new Script("param1 * param2 * _score", ScriptService.ScriptType.INLINE, "js", MapBuilder.<String, Object>newMapBuilder().put("param1", 2).put("param2", 2).immutableMap())))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -253,9 +257,9 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
searchSource().query(
functionScoreQuery(
functionScoreQuery(
functionScoreQuery().add(scriptFunction("1").lang("js")))
.add(scriptFunction("_score.doubleValue()").lang("js")))
.add(scriptFunction("_score.doubleValue()").lang("js")
functionScoreQuery().add(scriptFunction(new Script("1", ScriptService.ScriptType.INLINE, "js", null))))
.add(scriptFunction(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "js", null))))
.add(scriptFunction(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "js", null))
)
)
)
@ -274,9 +278,9 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
searchRequest().source(
searchSource().query(
functionScoreQuery()
.add(scriptFunction("_score.doubleValue()").lang("js")
.add(scriptFunction(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "js", null))
)
).aggregation(terms("score_agg").script("_score.doubleValue()").lang("js"))
).aggregation(terms("score_agg").script(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "js", null)))
)
).actionGet();
assertSearchResponse(response);
@ -291,7 +295,7 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest {
index("index", "testtype", "1", jsonBuilder().startObject().field("f", 42).endObject());
ensureSearchable("index");
refresh();
SearchResponse response = client().prepareSearch().addScriptField("foobar", "js", "doc['f'].values.length", null).get();
SearchResponse response = client().prepareSearch().addScriptField("foobar", new Script("doc['f'].values.length", ScriptService.ScriptType.INLINE, "js", null)).get();
assertSearchResponse(response);
assertHitCount(response, 1);
assertThat((Integer) response.getHits().getAt(0).getFields().get("foobar").value(), equalTo(1));

View File

@ -21,9 +21,11 @@ package org.elasticsearch.script.python;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.plugins.PluginsService;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.SortOrder;
@ -35,6 +37,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -81,9 +84,9 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value > 1");
SearchResponse response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > 1").lang("python")))
.setQuery(scriptQuery(new Script("doc['num1'].value > 1", ScriptService.ScriptType.INLINE, "python", null)))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "python", "doc['num1'].value", null)
.addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "python", null))
.execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
@ -94,10 +97,10 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value > param1");
response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > param1").lang("python").addParam("param1", 2)))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "python", "doc['num1'].value", null)
.execute().actionGet();
.setQuery(scriptQuery(new Script("doc['num1'].value > param1", ScriptService.ScriptType.INLINE, "python", Collections.singletonMap("param1", 2))))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "python", null))
.execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(1l));
assertThat(response.getHits().getAt(0).id(), equalTo("3"));
@ -105,10 +108,10 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value > param1");
response = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(), scriptQuery("doc['num1'].value > param1").lang("python").addParam("param1", -1)))
.setQuery(scriptQuery(new Script("doc['num1'].value > param1", ScriptService.ScriptType.INLINE, "python", Collections.singletonMap("param1", -1))))
.addSort("num1", SortOrder.ASC)
.addScriptField("sNum1", "python", "doc['num1'].value", null)
.execute().actionGet();
.addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "python", null))
.execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
assertThat(response.getHits().getAt(0).id(), equalTo("1"));
@ -131,10 +134,10 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("s_obj1", "python", "_source['obj1']", null)
.addScriptField("s_obj1_test", "python", "_source['obj1']['test']", null)
.addScriptField("s_obj2", "python", "_source['obj2']", null)
.addScriptField("s_obj2_arr2", "python", "_source['obj2']['arr2']", null)
.addScriptField("s_obj1", new Script("_source['obj1']", ScriptService.ScriptType.INLINE, "python", null))
.addScriptField("s_obj1_test", new Script("_source['obj1']['test']", ScriptService.ScriptType.INLINE, "python", null))
.addScriptField("s_obj2", new Script("_source['obj2']", ScriptService.ScriptType.INLINE, "python", null))
.addScriptField("s_obj2_arr2", new Script("_source['obj2']['arr2']", ScriptService.ScriptType.INLINE, "python", null))
.execute().actionGet();
Map<String, Object> sObj1 = (Map<String, Object>) response.getHits().getAt(0).field("s_obj1").value();
@ -164,9 +167,9 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value");
SearchResponse response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("doc['num1'].value").lang("python"))))
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction(new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "python", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -179,9 +182,9 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running -doc['num1'].value");
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("-doc['num1'].value").lang("python"))))
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction(new Script("-doc['num1'].value", ScriptService.ScriptType.INLINE, "python", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -195,9 +198,9 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
logger.info(" --> running doc['num1'].value * _score");
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("doc['num1'].value * _score.doubleValue()").lang("python"))))
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction(new Script("doc['num1'].value * _score.doubleValue()", ScriptService.ScriptType.INLINE, "python", null)))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -212,8 +215,8 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
response = client().search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value"))
.add(ScoreFunctionBuilders.scriptFunction("param1 * param2 * _score.doubleValue()").param("param1", 2).param("param2", 2).lang("python"))))
).actionGet();
.add(ScoreFunctionBuilders.scriptFunction(new Script("param1 * param2 * _score.doubleValue()", ScriptService.ScriptType.INLINE, "python", MapBuilder.<String, Object>newMapBuilder().put("param1", 2).put("param2", 2).immutableMap())))))
).actionGet();
assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0));
@ -233,8 +236,7 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
index("test", "type1", "1", jsonBuilder().startObject().field("myfield", "foo").endObject());
refresh();
client().prepareUpdate("test", "type1", "1").setScriptLang("python")
.setScript("ctx[\"_source\"][\"myfield\"]=\"bar\"", ScriptService.ScriptType.INLINE)
client().prepareUpdate("test", "type1", "1").setScript(new Script("ctx[\"_source\"][\"myfield\"]=\"bar\"", ScriptService.ScriptType.INLINE, "python", null))
.execute().actionGet();
refresh();
@ -255,10 +257,10 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
searchSource().query(
functionScoreQuery(
functionScoreQuery(
functionScoreQuery().add(scriptFunction("1").lang("python")))
.add(scriptFunction("_score.doubleValue()").lang("python")))
.add(scriptFunction("_score.doubleValue()").lang("python")
)
functionScoreQuery().add(scriptFunction(new Script("1", ScriptService.ScriptType.INLINE, "python", null))))
.add(scriptFunction(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "python", null)))
.add(scriptFunction(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "python", null)))
)
)
)
).actionGet();
@ -276,9 +278,9 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
searchRequest().source(
searchSource().query(
functionScoreQuery()
.add(scriptFunction("_score.doubleValue()").lang("python")
.add(scriptFunction(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "python", null))
)
).aggregation(terms("score_agg").script("_score.doubleValue()").lang("python"))
).aggregation(terms("score_agg").script(new Script("_score.doubleValue()", ScriptService.ScriptType.INLINE, "python", null)))
)
).actionGet();
assertSearchResponse(response);
@ -297,8 +299,8 @@ public class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
index("test", "type1", "1", jsonBuilder().startObject().field("myfield", "foo").endObject());
refresh();
client().prepareUpdate("test", "type1", "1").setScriptLang("python")
.setScript("a=42; ctx[\"_source\"][\"myfield\"]=\"bar\"", ScriptService.ScriptType.INLINE)
client().prepareUpdate("test", "type1", "1")
.setScript(new Script("a=42; ctx[\"_source\"][\"myfield\"]=\"bar\"", ScriptService.ScriptType.INLINE, "python", null))
.execute().actionGet();
refresh();