From f13c216aa25e80fb5b1d55e6b5991fe4969e21c8 Mon Sep 17 00:00:00 2001 From: Robin Clarke Date: Tue, 9 Jun 2015 16:35:49 +0200 Subject: [PATCH 01/43] More information about 'Copy field to' --- docs/reference/mapping/types/core-types.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/mapping/types/core-types.asciidoc b/docs/reference/mapping/types/core-types.asciidoc index 43fcc07a0ca..76d2728a22f 100644 --- a/docs/reference/mapping/types/core-types.asciidoc +++ b/docs/reference/mapping/types/core-types.asciidoc @@ -563,7 +563,7 @@ The following Similarities are configured out-of-box: Adding `copy_to` parameter to any field mapping will cause all values of this field to be copied to fields specified in the parameter. In the following example all values from fields `title` and `abstract` will be copied to the field -`meta_data`. +`meta_data`. The field which is being copied to will be indexed (i.e. searchable, and available through `fielddata_field`) but the original source will not be modified. [source,js] From e7a99501b0cfbade6214b836021ad586d5149c94 Mon Sep 17 00:00:00 2001 From: Britta Weber Date: Thu, 11 Jun 2015 09:06:08 +0200 Subject: [PATCH 02/43] [TEST] remove node from nodes list if disruption is removed from node If we don't remove the node from the nodes list then later clearDisruption might fail in case we shut down the node before. --- .../test/disruption/NetworkPartition.java | 2 + .../disruption/NetworkPartitionTests.java | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java diff --git a/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartition.java b/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartition.java index 8206fafef4e..8c379b0a008 100644 --- a/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartition.java +++ b/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartition.java @@ -128,8 +128,10 @@ public abstract class NetworkPartition implements ServiceDisruptionScheme { Set otherSideNodes; if (nodesSideOne.contains(node)) { otherSideNodes = nodesSideTwo; + nodesSideOne.remove(node); } else if (nodesSideTwo.contains(node)) { otherSideNodes = nodesSideOne; + nodesSideTwo.remove(node); } else { return; } diff --git a/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java b/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java new file mode 100644 index 00000000000..cea9a0e6d7f --- /dev/null +++ b/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.elasticsearch.test.disruption; + + +import org.apache.lucene.util.LuceneTestCase; +import org.elasticsearch.test.ElasticsearchIntegrationTest; +import org.elasticsearch.test.InternalTestCluster; +import org.junit.Test; + +import java.io.IOException; + +@LuceneTestCase.Slow +public class NetworkPartitionTests extends ElasticsearchIntegrationTest{ + + @Test + public void testNetworkPartitionWithNodeShutdown() throws IOException { + internalCluster().ensureAtLeastNumDataNodes(2); + String[] nodeNames = internalCluster().getNodeNames(); + NetworkPartition networkPartition = new NetworkUnresponsivePartition(nodeNames[0], nodeNames[1], getRandom()); + internalCluster().setDisruptionScheme(networkPartition); + networkPartition.startDisrupting(); + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeNames[0])); + internalCluster().clearDisruptionScheme(); + } +} From acfb3196f448ce50266bca169ba70c8d577802b1 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 11 Jun 2015 06:03:09 -0700 Subject: [PATCH 03/43] Removing top-level filter parameter from search API --- .../src/main/java/org/elasticsearch/search/query/QueryPhase.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java index 00157061803..da1b3248e75 100644 --- a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -65,7 +65,6 @@ public class QueryPhase implements SearchPhase { .put("query", new QueryParseElement()) .put("queryBinary", new QueryBinaryParseElement()) .put("query_binary", new QueryBinaryParseElement()) - .put("filter", new PostFilterParseElement()) // For bw comp reason, should be removed in version 1.1 .put("post_filter", new PostFilterParseElement()) .put("postFilter", new PostFilterParseElement()) .put("filterBinary", new FilterBinaryParseElement()) From a216062d88205c0d109ac00a432b45bc90a14fe8 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 7 May 2015 15:08:36 +0100 Subject: [PATCH 04/43] Aggregations: allow users to perform simple arithmetic operations on histogram aggregations Closes #11029 --- .../aggregations/AggregationModule.java | 2 + .../TransportAggregationModule.java | 2 + .../aggregations/pipeline/BucketHelpers.java | 3 +- .../pipeline/PipelineAggregatorBuilders.java | 5 + .../SeriesArithmeticBuilder.java | 83 +++ .../SeriesArithmeticParser.java | 129 +++++ .../SeriesArithmeticPipelineAggregator.java | 179 +++++++ .../pipeline/SeriesArithmeticTests.java | 499 ++++++++++++++++++ docs/reference/aggregations/pipeline.asciidoc | 1 + .../series-arithmetic-aggregation.asciidoc | 149 ++++++ 10 files changed, 1051 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticBuilder.java create mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticParser.java create mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticPipelineAggregator.java create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/pipeline/SeriesArithmeticTests.java create mode 100644 docs/reference/aggregations/pipeline/series-arithmetic-aggregation.asciidoc diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/AggregationModule.java b/core/src/main/java/org/elasticsearch/search/aggregations/AggregationModule.java index fd6eabd15f5..8e86c8b5e37 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/AggregationModule.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/AggregationModule.java @@ -64,6 +64,7 @@ import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.sum.SumBucke import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativeParser; import org.elasticsearch.search.aggregations.pipeline.movavg.MovAvgParser; import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModelModule; +import org.elasticsearch.search.aggregations.pipeline.seriesarithmetic.SeriesArithmeticParser; import java.util.List; @@ -114,6 +115,7 @@ public class AggregationModule extends AbstractModule implements SpawnModules{ pipelineAggParsers.add(AvgBucketParser.class); pipelineAggParsers.add(SumBucketParser.class); pipelineAggParsers.add(MovAvgParser.class); + pipelineAggParsers.add(SeriesArithmeticParser.class); } /** diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/TransportAggregationModule.java b/core/src/main/java/org/elasticsearch/search/aggregations/TransportAggregationModule.java index 358abcdfda4..3a9f11f85ea 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/TransportAggregationModule.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/TransportAggregationModule.java @@ -69,6 +69,7 @@ import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipel import org.elasticsearch.search.aggregations.pipeline.derivative.InternalDerivative; import org.elasticsearch.search.aggregations.pipeline.movavg.MovAvgPipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.movavg.models.TransportMovAvgModelModule; +import org.elasticsearch.search.aggregations.pipeline.seriesarithmetic.SeriesArithmeticPipelineAggregator; /** * A module that registers all the transport streams for the addAggregation @@ -127,6 +128,7 @@ public class TransportAggregationModule extends AbstractModule implements SpawnM AvgBucketPipelineAggregator.registerStreams(); SumBucketPipelineAggregator.registerStreams(); MovAvgPipelineAggregator.registerStreams(); + SeriesArithmeticPipelineAggregator.registerStreams(); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketHelpers.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketHelpers.java index b96bf4af176..16a1dc5677c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketHelpers.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketHelpers.java @@ -172,7 +172,8 @@ public class BucketHelpers { value = ((InternalNumericMetricsAggregation.SingleValue) propertyValue).value(); } else { throw new AggregationExecutionException(DerivativeParser.BUCKETS_PATH.getPreferredName() - + " must reference either a number value or a single value numeric metric aggregation"); + + " must reference either a number value or a single value numeric metric aggregation, got: " + + propertyValue.getClass().getCanonicalName()); } // doc count never has missing values so gap policy doesn't apply here boolean isDocCountProperty = aggPathAsList.size() == 1 && "_count".equals(aggPathAsList.get(0)); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorBuilders.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorBuilders.java index 0d3bfe1f3fd..18adc122cba 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorBuilders.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorBuilders.java @@ -25,6 +25,7 @@ import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.min.MinBucke import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.sum.SumBucketBuilder; import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativeBuilder; import org.elasticsearch.search.aggregations.pipeline.movavg.MovAvgBuilder; +import org.elasticsearch.search.aggregations.pipeline.seriesarithmetic.SeriesArithmeticBuilder; public final class PipelineAggregatorBuilders { @@ -54,4 +55,8 @@ public final class PipelineAggregatorBuilders { public static final MovAvgBuilder movingAvg(String name) { return new MovAvgBuilder(name); } + + public static final SeriesArithmeticBuilder seriesArithmetic(String name) { + return new SeriesArithmeticBuilder(name); + } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticBuilder.java new file mode 100644 index 00000000000..7171b7c2b35 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticBuilder.java @@ -0,0 +1,83 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.pipeline.seriesarithmetic; + +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.Script.ScriptField; +import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; + +import java.io.IOException; +import java.util.Map; + +public class SeriesArithmeticBuilder extends PipelineAggregatorBuilder { + + private String format; + private GapPolicy gapPolicy; + private Script script; + private Map bucketsPathsMap; + + public SeriesArithmeticBuilder(String name) { + super(name, SeriesArithmeticPipelineAggregator.TYPE.name()); + } + + public SeriesArithmeticBuilder script(Script script) { + this.script = script; + return this; + } + + public SeriesArithmeticBuilder format(String format) { + this.format = format; + return this; + } + + public SeriesArithmeticBuilder gapPolicy(GapPolicy gapPolicy) { + this.gapPolicy = gapPolicy; + return this; + } + + /** + * Sets the paths to the buckets to use for this pipeline aggregator + */ + public SeriesArithmeticBuilder setBucketsPathsMap(Map bucketsPathsMap) { + this.bucketsPathsMap = bucketsPathsMap; + return this; + } + + @Override + protected XContentBuilder internalXContent(XContentBuilder builder, Params builderParams) throws IOException { + if (script != null) { + builder.field(ScriptField.SCRIPT.getPreferredName(), script); + } + if (format != null) { + builder.field(SeriesArithmeticParser.FORMAT.getPreferredName(), format); + } + if (gapPolicy != null) { + builder.field(SeriesArithmeticParser.GAP_POLICY.getPreferredName(), gapPolicy.getName()); + } + if (bucketsPathsMap != null) { + builder.field(PipelineAggregator.Parser.BUCKETS_PATH.getPreferredName(), bucketsPathsMap); + } + return builder; + } + +} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticParser.java new file mode 100644 index 00000000000..5638d5e165b --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticParser.java @@ -0,0 +1,129 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.pipeline.seriesarithmetic; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.Script.ScriptField; +import org.elasticsearch.search.SearchParseException; +import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorFactory; +import org.elasticsearch.search.aggregations.support.format.ValueFormat; +import org.elasticsearch.search.aggregations.support.format.ValueFormatter; +import org.elasticsearch.search.internal.SearchContext; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class SeriesArithmeticParser implements PipelineAggregator.Parser { + + public static final ParseField FORMAT = new ParseField("format"); + public static final ParseField GAP_POLICY = new ParseField("gap_policy"); + public static final ParseField PARAMS_FIELD = new ParseField("params"); + + @Override + public String type() { + return SeriesArithmeticPipelineAggregator.TYPE.name(); + } + + @Override + public PipelineAggregatorFactory parse(String reducerName, XContentParser parser, SearchContext context) throws IOException { + XContentParser.Token token; + Script script = null; + String currentFieldName = null; + Map bucketsPathsMap = null; + String format = null; + GapPolicy gapPolicy = GapPolicy.SKIP; + + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else if (token == XContentParser.Token.VALUE_STRING) { + if (FORMAT.match(currentFieldName)) { + format = parser.text(); + } else if (BUCKETS_PATH.match(currentFieldName)) { + bucketsPathsMap = new HashMap<>(); + bucketsPathsMap.put("_value", parser.text()); + } else if (GAP_POLICY.match(currentFieldName)) { + gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation()); + } else if (ScriptField.SCRIPT.match(currentFieldName)) { + script = Script.parse(parser); + } else { + throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: [" + + currentFieldName + "].", parser.getTokenLocation()); + } + } else if (token == XContentParser.Token.START_ARRAY) { + if (BUCKETS_PATH.match(currentFieldName)) { + List paths = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + String path = parser.text(); + paths.add(path); + } + bucketsPathsMap = new HashMap<>(); + for (int i = 0; i < paths.size(); i++) { + bucketsPathsMap.put("_value" + i, paths.get(i)); + } + } else { + throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: [" + + currentFieldName + "].", parser.getTokenLocation()); + } + } else if (token == XContentParser.Token.START_OBJECT) { + if (ScriptField.SCRIPT.match(currentFieldName)) { + script = Script.parse(parser); + } else if (BUCKETS_PATH.match(currentFieldName)) { + Map map = parser.map(); + bucketsPathsMap = new HashMap<>(); + for (Map.Entry entry : map.entrySet()) { + bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue())); + } + } else { + throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: [" + + currentFieldName + "].", parser.getTokenLocation()); + } + } else { + throw new SearchParseException(context, "Unexpected token " + token + " in [" + reducerName + "].", + parser.getTokenLocation()); + } + } + + if (bucketsPathsMap == null) { + throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName() + + "] for series_arithmetic aggregation [" + reducerName + "]", parser.getTokenLocation()); + } + + if (script == null) { + throw new SearchParseException(context, "Missing required field [" + ScriptField.SCRIPT.getPreferredName() + + "] for series_arithmetic aggregation [" + reducerName + "]", parser.getTokenLocation()); + } + + ValueFormatter formatter = null; + if (format != null) { + formatter = ValueFormat.Patternable.Number.format(format).formatter(); + } + + return new SeriesArithmeticPipelineAggregator.Factory(reducerName, bucketsPathsMap, script, formatter, gapPolicy); + } + +} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticPipelineAggregator.java new file mode 100644 index 00000000000..b2855204ce1 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/seriesarithmetic/SeriesArithmeticPipelineAggregator.java @@ -0,0 +1,179 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.pipeline.seriesarithmetic; + +import com.google.common.base.Function; +import com.google.common.collect.Lists; + +import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.script.CompiledScript; +import org.elasticsearch.script.ExecutableScript; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptContext; +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.AggregationExecutionException; +import org.elasticsearch.search.aggregations.InternalAggregation; +import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; +import org.elasticsearch.search.aggregations.InternalAggregation.Type; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation; +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket; +import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; +import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorFactory; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; +import org.elasticsearch.search.aggregations.support.format.ValueFormatter; +import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue; + +public class SeriesArithmeticPipelineAggregator extends PipelineAggregator { + + public final static Type TYPE = new Type("series_arithmetic"); + + public final static PipelineAggregatorStreams.Stream STREAM = new PipelineAggregatorStreams.Stream() { + @Override + public SeriesArithmeticPipelineAggregator readResult(StreamInput in) throws IOException { + SeriesArithmeticPipelineAggregator result = new SeriesArithmeticPipelineAggregator(); + result.readFrom(in); + return result; + } + }; + + public static void registerStreams() { + PipelineAggregatorStreams.registerStream(STREAM, TYPE.stream()); + } + + private static final Function FUNCTION = new Function() { + @Override + public InternalAggregation apply(Aggregation input) { + return (InternalAggregation) input; + } + }; + + private ValueFormatter formatter; + private GapPolicy gapPolicy; + + private Script script; + + private Map bucketsPathsMap; + + public SeriesArithmeticPipelineAggregator() { + } + + public SeriesArithmeticPipelineAggregator(String name, Map bucketsPathsMap, Script script, @Nullable ValueFormatter formatter, + GapPolicy gapPolicy, Map metadata) { + super(name, bucketsPathsMap.values().toArray(new String[bucketsPathsMap.size()]), metadata); + this.bucketsPathsMap = bucketsPathsMap; + this.script = script; + this.formatter = formatter; + this.gapPolicy = gapPolicy; + } + + @Override + public Type type() { + return TYPE; + } + + @Override + public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) { + InternalMultiBucketAggregation originalAgg = (InternalMultiBucketAggregation) aggregation; + List buckets = originalAgg.getBuckets(); + + CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS); + List newBuckets = new ArrayList<>(); + for (Bucket bucket : buckets) { + Map vars = new HashMap<>(); + if (script.getParams() != null) { + vars.putAll(script.getParams()); + } + for (Map.Entry entry : bucketsPathsMap.entrySet()) { + String varName = entry.getKey(); + String bucketsPath = entry.getValue(); + Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); + vars.put(varName, value); + } + ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars); + Object returned = executableScript.run(); + if (returned == null) { + newBuckets.add(bucket); + } else { + if (!(returned instanceof Number)) { + throw new AggregationExecutionException("series_arithmetic script for reducer [" + name() + "] must return a Number"); + } + List aggs = new ArrayList<>(Lists.transform(bucket.getAggregations().asList(), FUNCTION)); + aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter, new ArrayList(), + metaData())); + InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), + (InternalMultiBucketAggregation.InternalBucket) bucket); + newBuckets.add(newBucket); + } + } + return originalAgg.create(newBuckets); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + script.writeTo(out); + ValueFormatterStreams.writeOptional(formatter, out); + gapPolicy.writeTo(out); + out.writeGenericValue(bucketsPathsMap); + } + + @SuppressWarnings("unchecked") + @Override + protected void doReadFrom(StreamInput in) throws IOException { + script = Script.readScript(in); + formatter = ValueFormatterStreams.readOptional(in); + gapPolicy = GapPolicy.readFrom(in); + bucketsPathsMap = (Map) in.readGenericValue(); + } + + public static class Factory extends PipelineAggregatorFactory { + + private Script script; + private final ValueFormatter formatter; + private GapPolicy gapPolicy; + private Map bucketsPathsMap; + + public Factory(String name, Map bucketsPathsMap, Script script, @Nullable ValueFormatter formatter, GapPolicy gapPolicy) { + super(name, TYPE.name(), bucketsPathsMap.values().toArray(new String[bucketsPathsMap.size()])); + this.bucketsPathsMap = bucketsPathsMap; + this.script = script; + this.formatter = formatter; + this.gapPolicy = gapPolicy; + } + + @Override + protected PipelineAggregator createInternal(Map metaData) throws IOException { + return new SeriesArithmeticPipelineAggregator(name, bucketsPathsMap, script, formatter, gapPolicy, metaData); + } + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/SeriesArithmeticTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/SeriesArithmeticTests.java new file mode 100644 index 00000000000..2fbbb4d717a --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/SeriesArithmeticTests.java @@ -0,0 +1,499 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.pipeline; + +import org.elasticsearch.action.index.IndexRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptService.ScriptType; +import org.elasticsearch.script.groovy.GroovyScriptEngineService; +import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; +import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram; +import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram.Bucket; +import org.elasticsearch.search.aggregations.metrics.sum.Sum; +import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; +import org.elasticsearch.test.ElasticsearchIntegrationTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.search.aggregations.AggregationBuilders.histogram; +import static org.elasticsearch.search.aggregations.AggregationBuilders.sum; +import static org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.seriesArithmetic; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +@ElasticsearchIntegrationTest.SuiteScopeTest +public class SeriesArithmeticTests extends ElasticsearchIntegrationTest { + + private static final String FIELD_1_NAME = "field1"; + private static final String FIELD_2_NAME = "field2"; + private static final String FIELD_3_NAME = "field3"; + private static final String FIELD_4_NAME = "field4"; + + private static int interval; + private static int numDocs; + private static int minNumber; + private static int maxNumber; + + @Override + public void setupSuiteScopeCluster() throws Exception { + createIndex("idx"); + createIndex("idx_unmapped"); + + interval = randomIntBetween(1, 50); + numDocs = randomIntBetween(10, 500); + minNumber = -200; + maxNumber = 200; + + List builders = new ArrayList<>(); + for (int docs = 0; docs < numDocs; docs++) { + builders.add(client().prepareIndex("idx", "type").setSource(newDocBuilder())); + } + + client().preparePutIndexedScript().setId("my_script").setScriptLang(GroovyScriptEngineService.NAME).setSource("{ \"script\": \"_value0 + _value1 + _value2\" }").get(); + + indexRandom(true, builders); + ensureSearchable(); + } + + private XContentBuilder newDocBuilder() throws IOException { + XContentBuilder jsonBuilder = jsonBuilder(); + jsonBuilder.startObject(); + jsonBuilder.field(FIELD_1_NAME, randomIntBetween(minNumber, maxNumber)); + jsonBuilder.field(FIELD_2_NAME, randomIntBetween(minNumber, maxNumber)); + jsonBuilder.field(FIELD_3_NAME, randomIntBetween(minNumber, maxNumber)); + jsonBuilder.field(FIELD_4_NAME, randomIntBetween(minNumber, maxNumber)); + jsonBuilder.endObject(); + return jsonBuilder; + } + + @Test + public void inlineScript() { + + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("_value0 + _value1 + _value2", ScriptType.INLINE, null, null)))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertTrue(Double.isNaN(seriesArithmeticValue)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); + } + } + } + + @Test + public void inlineScript2() { + + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("_value0 + _value1 / _value2", ScriptType.INLINE, null, null)))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertTrue(Double.isNaN(seriesArithmeticValue)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue / field4SumValue)); + } + } + } + + @Test + public void inlineScriptSingleVariable() { + + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum").script( + new Script("_value0", ScriptType.INLINE, null, null)))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertTrue(Double.isNaN(seriesArithmeticValue)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue)); + } + } + } + + @Test + public void inlineScriptNamedVars() { + + Map bucketsPathsMap = new HashMap<>(); + bucketsPathsMap.put("foo", "field2Sum"); + bucketsPathsMap.put("bar", "field3Sum"); + bucketsPathsMap.put("baz", "field4Sum"); + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPathsMap(bucketsPathsMap ).script( + new Script("foo + bar + baz", ScriptType.INLINE, null, null)))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertTrue(Double.isNaN(seriesArithmeticValue)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); + } + } + } + + @Test + public void inlineScriptWithParams() { + + Map params = new HashMap<>(); + params.put("factor", 3); + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("(_value0 + _value1 + _value2) * factor", ScriptType.INLINE, null, params)))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertTrue(Double.isNaN(seriesArithmeticValue)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo((field2SumValue + field3SumValue + field4SumValue) * 3)); + } + } + } + + @Test + public void inlineScriptInsertZeros() { + + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("_value0 + _value1 + _value2", ScriptType.INLINE, null, null)).gapPolicy(GapPolicy.INSERT_ZEROS))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(0.0)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); + } + } + } + + @Test + public void indexedScript() { + + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("my_script", ScriptType.INDEXED, null, null)).gapPolicy(GapPolicy.INSERT_ZEROS))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(0.0)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); + } + } + } + + @Test + public void unmapped() throws Exception { + SearchResponse response = client() + .prepareSearch("idx_unmapped") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("_value0 + _value1 + _value2", ScriptType.INLINE, null, null)))) + .execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram deriv = response.getAggregations().get("histo"); + assertThat(deriv, notNullValue()); + assertThat(deriv.getName(), equalTo("histo")); + assertThat(deriv.getBuckets().size(), equalTo(0)); + } + + @Test + public void partiallyUnmapped() throws Exception { + SearchResponse response = client() + .prepareSearch("idx", "idx_unmapped") + .addAggregation( + histogram("histo") + .field(FIELD_1_NAME) + .interval(interval) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + seriesArithmetic("seriesArithmetic").setBucketsPaths("field2Sum", "field3Sum", "field4Sum").script( + new Script("_value0 + _value1 + _value2", ScriptType.INLINE, null, null)))).execute().actionGet(); + + assertSearchResponse(response); + + InternalHistogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Histogram.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertTrue(Double.isNaN(seriesArithmeticValue)); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); + } + } + } +} diff --git a/docs/reference/aggregations/pipeline.asciidoc b/docs/reference/aggregations/pipeline.asciidoc index c991086532e..dbf63180144 100644 --- a/docs/reference/aggregations/pipeline.asciidoc +++ b/docs/reference/aggregations/pipeline.asciidoc @@ -160,3 +160,4 @@ include::pipeline/max-bucket-aggregation.asciidoc[] include::pipeline/min-bucket-aggregation.asciidoc[] include::pipeline/sum-bucket-aggregation.asciidoc[] include::pipeline/movavg-aggregation.asciidoc[] +include::pipeline/series-arithmetic-aggregation.asciidoc[] diff --git a/docs/reference/aggregations/pipeline/series-arithmetic-aggregation.asciidoc b/docs/reference/aggregations/pipeline/series-arithmetic-aggregation.asciidoc new file mode 100644 index 00000000000..dc39ab326d2 --- /dev/null +++ b/docs/reference/aggregations/pipeline/series-arithmetic-aggregation.asciidoc @@ -0,0 +1,149 @@ +[[search-aggregations-pipeline-series-arithmetic-aggregation]] +=== Series Arithmetic Aggregation + +A parent pipeline aggregation which executes a script which can perform per bucket computations on specified metrics +in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a numeric value. + +==== Syntax + +A `series_arithmetic` aggregation looks like this in isolation: + +[source,js] +-------------------------------------------------- +{ + "series_arithmetic": { + "buckets_path": { + "my_var1": "the_sum", <1> + "my_var2": "the_value_count" + }, + script: "my_var1 / my_var2" + } +} +-------------------------------------------------- +<1> Here, `my_var1` is the name of the variable for this buckets path to use in the script, `the_sum` is the path to +the metrics to use for that variable. + + +.`series_arithmetic` Parameters +|=== +|Parameter Name |Description |Required |Default Value +|`script` |The script to run for this aggregation. The script can be inline, file or indexed. (see <> +for more details) |Required | +|`buckets_path` |A map of script variables and their associated path to the buckets we wish to use for the variable +(see <> for more details) |Required | + |`gap_policy` |The policy to apply when gaps are found in the data (see <> for more + details)|Optional, defaults to `skip` | + |`format` |format to apply to the output value of this aggregation |Optional, defaults to `null` | +|=== + +The following snippet calculates the ratio percentage of t-shirt sales compared to total sales each month: + +[source,js] +-------------------------------------------------- +{ + "aggs" : { + "sales_per_month" : { + "date_histogram" : { + "field" : "date", + "interval" : "month" + }, + "aggs": { + "total_sales": { + "sum": { + "field": "price" + } + }, + "t-shirts": { + "filter": { + "term": { + "type": "t-shirt" + } + }, + "aggs": { + "sales": { + "sum": { + "field": "price" + } + } + } + }, + "t-shirt-percentage": { + "series_arithmetic": { + "buckets_paths": { + "tShirtSales": "t-shirts>sales", + "totalSales": "total_sales" + }, + "script": "tShirtSales / totalSales * 100" + } + } + } + } + } +} +-------------------------------------------------- + +And the following may be the response: + +[source,js] +-------------------------------------------------- +{ + "aggregations": { + "sales_per_month": { + "buckets": [ + { + "key_as_string": "2015/01/01 00:00:00", + "key": 1420070400000, + "doc_count": 3, + "total_sales": { + "value": 50 + }, + "t-shirts": { + "doc_count": 2, + "sales": { + "value": 10 + } + }, + "t-shirt-percentage": { + "value": 20 + } + }, + { + "key_as_string": "2015/02/01 00:00:00", + "key": 1422748800000, + "doc_count": 2 + "total_sales": { + "value": 60 + }, + "t-shirts": { + "doc_count": 1, + "sales": { + "value": 15 + } + }, + "t-shirt-percentage": { + "value": 25 + } + }, + { + "key_as_string": "2015/03/01 00:00:00", + "key": 1425168000000, + "doc_count": 2, + "total_sales": { + "value": 40 + }, + "t-shirts": { + "doc_count": 1, + "sales": { + "value": 20 + } + }, + "t-shirt-percentage": { + "value": 50 + } + } + ] + } + } +} +-------------------------------------------------- + From fab27f1bb032f573b16b53ef4d5bf062314d0ebe Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 11 Jun 2015 17:32:29 +0200 Subject: [PATCH 05/43] Create ShardSuggestService/Metrics manually outside of guice This ShardSuggestService is a simple metrics counter and doesn't need to be injected. It just makes IndexShard ctor bigger for no reason. --- .../action/suggest/TransportSuggestAction.java | 8 ++++---- .../org/elasticsearch/index/shard/IndexShard.java | 15 +++++++-------- .../index/shard/IndexShardModule.java | 2 -- .../index/shard/ShadowIndexShard.java | 5 ++--- ...uggestService.java => ShardSuggestMetric.java} | 9 +-------- 5 files changed, 14 insertions(+), 25 deletions(-) rename core/src/main/java/org/elasticsearch/index/suggest/stats/{ShardSuggestService.java => ShardSuggestMetric.java} (89%) diff --git a/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java b/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java index ac046e6552d..27caf314b0d 100644 --- a/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java +++ b/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java @@ -39,7 +39,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.shard.IndexShard; -import org.elasticsearch.index.suggest.stats.ShardSuggestService; +import org.elasticsearch.index.suggest.stats.ShardSuggestMetric; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.search.suggest.SuggestPhase; @@ -130,8 +130,8 @@ public class TransportSuggestAction extends TransportBroadcastAction Date: Thu, 11 Jun 2015 17:46:50 +0200 Subject: [PATCH 06/43] Fold ShardGetService creation away from Guice into IndexShard it's always acccessed via IndexShard and has crazy circular dependencies or rather had. It just makes IndexShard ctor bigger for no reason. --- .../index/get/ShardGetService.java | 37 ++++++------------- .../elasticsearch/index/shard/IndexShard.java | 6 +-- .../index/shard/IndexShardModule.java | 2 - .../index/shard/ShadowIndexShard.java | 5 +-- 4 files changed, 16 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java index 6af30b0001a..ff69941fe42 100644 --- a/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -64,41 +64,26 @@ import static com.google.common.collect.Maps.newHashMapWithExpectedSize; /** */ -public class ShardGetService extends AbstractIndexShardComponent { - - private final ScriptService scriptService; - +public final class ShardGetService extends AbstractIndexShardComponent { private final MapperService mapperService; - - private final IndexFieldDataService fieldDataService; - - private IndexShard indexShard; - private final MeanMetric existsMetric = new MeanMetric(); private final MeanMetric missingMetric = new MeanMetric(); private final CounterMetric currentMetric = new CounterMetric(); + private final IndexShard indexShard; - @Inject - public ShardGetService(ShardId shardId, @IndexSettings Settings indexSettings, ScriptService scriptService, - MapperService mapperService, IndexFieldDataService fieldDataService) { - super(shardId, indexSettings); - this.scriptService = scriptService; + public ShardGetService(IndexShard indexShard, + MapperService mapperService) { + super(indexShard.shardId(), indexShard.indexSettings()); this.mapperService = mapperService; - this.fieldDataService = fieldDataService; + this.indexShard = indexShard; } public GetStats stats() { return new GetStats(existsMetric.count(), TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()), missingMetric.count(), TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()), currentMetric.count()); } - // sadly, to overcome cyclic dep, we need to do this and inject it ourselves... - public ShardGetService setIndexShard(IndexShard indexShard) { - this.indexShard = indexShard; - return this; - } - public GetResult get(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) - { + public GetResult get(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) { currentMetric.inc(); try { long now = System.nanoTime(); @@ -151,7 +136,7 @@ public class ShardGetService extends AbstractIndexShardComponent { /** * decides what needs to be done based on the request input and always returns a valid non-null FetchSourceContext */ - protected FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSourceContext context, @Nullable String[] gFields) { + private FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSourceContext context, @Nullable String[] gFields) { if (context != null) { return context; } @@ -166,7 +151,7 @@ public class ShardGetService extends AbstractIndexShardComponent { return FetchSourceContext.DO_NOT_FETCH_SOURCE; } - public GetResult innerGet(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) { + private GetResult innerGet(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) { fetchSourceContext = normalizeFetchSourceContent(fetchSourceContext, gFields); boolean loadSource = (gFields != null && gFields.length > 0) || fetchSourceContext.fetchSource(); @@ -238,7 +223,7 @@ public class ShardGetService extends AbstractIndexShardComponent { value = source.source.length(); } else { if (searchLookup == null) { - searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type}); + searchLookup = new SearchLookup(mapperService, null, new String[]{type}); searchLookup.source().setSource(source.source); } @@ -370,7 +355,7 @@ public class ShardGetService extends AbstractIndexShardComponent { } } else if (!fieldMapper.fieldType().stored() && !fieldMapper.isGenerated()) { if (searchLookup == null) { - searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type}); + searchLookup = new SearchLookup(mapperService, null, new String[]{type}); LeafSearchLookup leafSearchLookup = searchLookup.getLeafSearchLookup(docIdAndVersion.context); searchLookup.source().setSource(source); leafSearchLookup.setDocument(docIdAndVersion.docId); diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java index d3142e0d253..60c77985d3e 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -22,7 +22,7 @@ package org.elasticsearch.index.shard; import com.google.common.base.Charsets; import com.google.common.base.Preconditions; import org.apache.lucene.codecs.PostingsFormat; -import org.apache.lucene.index.*; +import org.apache.lucene.index.CheckIndex; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.ThreadInterruptedException; @@ -192,7 +192,7 @@ public class IndexShard extends AbstractIndexShardComponent { @Inject public IndexShard(ShardId shardId, IndexSettingsService indexSettingsService, IndicesLifecycle indicesLifecycle, Store store, - ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, ShardIndexingService indexingService, ShardGetService getService, ShardSearchService searchService, ShardIndexWarmerService shardWarmerService, + ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, ShardIndexingService indexingService, ShardSearchService searchService, ShardIndexWarmerService shardWarmerService, ShardFilterCache shardFilterCache, ShardFieldData shardFieldData, PercolatorQueriesRegistry percolatorQueriesRegistry, ShardPercolateService shardPercolateService, CodecService codecService, ShardTermVectorsService termVectorsService, IndexFieldDataService indexFieldDataService, IndexService indexService, ShardSuggestService shardSuggestService, ShardQueryCache shardQueryCache, ShardBitsetFilterCache shardBitsetFilterCache, @@ -216,7 +216,7 @@ public class IndexShard extends AbstractIndexShardComponent { this.indexCache = indexCache; this.indexAliasesService = indexAliasesService; this.indexingService = indexingService; - this.getService = getService.setIndexShard(this); + this.getService = new ShardGetService(this, mapperService); this.termVectorsService = termVectorsService.setIndexShard(this); this.searchService = searchService; this.shardWarmerService = shardWarmerService; diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java index f5fd555b989..b54ac6d1b2f 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.engine.InternalEngineFactory; import org.elasticsearch.index.fielddata.ShardFieldData; import org.elasticsearch.index.gateway.IndexShardGateway; import org.elasticsearch.index.gateway.IndexShardGatewayService; -import org.elasticsearch.index.get.ShardGetService; import org.elasticsearch.index.indexing.ShardIndexingService; import org.elasticsearch.index.indexing.slowlog.ShardSlowLogIndexingService; import org.elasticsearch.index.percolator.PercolatorQueriesRegistry; @@ -92,7 +91,6 @@ public class IndexShardModule extends AbstractModule { bind(ShardSlowLogIndexingService.class).asEagerSingleton(); bind(ShardSearchService.class).asEagerSingleton(); bind(ShardSlowLogSearchService.class).asEagerSingleton(); - bind(ShardGetService.class).asEagerSingleton(); bind(ShardFilterCache.class).toInstance(shardFilterCache); bind(ShardQueryCache.class).asEagerSingleton(); bind(ShardBitsetFilterCache.class).asEagerSingleton(); diff --git a/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java index 6e1af76f94c..7179cc1d9b9 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java @@ -37,7 +37,6 @@ import org.elasticsearch.index.engine.EngineConfig; import org.elasticsearch.index.engine.EngineFactory; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.fielddata.ShardFieldData; -import org.elasticsearch.index.get.ShardGetService; import org.elasticsearch.index.indexing.ShardIndexingService; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.merge.MergeStats; @@ -71,7 +70,7 @@ public final class ShadowIndexShard extends IndexShard { ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, ShardIndexingService indexingService, - ShardGetService getService, ShardSearchService searchService, + ShardSearchService searchService, ShardIndexWarmerService shardWarmerService, ShardFilterCache shardFilterCache, ShardFieldData shardFieldData, PercolatorQueriesRegistry percolatorQueriesRegistry, ShardPercolateService shardPercolateService, CodecService codecService, @@ -83,7 +82,7 @@ public final class ShadowIndexShard extends IndexShard { NodeEnvironment nodeEnv, ShardPath path, BigArrays bigArrays) throws IOException { super(shardId, indexSettingsService, indicesLifecycle, store, threadPool, mapperService, queryParserService, indexCache, indexAliasesService, - indexingService, getService, searchService, shardWarmerService, shardFilterCache, + indexingService, searchService, shardWarmerService, shardFilterCache, shardFieldData, percolatorQueriesRegistry, shardPercolateService, codecService, termVectorsService, indexFieldDataService, indexService, shardSuggestService, shardQueryCache, shardBitsetFilterCache, warmer, deletionPolicy, similarityService, From 5f40f97740d60f96ae2509993f45c0da33857588 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 11 Jun 2015 17:46:50 +0200 Subject: [PATCH 07/43] Fold ShardGetService creation away from Guice into IndexShard it's always acccessed via IndexShard and has crazy circular dependencies or rather had. It just makes IndexShard ctor bigger for no reason. --- .../index/get/ShardGetService.java | 37 ++++++------------- .../elasticsearch/index/shard/IndexShard.java | 4 +- .../index/shard/IndexShardModule.java | 2 - .../index/shard/ShadowIndexShard.java | 5 +-- 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java index 6af30b0001a..ff69941fe42 100644 --- a/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/core/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -64,41 +64,26 @@ import static com.google.common.collect.Maps.newHashMapWithExpectedSize; /** */ -public class ShardGetService extends AbstractIndexShardComponent { - - private final ScriptService scriptService; - +public final class ShardGetService extends AbstractIndexShardComponent { private final MapperService mapperService; - - private final IndexFieldDataService fieldDataService; - - private IndexShard indexShard; - private final MeanMetric existsMetric = new MeanMetric(); private final MeanMetric missingMetric = new MeanMetric(); private final CounterMetric currentMetric = new CounterMetric(); + private final IndexShard indexShard; - @Inject - public ShardGetService(ShardId shardId, @IndexSettings Settings indexSettings, ScriptService scriptService, - MapperService mapperService, IndexFieldDataService fieldDataService) { - super(shardId, indexSettings); - this.scriptService = scriptService; + public ShardGetService(IndexShard indexShard, + MapperService mapperService) { + super(indexShard.shardId(), indexShard.indexSettings()); this.mapperService = mapperService; - this.fieldDataService = fieldDataService; + this.indexShard = indexShard; } public GetStats stats() { return new GetStats(existsMetric.count(), TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()), missingMetric.count(), TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()), currentMetric.count()); } - // sadly, to overcome cyclic dep, we need to do this and inject it ourselves... - public ShardGetService setIndexShard(IndexShard indexShard) { - this.indexShard = indexShard; - return this; - } - public GetResult get(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) - { + public GetResult get(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) { currentMetric.inc(); try { long now = System.nanoTime(); @@ -151,7 +136,7 @@ public class ShardGetService extends AbstractIndexShardComponent { /** * decides what needs to be done based on the request input and always returns a valid non-null FetchSourceContext */ - protected FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSourceContext context, @Nullable String[] gFields) { + private FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSourceContext context, @Nullable String[] gFields) { if (context != null) { return context; } @@ -166,7 +151,7 @@ public class ShardGetService extends AbstractIndexShardComponent { return FetchSourceContext.DO_NOT_FETCH_SOURCE; } - public GetResult innerGet(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) { + private GetResult innerGet(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType, FetchSourceContext fetchSourceContext, boolean ignoreErrorsOnGeneratedFields) { fetchSourceContext = normalizeFetchSourceContent(fetchSourceContext, gFields); boolean loadSource = (gFields != null && gFields.length > 0) || fetchSourceContext.fetchSource(); @@ -238,7 +223,7 @@ public class ShardGetService extends AbstractIndexShardComponent { value = source.source.length(); } else { if (searchLookup == null) { - searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type}); + searchLookup = new SearchLookup(mapperService, null, new String[]{type}); searchLookup.source().setSource(source.source); } @@ -370,7 +355,7 @@ public class ShardGetService extends AbstractIndexShardComponent { } } else if (!fieldMapper.fieldType().stored() && !fieldMapper.isGenerated()) { if (searchLookup == null) { - searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type}); + searchLookup = new SearchLookup(mapperService, null, new String[]{type}); LeafSearchLookup leafSearchLookup = searchLookup.getLeafSearchLookup(docIdAndVersion.context); searchLookup.source().setSource(source); leafSearchLookup.setDocument(docIdAndVersion.docId); diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 3dde442dbcf..9d4e498868a 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -192,7 +192,7 @@ public class IndexShard extends AbstractIndexShardComponent { @Inject public IndexShard(ShardId shardId, IndexSettingsService indexSettingsService, IndicesLifecycle indicesLifecycle, Store store, - ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, ShardIndexingService indexingService, ShardGetService getService, ShardSearchService searchService, ShardIndexWarmerService shardWarmerService, + ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, ShardIndexingService indexingService, ShardSearchService searchService, ShardIndexWarmerService shardWarmerService, ShardFilterCache shardFilterCache, ShardFieldData shardFieldData, PercolatorQueriesRegistry percolatorQueriesRegistry, ShardPercolateService shardPercolateService, CodecService codecService, ShardTermVectorsService termVectorsService, IndexFieldDataService indexFieldDataService, IndexService indexService, ShardQueryCache shardQueryCache, ShardBitsetFilterCache shardBitsetFilterCache, @@ -216,7 +216,7 @@ public class IndexShard extends AbstractIndexShardComponent { this.indexCache = indexCache; this.indexAliasesService = indexAliasesService; this.indexingService = indexingService; - this.getService = getService.setIndexShard(this); + this.getService = new ShardGetService(this, mapperService); this.termVectorsService = termVectorsService.setIndexShard(this); this.searchService = searchService; this.shardWarmerService = shardWarmerService; diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java index 11e1e853356..7d7074d001c 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShardModule.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.engine.InternalEngineFactory; import org.elasticsearch.index.fielddata.ShardFieldData; import org.elasticsearch.index.gateway.IndexShardGateway; import org.elasticsearch.index.gateway.IndexShardGatewayService; -import org.elasticsearch.index.get.ShardGetService; import org.elasticsearch.index.indexing.ShardIndexingService; import org.elasticsearch.index.indexing.slowlog.ShardSlowLogIndexingService; import org.elasticsearch.index.percolator.PercolatorQueriesRegistry; @@ -91,7 +90,6 @@ public class IndexShardModule extends AbstractModule { bind(ShardSlowLogIndexingService.class).asEagerSingleton(); bind(ShardSearchService.class).asEagerSingleton(); bind(ShardSlowLogSearchService.class).asEagerSingleton(); - bind(ShardGetService.class).asEagerSingleton(); bind(ShardFilterCache.class).toInstance(shardFilterCache); bind(ShardQueryCache.class).asEagerSingleton(); bind(ShardBitsetFilterCache.class).asEagerSingleton(); diff --git a/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java index 76211df9627..4a8d3f4a843 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/ShadowIndexShard.java @@ -37,7 +37,6 @@ import org.elasticsearch.index.engine.EngineConfig; import org.elasticsearch.index.engine.EngineFactory; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.fielddata.ShardFieldData; -import org.elasticsearch.index.get.ShardGetService; import org.elasticsearch.index.indexing.ShardIndexingService; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.merge.MergeStats; @@ -70,7 +69,7 @@ public final class ShadowIndexShard extends IndexShard { ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, ShardIndexingService indexingService, - ShardGetService getService, ShardSearchService searchService, + ShardSearchService searchService, ShardIndexWarmerService shardWarmerService, ShardFilterCache shardFilterCache, ShardFieldData shardFieldData, PercolatorQueriesRegistry percolatorQueriesRegistry, ShardPercolateService shardPercolateService, CodecService codecService, @@ -82,7 +81,7 @@ public final class ShadowIndexShard extends IndexShard { NodeEnvironment nodeEnv, ShardPath path, BigArrays bigArrays) throws IOException { super(shardId, indexSettingsService, indicesLifecycle, store, threadPool, mapperService, queryParserService, indexCache, indexAliasesService, - indexingService, getService, searchService, shardWarmerService, shardFilterCache, + indexingService, searchService, shardWarmerService, shardFilterCache, shardFieldData, percolatorQueriesRegistry, shardPercolateService, codecService, termVectorsService, indexFieldDataService, indexService, shardQueryCache, shardBitsetFilterCache, warmer, deletionPolicy, similarityService, From a41f68f9fe11d9b4fbfe86529dbcb3d328af473c Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 12 Jun 2015 13:56:35 +0200 Subject: [PATCH 08/43] [build] update maven-shade-plugin to 2.4 ## Release Notes - Apache Maven Shade - Version 2.4 https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12317921&version=12331393 ### Bugs: * [MSHADE-155] - dependency-reduced-pom should use shadedArtifactId * [MSHADE-169] - Typos in warning message * [MSHADE-172] - "java.lang.ArithmeticException: / by zero" in MinijarFilter * [MSHADE-174] - Unable to shade Java 8 jarfiles with static interface methods using minimizeJar * [MSHADE-183] - Getting "Error creating shaded jar: java.util.jar.Attributes cannot be cast to java.lang.String" error when using ManifestResourceTransformer with Maven 3.2.5 * [MSHADE-185] - systemPath content is interpolated for system dependencies ### Improvements: * [MSHADE-177] - MavenProject/MavenSession Injection as a paremeter instead as a component. * [MSHADE-178] - Removing plexus-container-default dependency * [MSHADE-179] - Fix RAT Report * [MSHADE-180] - Upgrade plexus-utils to 3.0.18 * [MSHADE-188] - Upgrade maven-dependency-tree to 2.2 * [MSHADE-191] - Upgrade plexus-utils to 3.0.22 * [MSHADE-192] - Upgrade maven-invoker to 1.10 * [MSHADE-193] - Upgrade to fluido skin 1.4.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 304a060a6cc..30e8b8fec5d 100644 --- a/pom.xml +++ b/pom.xml @@ -807,7 +807,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.3 + 2.4 From 5bb6f5d6ac452aaabcf1c451b977b67c372d9f2c Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Fri, 12 Jun 2015 14:04:40 +0100 Subject: [PATCH 09/43] [DOCS] Make build from source read correct for new structure The 'Building From Source' section of README.textile was misleading as it said that the release can be found in `target/releases` when they are actually now found in the `target/releases` folder of each module. --- README.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.textile b/README.textile index 720f357406b..63f1841822c 100644 --- a/README.textile +++ b/README.textile @@ -205,7 +205,7 @@ Elasticsearch uses "Maven":http://maven.apache.org for its build system. In order to create a distribution, simply run the @mvn clean package -DskipTests@ command in the cloned directory. -The distribution will be created under @target/releases@. +The distribution for each project will be created under the @target/releases@ directory in that project. See the "TESTING":TESTING.asciidoc file for more information about running the Elasticsearch test suite. From f152614f9215bc6e9702d1bbed7470f349fd4d5d Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Fri, 12 Jun 2015 09:16:23 -0400 Subject: [PATCH 10/43] use System.nanoTime for ThreadPool's estimated time, since it's less likely to go backwards --- .../main/java/org/elasticsearch/threadpool/ThreadPool.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/threadpool/ThreadPool.java b/core/src/main/java/org/elasticsearch/threadpool/ThreadPool.java index 750d670ad7f..f6e359baafb 100644 --- a/core/src/main/java/org/elasticsearch/threadpool/ThreadPool.java +++ b/core/src/main/java/org/elasticsearch/threadpool/ThreadPool.java @@ -564,7 +564,7 @@ public class ThreadPool extends AbstractComponent { EstimatedTimeThread(String name, long interval) { super(name); this.interval = interval; - this.estimatedTimeInMillis = System.currentTimeMillis(); + this.estimatedTimeInMillis = TimeValue.nsecToMSec(System.nanoTime()); this.counter = new TimeCounter(); setDaemon(true); } @@ -576,7 +576,7 @@ public class ThreadPool extends AbstractComponent { @Override public void run() { while (running) { - estimatedTimeInMillis = System.currentTimeMillis(); + estimatedTimeInMillis = TimeValue.nsecToMSec(System.nanoTime()); try { Thread.sleep(interval); } catch (InterruptedException e) { From 7c365daf671d11e03bc9084854a45114090b1a41 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 12 Jun 2015 18:53:35 +0200 Subject: [PATCH 11/43] [build] mark elasticsearch as provided in plugins When we build a plugin, we suppose it will be executed within elasticsearch server. So we should mark it as `provided`. If a java developer needs to embed the plugin and elasticsearch, it will make sense to declare both in its `pom.xml` file. --- plugins/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/pom.xml b/plugins/pom.xml index 7e54c2207be..104c21f3d91 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -39,6 +39,7 @@ org.elasticsearch elasticsearch + provided org.elasticsearch From 2498cf9fab9fcc49438e2f051a79d1f936b9dbdd Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Fri, 12 Jun 2015 11:09:58 -0700 Subject: [PATCH 12/43] Revert "Removing top-level filter parameter from search API." --- .../src/main/java/org/elasticsearch/search/query/QueryPhase.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java index da1b3248e75..00157061803 100644 --- a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -65,6 +65,7 @@ public class QueryPhase implements SearchPhase { .put("query", new QueryParseElement()) .put("queryBinary", new QueryBinaryParseElement()) .put("query_binary", new QueryBinaryParseElement()) + .put("filter", new PostFilterParseElement()) // For bw comp reason, should be removed in version 1.1 .put("post_filter", new PostFilterParseElement()) .put("postFilter", new PostFilterParseElement()) .put("filterBinary", new FilterBinaryParseElement()) From 9b833fdf4e9033dae51b6f5f0372ac909c9edd13 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 12 Jun 2015 20:17:00 +0200 Subject: [PATCH 13/43] Fix CONTRIBUTING.md to include accurate information --- CONTRIBUTING.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1245894c106..32dcb9afac2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -100,8 +100,7 @@ You will find the newly built packages under: `./target/releases/`. Before submitting your changes, run the test suite to make sure that nothing is broken, with: ```sh -ES_TEST_LOCAL=true -mvn clean test +mvn clean test -Dtests.slow=true ``` Source: [Contributing to elasticsearch](http://www.elasticsearch.org/contributing-to-elasticsearch/) From 3a97f322f78d57621f29c5b03c40ed2d485085b1 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Fri, 12 Jun 2015 18:18:26 +0200 Subject: [PATCH 14/43] Simplify ShardRouting and centralize move to unassigned Make sure there is a single place where shard routing move to unassigned, so we can add additional metadata when it does, also, simplify shard routing implementations a bit closes #11634 --- .../routing/ImmutableShardRouting.java | 29 ++++++------------- .../cluster/routing/MutableShardRouting.java | 24 +++++++-------- .../cluster/routing/RoutingNodes.java | 9 +++--- .../routing/allocation/AllocationService.java | 10 ++----- .../command/CancelAllocationCommand.java | 4 +-- .../cluster/ClusterHealthResponsesTests.java | 12 +++----- .../cluster/ClusterStateDiffTests.java | 4 +-- .../allocation/CatAllocationTestBase.java | 2 +- .../decider/DiskThresholdDeciderTests.java | 8 ++--- .../index/shard/IndexShardTests.java | 4 +-- 10 files changed, 42 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/ImmutableShardRouting.java b/core/src/main/java/org/elasticsearch/cluster/routing/ImmutableShardRouting.java index ea3d980bcc6..e177ccd0d73 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/ImmutableShardRouting.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/ImmutableShardRouting.java @@ -60,44 +60,33 @@ public class ImmutableShardRouting implements Streamable, Serializable, ShardRou } public ImmutableShardRouting(ShardRouting copy) { - this(copy.index(), copy.id(), copy.currentNodeId(), copy.primary(), copy.state(), copy.version()); - this.relocatingNodeId = copy.relocatingNodeId(); - this.restoreSource = copy.restoreSource(); - if (copy instanceof ImmutableShardRouting) { - this.shardIdentifier = ((ImmutableShardRouting) copy).shardIdentifier; - } + this(copy, copy.version()); } public ImmutableShardRouting(ShardRouting copy, long version) { - this(copy.index(), copy.id(), copy.currentNodeId(), copy.primary(), copy.state(), copy.version()); - this.relocatingNodeId = copy.relocatingNodeId(); - this.restoreSource = copy.restoreSource(); - this.version = version; - if (copy instanceof ImmutableShardRouting) { - this.shardIdentifier = ((ImmutableShardRouting) copy).shardIdentifier; - } + this(copy.index(), copy.id(), copy.currentNodeId(), copy.relocatingNodeId(), copy.restoreSource(), copy.primary(), copy.state(), version); + } + + public ImmutableShardRouting(String index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state, long version) { + this(index, shardId, currentNodeId, null, primary, state, version); } public ImmutableShardRouting(String index, int shardId, String currentNodeId, String relocatingNodeId, boolean primary, ShardRoutingState state, long version) { - this(index, shardId, currentNodeId, primary, state, version); - this.relocatingNodeId = relocatingNodeId; + this(index, shardId, currentNodeId, relocatingNodeId, null, primary, state, version); } public ImmutableShardRouting(String index, int shardId, String currentNodeId, String relocatingNodeId, RestoreSource restoreSource, boolean primary, ShardRoutingState state, long version) { - this(index, shardId, currentNodeId, relocatingNodeId, primary, state, version); - this.restoreSource = restoreSource; - } - - public ImmutableShardRouting(String index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state, long version) { this.index = index; this.shardId = shardId; this.currentNodeId = currentNodeId; + this.relocatingNodeId = relocatingNodeId; this.primary = primary; this.state = state; this.asList = ImmutableList.of((ShardRouting) this); this.version = version; + this.restoreSource = restoreSource; } @Override diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/MutableShardRouting.java b/core/src/main/java/org/elasticsearch/cluster/routing/MutableShardRouting.java index d5ed083f13f..18d7620697e 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/MutableShardRouting.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/MutableShardRouting.java @@ -32,25 +32,25 @@ public class MutableShardRouting extends ImmutableShardRouting { } public MutableShardRouting(ShardRouting copy, long version) { - super(copy); - this.version = version; - } - - public MutableShardRouting(String index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state, long version) { - super(index, shardId, currentNodeId, primary, state, version); - } - - public MutableShardRouting(String index, int shardId, String currentNodeId, - String relocatingNodeId, boolean primary, ShardRoutingState state, long version) { - super(index, shardId, currentNodeId, relocatingNodeId, null, primary, state, version); + super(copy, version); } public MutableShardRouting(String index, int shardId, String currentNodeId, String relocatingNodeId, RestoreSource restoreSource, boolean primary, ShardRoutingState state, long version) { super(index, shardId, currentNodeId, relocatingNodeId, restoreSource, primary, state, version); + assert state != ShardRoutingState.UNASSIGNED : "new mutable routing should not be created with UNASSIGNED state, should moveToUnassigned"; } - + /** + * Moves the shard to unassigned state. + */ + void moveToUnassigned() { + version++; + assert state != ShardRoutingState.UNASSIGNED; + state = ShardRoutingState.UNASSIGNED; + currentNodeId = null; + relocatingNodeId = null; + } /** * Assign this shard to a node. diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java index 80e674f0346..6052e7fa4ee 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java @@ -109,7 +109,7 @@ public class RoutingNodes implements Iterable { // add the counterpart shard with relocatingNodeId reflecting the source from which // it's relocating from. sr = new MutableShardRouting(shard.index(), shard.id(), shard.relocatingNodeId(), - shard.currentNodeId(), shard.primary(), ShardRoutingState.INITIALIZING, shard.version()); + shard.currentNodeId(), shard.restoreSource(), shard.primary(), ShardRoutingState.INITIALIZING, shard.version()); entries.add(sr); assignedShardsAdd(sr); } else if (!shard.active()) { // shards that are initializing without being relocated @@ -796,9 +796,10 @@ public class RoutingNodes implements Iterable { } public void moveToUnassigned() { - iterator().remove(); - unassigned().add(new MutableShardRouting(shard.index(), shard.id(), - null, shard.primary(), ShardRoutingState.UNASSIGNED, shard.version() + 1)); + remove(); + MutableShardRouting unassigned = new MutableShardRouting(shard); // protective copy of the mutable shard + unassigned.moveToUnassigned(); + unassigned().add(unassigned); } } } diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/allocation/AllocationService.java b/core/src/main/java/org/elasticsearch/cluster/routing/allocation/AllocationService.java index 4b99a48a2ab..30698502de9 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/allocation/AllocationService.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/allocation/AllocationService.java @@ -470,14 +470,11 @@ public class AllocationService extends AbstractComponent { MutableShardRouting shardRouting = relocatingFromNode.next(); if (shardRouting.equals(failedShard)) { dirty = true; - relocatingFromNode.remove(); if (addToIgnoreList) { // make sure we ignore this shard on the relevant node allocation.addIgnoreShardForNode(failedShard.shardId(), failedShard.currentNodeId()); } - - routingNodes.unassigned().add(new MutableShardRouting(failedShard.index(), failedShard.id(), - null, failedShard.primary(), ShardRoutingState.UNASSIGNED, failedShard.version() + 1)); + relocatingFromNode.moveToUnassigned(); break; } } @@ -512,7 +509,6 @@ public class AllocationService extends AbstractComponent { // make sure we ignore this shard on the relevant node allocation.addIgnoreShardForNode(failedShard.shardId(), failedShard.currentNodeId()); } - node.remove(); // move all the shards matching the failed shard to the end of the unassigned list // so we give a chance for other allocations and won't create poison failed allocations // that can keep other shards from being allocated (because of limits applied on how many @@ -529,9 +525,7 @@ public class AllocationService extends AbstractComponent { routingNodes.unassigned().addAll(shardsToMove); } - routingNodes.unassigned().add(new MutableShardRouting(failedShard.index(), failedShard.id(), null, - null, failedShard.restoreSource(), failedShard.primary(), ShardRoutingState.UNASSIGNED, failedShard.version() + 1)); - + node.moveToUnassigned(); break; } } diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/allocation/command/CancelAllocationCommand.java b/core/src/main/java/org/elasticsearch/cluster/routing/allocation/command/CancelAllocationCommand.java index 5d7a93635b4..ecfad2c7923 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/allocation/command/CancelAllocationCommand.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/allocation/command/CancelAllocationCommand.java @@ -222,9 +222,7 @@ public class CancelAllocationCommand implements AllocationCommand { throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " + discoNode + ", shard is primary and started"); } - it.remove(); - allocation.routingNodes().unassigned().add(new MutableShardRouting(shardRouting.index(), shardRouting.id(), - null, shardRouting.primary(), ShardRoutingState.UNASSIGNED, shardRouting.version() + 1)); + it.moveToUnassigned(); } } if (!found) { diff --git a/core/src/test/java/org/elasticsearch/cluster/ClusterHealthResponsesTests.java b/core/src/test/java/org/elasticsearch/cluster/ClusterHealthResponsesTests.java index 76e06b6862d..78477acf3b4 100644 --- a/core/src/test/java/org/elasticsearch/cluster/ClusterHealthResponsesTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/ClusterHealthResponsesTests.java @@ -114,21 +114,17 @@ public class ClusterHealthResponsesTests extends ElasticsearchTestCase { state = ShardRoutingState.STARTED; } else if (i > 3) { state = ShardRoutingState.RELOCATING; - } else if (i > 1) { - state = ShardRoutingState.INITIALIZING; } else { - state = ShardRoutingState.UNASSIGNED; + state = ShardRoutingState.INITIALIZING; } switch (state) { - case UNASSIGNED: - return new MutableShardRouting(index, shardId, null, primary, ShardRoutingState.UNASSIGNED, 1); case STARTED: - return new MutableShardRouting(index, shardId, "node_" + Integer.toString(node_id++), primary, ShardRoutingState.STARTED, 1); + return new MutableShardRouting(index, shardId, "node_" + Integer.toString(node_id++), null, null, primary, ShardRoutingState.STARTED, 1); case INITIALIZING: - return new MutableShardRouting(index, shardId, "node_" + Integer.toString(node_id++), primary, ShardRoutingState.INITIALIZING, 1); + return new MutableShardRouting(index, shardId, "node_" + Integer.toString(node_id++), null, null, primary, ShardRoutingState.INITIALIZING, 1); case RELOCATING: - return new MutableShardRouting(index, shardId, "node_" + Integer.toString(node_id++), "node_" + Integer.toString(node_id++), primary, ShardRoutingState.RELOCATING, 1); + return new MutableShardRouting(index, shardId, "node_" + Integer.toString(node_id++), "node_" + Integer.toString(node_id++), null, primary, ShardRoutingState.RELOCATING, 1); default: throw new ElasticsearchException("Unknown state: " + state.name()); } diff --git a/core/src/test/java/org/elasticsearch/cluster/ClusterStateDiffTests.java b/core/src/test/java/org/elasticsearch/cluster/ClusterStateDiffTests.java index 133daed877a..a054896f721 100644 --- a/core/src/test/java/org/elasticsearch/cluster/ClusterStateDiffTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/ClusterStateDiffTests.java @@ -222,7 +222,7 @@ public class ClusterStateDiffTests extends ElasticsearchIntegrationTest { int replicaCount = randomIntBetween(1, 10); for (int j = 0; j < replicaCount; j++) { indexShard.addShard( - new MutableShardRouting(index, i, randomFrom(nodeIds), j == 0, ShardRoutingState.fromValue((byte) randomIntBetween(1, 4)), 1)); + new MutableShardRouting(index, i, randomFrom(nodeIds), null, null, j == 0, ShardRoutingState.fromValue((byte) randomIntBetween(2, 4)), 1)); } builder.addIndexShard(indexShard.build()); } @@ -663,4 +663,4 @@ public class ClusterStateDiffTests extends ElasticsearchIntegrationTest { private String randomName(String prefix) { return prefix + Strings.randomBase64UUID(getRandom()); } -} \ No newline at end of file +} diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestBase.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestBase.java index 2ab363abd87..8d48cd8ec53 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestBase.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestBase.java @@ -77,7 +77,7 @@ public abstract class CatAllocationTestBase extends ElasticsearchAllocationTestC ShardRoutingState state = ShardRoutingState.valueOf(matcher.group(4)); String ip = matcher.group(5); nodes.add(ip); - MutableShardRouting routing = new MutableShardRouting(index, shard, ip, primary, state, 1); + MutableShardRouting routing = new MutableShardRouting(index, shard, ip, null, null, primary, state, 1); idx.add(routing); logger.debug("Add routing {}", routing); } else { diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java index 707d54ee517..7ec9c5e8b16 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java @@ -829,8 +829,8 @@ public class DiskThresholdDeciderTests extends ElasticsearchAllocationTestCase { .build(); // Two shards consuming each 80% of disk space while 70% is allowed, so shard 0 isn't allowed here - MutableShardRouting firstRouting = new MutableShardRouting("test", 0, "node1", true, ShardRoutingState.STARTED, 1); - MutableShardRouting secondRouting = new MutableShardRouting("test", 1, "node1", true, ShardRoutingState.STARTED, 1); + MutableShardRouting firstRouting = new MutableShardRouting("test", 0, "node1", null, null, true, ShardRoutingState.STARTED, 1); + MutableShardRouting secondRouting = new MutableShardRouting("test", 1, "node1", null, null, true, ShardRoutingState.STARTED, 1); RoutingNode firstRoutingNode = new RoutingNode("node1", discoveryNode1, Arrays.asList(firstRouting, secondRouting)); RoutingTable.Builder builder = RoutingTable.builder().add( IndexRoutingTable.builder("test") @@ -849,8 +849,8 @@ public class DiskThresholdDeciderTests extends ElasticsearchAllocationTestCase { assertThat(decision.type(), equalTo(Decision.Type.NO)); // Two shards consuming each 80% of disk space while 70% is allowed, but one is relocating, so shard 0 can stay - firstRouting = new MutableShardRouting("test", 0, "node1", true, ShardRoutingState.STARTED, 1); - secondRouting = new MutableShardRouting("test", 1, "node1", "node2", true, ShardRoutingState.RELOCATING, 1); + firstRouting = new MutableShardRouting("test", 0, "node1", null, null, true, ShardRoutingState.STARTED, 1); + secondRouting = new MutableShardRouting("test", 1, "node1", "node2", null, true, ShardRoutingState.RELOCATING, 1); firstRoutingNode = new RoutingNode("node1", discoveryNode1, Arrays.asList(firstRouting, secondRouting)); builder = RoutingTable.builder().add( IndexRoutingTable.builder("test") diff --git a/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java b/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java index 45fed8a5941..2a109849c23 100644 --- a/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java +++ b/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java @@ -159,7 +159,7 @@ public class IndexShardTests extends ElasticsearchSingleNodeTest { assertEquals(shardStateMetaData, new ShardStateMetaData(routing.version(), routing.primary(), shard.indexSettings.get(IndexMetaData.SETTING_UUID))); // test if we still write it even if the shard is not active - MutableShardRouting inactiveRouting = new MutableShardRouting(shard.shardRouting.index(), shard.shardRouting.shardId().id(), shard.shardRouting.currentNodeId(), true, ShardRoutingState.INITIALIZING, shard.shardRouting.version() + 1); + MutableShardRouting inactiveRouting = new MutableShardRouting(shard.shardRouting.index(), shard.shardRouting.shardId().id(), shard.shardRouting.currentNodeId(), null, null, true, ShardRoutingState.INITIALIZING, shard.shardRouting.version() + 1); shard.persistMetadata(inactiveRouting, shard.shardRouting); shardStateMetaData = load(logger, env.availableShardPaths(shard.shardId)); assertEquals("inactive shard state shouldn't be persisted", shardStateMetaData, getShardStateMetadata(shard)); @@ -197,7 +197,7 @@ public class IndexShardTests extends ElasticsearchSingleNodeTest { ShardStateMetaData shardStateMetaData = load(logger, env.availableShardPaths(shard.shardId)); assertEquals(shardStateMetaData, getShardStateMetadata(shard)); - routing = new MutableShardRouting(shard.shardId.index().getName(), shard.shardId.id(), routing.currentNodeId(), routing.primary(), ShardRoutingState.INITIALIZING, shard.shardRouting.version() + 1); + routing = new MutableShardRouting(shard.shardId.index().getName(), shard.shardId.id(), routing.currentNodeId(), null, null, routing.primary(), ShardRoutingState.INITIALIZING, shard.shardRouting.version() + 1); shard.updateRoutingEntry(routing, true); shard.deleteShardState(); From e34cb18f8a0332eadd6df65a80b4c8c7b01e06a0 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Fri, 12 Jun 2015 17:09:26 -0400 Subject: [PATCH 15/43] Core: reduce the size of the XContent parsing exception The content that caused the exception can be potentially very big and in most cases it's not very useful for debugging. --- .../org/elasticsearch/common/xcontent/XContentFactory.java | 6 +++--- .../org/elasticsearch/percolator/MultiPercolatorTests.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java index 75e57509948..c9e38f1d94e 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java @@ -177,7 +177,7 @@ public class XContentFactory { public static XContent xContent(CharSequence content) { XContentType type = xContentType(content); if (type == null) { - throw new ElasticsearchParseException("Failed to derive xcontent from " + content); + throw new ElasticsearchParseException("Failed to derive xcontent"); } return xContent(type); } @@ -195,7 +195,7 @@ public class XContentFactory { public static XContent xContent(byte[] data, int offset, int length) { XContentType type = xContentType(data, offset, length); if (type == null) { - throw new ElasticsearchParseException("Failed to derive xcontent from (offset=" + offset + ", length=" + length + "): " + Arrays.toString(data)); + throw new ElasticsearchParseException("Failed to derive xcontent"); } return xContent(type); } @@ -290,7 +290,7 @@ public class XContentFactory { public static XContent xContent(BytesReference bytes) { XContentType type = xContentType(bytes); if (type == null) { - throw new ElasticsearchParseException("Failed to derive xcontent from " + bytes); + throw new ElasticsearchParseException("Failed to derive xcontent"); } return xContent(type); } diff --git a/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorTests.java b/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorTests.java index 46982cb500e..b04ad411f7c 100644 --- a/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorTests.java +++ b/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorTests.java @@ -311,7 +311,7 @@ public class MultiPercolatorTests extends ElasticsearchIntegrationTest { assertThat(item.getResponse().getSuccessfulShards(), equalTo(0)); assertThat(item.getResponse().getShardFailures().length, equalTo(test.numPrimaries)); for (ShardOperationFailedException shardFailure : item.getResponse().getShardFailures()) { - assertThat(shardFailure.reason(), containsString("Failed to derive xcontent from")); + assertThat(shardFailure.reason(), containsString("Failed to derive xcontent")); assertThat(shardFailure.status().getStatus(), equalTo(500)); } } From 0ef5a27556670645c54f93b1dd37e895ea3b7c38 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 10 Jun 2015 09:38:44 -0700 Subject: [PATCH 16/43] Mappings: Add equals/hashcode to fieldtypes In order to restrict a single set of field type settings for a given field name across an index, we need the ability to compare field types. This change adds equals and hashcode, as well as tests for every field type. --- .../index/mapper/MappedFieldType.java | 27 +- .../index/mapper/core/BinaryFieldMapper.java | 28 +- .../index/mapper/core/DateFieldMapper.java | 16 + .../index/mapper/geo/GeoPointFieldMapper.java | 22 ++ .../index/mapper/geo/GeoShapeFieldMapper.java | 298 ++++++++++-------- .../internal/FieldNamesFieldMapper.java | 17 +- .../mapper/internal/ParentFieldMapper.java | 2 - .../index/mapper/FieldTypeTestCase.java | 126 ++++++++ .../index/mapper/MappedFieldTypeTests.java | 27 ++ .../mapper/core/BinaryFieldTypeTests.java | 44 +++ .../mapper/core/BooleanFieldTypeTests.java | 34 ++ .../index/mapper/core/ByteFieldTypeTests.java | 34 ++ .../mapper/core/CompletionFieldTypeTests.java | 29 ++ .../index/mapper/core/DateFieldTypeTests.java | 53 ++++ .../mapper/core/DoubleFieldTypeTests.java | 34 ++ .../mapper/core/FloatFieldTypeTests.java | 34 ++ .../mapper/core/IntegerFieldTypeTests.java | 34 ++ .../index/mapper/core/LongFieldTypeTests.java | 34 ++ .../mapper/core/ShortFieldTypeTests.java | 34 ++ .../mapper/core/StringFieldTypeTests.java | 29 ++ .../mapper/geo/GeoPointFieldTypeTests.java | 48 +++ .../mapper/geo/GeoShapeFieldMapperTests.java | 11 +- .../mapper/geo/GeoShapeFieldTypeTests.java | 51 +++ .../mapper/internal/AllFieldTypeTests.java | 29 ++ .../internal/FieldNamesFieldTypeTests.java | 43 +++ .../mapper/internal/IdFieldTypeTests.java | 29 ++ .../mapper/internal/IndexFieldTypeTests.java | 29 ++ .../mapper/internal/ParentFieldTypeTests.java | 29 ++ .../internal/RoutingFieldTypeTests.java | 29 ++ .../mapper/internal/SourceFieldTypeTests.java | 29 ++ .../internal/TimestampFieldTypeTests.java | 29 ++ .../mapper/internal/TypeFieldTypeTests.java | 29 ++ .../mapper/internal/UidFieldTypeTests.java | 29 ++ .../internal/VersionFieldTypeTests.java | 29 ++ .../string/SimpleStringMappingTests.java | 2 +- 35 files changed, 1256 insertions(+), 145 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/MappedFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/ByteFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/CompletionFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/ShortFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/core/StringFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/AllFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/IdFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/IndexFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/ParentFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/RoutingFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/SourceFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/TimestampFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/TypeFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/UidFieldTypeTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/mapper/internal/VersionFieldTypeTests.java diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index d9b875c2829..6f2cab8ede4 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -46,6 +46,7 @@ import org.elasticsearch.index.similarity.SimilarityProvider; import java.io.IOException; import java.util.List; +import java.util.Objects; /** * This defines the core properties and functions to operate on a field. @@ -200,7 +201,31 @@ public class MappedFieldType extends FieldType { return new MappedFieldType(this); } - // norelease: we need to override freeze() and add safety checks that all settings are actually set + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MappedFieldType)) return false; + if (!super.equals(o)) return false; + MappedFieldType fieldType = (MappedFieldType) o; + return Objects.equals(boost, fieldType.boost) && + Objects.equals(docValues, fieldType.docValues) && + Objects.equals(names, fieldType.names) && + Objects.equals(indexAnalyzer, fieldType.indexAnalyzer) && + Objects.equals(searchAnalyzer, fieldType.searchAnalyzer) && + Objects.equals(searchQuoteAnalyzer(), fieldType.searchQuoteAnalyzer()) && + Objects.equals(similarity, fieldType.similarity) && + Objects.equals(normsLoading, fieldType.normsLoading) && + Objects.equals(fieldDataType, fieldType.fieldDataType) && + Objects.equals(nullValue, fieldType.nullValue) && + Objects.equals(nullValueAsString, fieldType.nullValueAsString); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), names, boost, docValues, indexAnalyzer, searchAnalyzer, searchQuoteAnalyzer, similarity, normsLoading, fieldDataType, nullValue, nullValueAsString); + } + +// norelease: we need to override freeze() and add safety checks that all settings are actually set public boolean isNumeric() { return false; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java index 20bcb15ad53..02946df4446 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java @@ -49,6 +49,7 @@ import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import static org.elasticsearch.index.mapper.MapperBuilders.binaryField; import static org.elasticsearch.index.mapper.core.TypeParsers.parseField; @@ -82,7 +83,7 @@ public class BinaryFieldMapper extends AbstractFieldMapper { @Override public BinaryFieldMapper build(BuilderContext context) { setupFieldType(context); - ((BinaryFieldType)fieldType).tryUncompressing = context.indexCreatedVersion().before(Version.V_2_0_0); + ((BinaryFieldType)fieldType).setTryUncompressing(context.indexCreatedVersion().before(Version.V_2_0_0)); return new BinaryFieldMapper(fieldType, docValues, fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @@ -106,7 +107,7 @@ public class BinaryFieldMapper extends AbstractFieldMapper { } static final class BinaryFieldType extends MappedFieldType { - protected boolean tryUncompressing = false; + private boolean tryUncompressing = false; public BinaryFieldType() { super(AbstractFieldMapper.Defaults.FIELD_TYPE); @@ -122,6 +123,29 @@ public class BinaryFieldMapper extends AbstractFieldMapper { return new BinaryFieldType(this); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof BinaryFieldType)) return false; + if (!super.equals(o)) return false; + BinaryFieldType that = (BinaryFieldType) o; + return Objects.equals(tryUncompressing, that.tryUncompressing); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), tryUncompressing); + } + + public boolean tryUncompressing() { + return tryUncompressing; + } + + public void setTryUncompressing(boolean tryUncompressing) { + checkIfFrozen(); + this.tryUncompressing = tryUncompressing; + } + @Override public BytesReference value(Object value) { if (value == null) { diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index a8ba8a2bf0b..62c77ee2f87 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -57,6 +57,7 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -234,6 +235,21 @@ public class DateFieldMapper extends NumberFieldMapper { return new DateFieldType(this); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof DateFieldType)) return false; + if (!super.equals(o)) return false; + DateFieldType that = (DateFieldType) o; + return Objects.equals(dateTimeFormatter.format(), that.dateTimeFormatter.format()) && + Objects.equals(timeUnit, that.timeUnit); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), dateTimeFormatter, timeUnit); + } + public FormatDateTimeFormatter dateTimeFormatter() { return dateTimeFormatter; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java index bb3ada8278d..9ce4dba8270 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java @@ -312,6 +312,28 @@ public class GeoPointFieldMapper extends AbstractFieldMapper implements ArrayVal return new GeoPointFieldType(this); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof GeoPointFieldType)) return false; + if (!super.equals(o)) return false; + GeoPointFieldType that = (GeoPointFieldType) o; + return java.util.Objects.equals(geohashPrecision, that.geohashPrecision) && + java.util.Objects.equals(geohashPrefixEnabled, that.geohashPrefixEnabled) && + java.util.Objects.equals(validateLon, that.validateLon) && + java.util.Objects.equals(validateLat, that.validateLat) && + java.util.Objects.equals(normalizeLon, that.normalizeLon) && + java.util.Objects.equals(normalizeLat, that.normalizeLat) && + java.util.Objects.equals(geohashFieldType, that.geohashFieldType) && + java.util.Objects.equals(latFieldType, that.latFieldType) && + java.util.Objects.equals(lonFieldType, that.lonFieldType); + } + + @Override + public int hashCode() { + return java.util.Objects.hash(super.hashCode(), geohashFieldType, geohashPrecision, geohashPrefixEnabled, latFieldType, lonFieldType, validateLon, validateLat, normalizeLon, normalizeLat); + } + public boolean isGeohashEnabled() { return geohashFieldType != null; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java index 817b09bd071..aafda6d3cea 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java @@ -51,6 +51,7 @@ import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import static org.elasticsearch.index.mapper.MapperBuilders.geoShapeField; @@ -59,7 +60,7 @@ import static org.elasticsearch.index.mapper.MapperBuilders.geoShapeField; * FieldMapper for indexing {@link com.spatial4j.core.shape.Shape}s. *

* Currently Shapes can only be indexed and can only be queried using - * {@link org.elasticsearch.index.query.GeoShapeFilterParser}, consequently + * {@link org.elasticsearch.index.query.GeoShapeQueryParser}, consequently * a lot of behavior in this Mapper is disabled. *

* Format supported: @@ -91,12 +92,15 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { public static final String STRATEGY = SpatialStrategy.RECURSIVE.getStrategyName(); public static final int GEOHASH_LEVELS = GeoUtils.geoHashLevelsForPrecision("50m"); public static final int QUADTREE_LEVELS = GeoUtils.quadTreeLevelsForPrecision("50m"); - public static final double DISTANCE_ERROR_PCT = 0.025d; + public static final double LEGACY_DISTANCE_ERROR_PCT = 0.025d; public static final Orientation ORIENTATION = Orientation.RIGHT; public static final MappedFieldType FIELD_TYPE = new GeoShapeFieldType(); static { + // setting name here is a hack so freeze can be called...instead all these options should be + // moved to the default ctor for GeoShapeFieldType, and defaultFieldType() should be removed from mappers... + FIELD_TYPE.setNames(new MappedFieldType.Names("DoesNotExist")); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(false); @@ -108,91 +112,30 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { public static class Builder extends AbstractFieldMapper.Builder { - private String tree = Defaults.TREE; - private String strategyName = Defaults.STRATEGY; - private int treeLevels = 0; - private double precisionInMeters = -1; - private double distanceErrorPct = Defaults.DISTANCE_ERROR_PCT; - private boolean distErrPctDefined; - private Orientation orientation = Defaults.ORIENTATION; - - private SpatialPrefixTree prefixTree; - public Builder(String name) { super(name, Defaults.FIELD_TYPE); } - public Builder tree(String tree) { - this.tree = tree; - return this; - } - - public Builder strategy(String strategy) { - this.strategyName = strategy; - return this; - } - - public Builder treeLevelsByDistance(double meters) { - this.precisionInMeters = meters; - return this; - } - - public Builder treeLevels(int treeLevels) { - this.treeLevels = treeLevels; - return this; - } - - public Builder distanceErrorPct(double distanceErrorPct) { - this.distanceErrorPct = distanceErrorPct; - return this; - } - - public Builder orientation(Orientation orientation) { - this.orientation = orientation; - return this; + public GeoShapeFieldType fieldType() { + return (GeoShapeFieldType)fieldType; } @Override public GeoShapeFieldMapper build(BuilderContext context) { + GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType)fieldType; - if (Names.TREE_GEOHASH.equals(tree)) { - prefixTree = new GeohashPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults.GEOHASH_LEVELS, true)); - } else if (Names.TREE_QUADTREE.equals(tree)) { - if (context.indexCreatedVersion().before(Version.V_1_6_0)) { - prefixTree = new QuadPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults - .QUADTREE_LEVELS, false)); - } else { - prefixTree = new PackedQuadPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults - .QUADTREE_LEVELS, false)); - } - } else { - throw new IllegalArgumentException("Unknown prefix tree type [" + tree + "]"); + if (geoShapeFieldType.tree.equals("quadtree") && context.indexCreatedVersion().before(Version.V_2_0_0)) { + geoShapeFieldType.setTree("legacyquadtree"); + } + + if (context.indexCreatedVersion().before(Version.V_2_0_0) || + (geoShapeFieldType.treeLevels() == 0 && geoShapeFieldType.precisionInMeters() < 0)) { + geoShapeFieldType.setDefaultDistanceErrorPct(Defaults.LEGACY_DISTANCE_ERROR_PCT); } setupFieldType(context); - RecursivePrefixTreeStrategy recursiveStrategy = new RecursivePrefixTreeStrategy(prefixTree, fieldType.names().indexName()); - recursiveStrategy.setDistErrPct(distanceErrorPct); - recursiveStrategy.setPruneLeafyBranches(false); - TermQueryPrefixTreeStrategy termStrategy = new TermQueryPrefixTreeStrategy(prefixTree, fieldType.names().indexName()); - termStrategy.setDistErrPct(distanceErrorPct); - - GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType)fieldType; - geoShapeFieldType.setStrategies(strategyName, recursiveStrategy, termStrategy); - geoShapeFieldType.setOrientation(orientation); - return new GeoShapeFieldMapper(fieldType, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } - - private final int getLevels(int treeLevels, double precisionInMeters, int defaultLevels, boolean geoHash) { - if (treeLevels > 0 || precisionInMeters >= 0) { - // if the user specified a precision but not a distance error percent then zero out the distance err pct - // this is done to guarantee precision specified by the user without doing something unexpected under the covers - if (!distErrPctDefined) distanceErrorPct = 0; - return Math.max(treeLevels, precisionInMeters >= 0 ? (geoHash ? GeoUtils.geoHashLevelsForPrecision(precisionInMeters) - : GeoUtils.quadTreeLevelsForPrecision(precisionInMeters)) : 0); - } - return defaultLevels; - } } public static class TypeParser implements Mapper.TypeParser { @@ -200,31 +143,27 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { @Override public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { Builder builder = geoShapeField(name); - // if index was created before 1.6, this conditional should be true (this forces any index created on/or after 1.6 to use 0 for - // the default distanceErrorPct parameter). - builder.distErrPctDefined = parserContext.indexVersionCreated().before(Version.V_1_6_0); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); String fieldName = Strings.toUnderscoreCase(entry.getKey()); Object fieldNode = entry.getValue(); if (Names.TREE.equals(fieldName)) { - builder.tree(fieldNode.toString()); + builder.fieldType().setTree(fieldNode.toString()); iterator.remove(); } else if (Names.TREE_LEVELS.equals(fieldName)) { - builder.treeLevels(Integer.parseInt(fieldNode.toString())); + builder.fieldType().setTreeLevels(Integer.parseInt(fieldNode.toString())); iterator.remove(); } else if (Names.TREE_PRESISION.equals(fieldName)) { - builder.treeLevelsByDistance(DistanceUnit.parse(fieldNode.toString(), DistanceUnit.DEFAULT, DistanceUnit.DEFAULT)); + builder.fieldType().setPrecisionInMeters(DistanceUnit.parse(fieldNode.toString(), DistanceUnit.DEFAULT, DistanceUnit.DEFAULT)); iterator.remove(); } else if (Names.DISTANCE_ERROR_PCT.equals(fieldName)) { - builder.distanceErrorPct(Double.parseDouble(fieldNode.toString())); - builder.distErrPctDefined = true; + builder.fieldType().setDistanceErrorPct(Double.parseDouble(fieldNode.toString())); iterator.remove(); } else if (Names.ORIENTATION.equals(fieldName)) { - builder.orientation(ShapeBuilder.orientationFromString(fieldNode.toString())); + builder.fieldType().setOrientation(ShapeBuilder.orientationFromString(fieldNode.toString())); iterator.remove(); } else if (Names.STRATEGY.equals(fieldName)) { - builder.strategy(fieldNode.toString()); + builder.fieldType().setStrategyName(fieldNode.toString()); iterator.remove(); } } @@ -234,10 +173,18 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { public static final class GeoShapeFieldType extends MappedFieldType { + private String tree = Defaults.TREE; + private String strategyName = Defaults.STRATEGY; + private int treeLevels = 0; + private double precisionInMeters = -1; + private Double distanceErrorPct; + private double defaultDistanceErrorPct = 0.0; + private Orientation orientation = Defaults.ORIENTATION; + + // these are built when the field type is frozen private PrefixTreeStrategy defaultStrategy; private RecursivePrefixTreeStrategy recursiveStrategy; private TermQueryPrefixTreeStrategy termStrategy; - private Orientation orientation; public GeoShapeFieldType() { super(AbstractFieldMapper.Defaults.FIELD_TYPE); @@ -245,10 +192,12 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { protected GeoShapeFieldType(GeoShapeFieldType ref) { super(ref); - // TODO: this shallow copy is probably not good...need to extract the parameters and recreate the tree and strategies? - this.defaultStrategy = ref.defaultStrategy; - this.recursiveStrategy = ref.recursiveStrategy; - this.termStrategy = ref.termStrategy; + this.tree = ref.tree; + this.strategyName = ref.strategyName; + this.treeLevels = ref.treeLevels; + this.precisionInMeters = ref.precisionInMeters; + this.distanceErrorPct = ref.distanceErrorPct; + this.defaultDistanceErrorPct = ref.defaultDistanceErrorPct; this.orientation = ref.orientation; } @@ -257,6 +206,113 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { return new GeoShapeFieldType(this); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof GeoShapeFieldType)) return false; + if (!super.equals(o)) return false; + GeoShapeFieldType that = (GeoShapeFieldType) o; + return Objects.equals(treeLevels, that.treeLevels) && + Objects.equals(precisionInMeters, that.precisionInMeters) && + Objects.equals(defaultDistanceErrorPct, that.defaultDistanceErrorPct) && + Objects.equals(tree, that.tree) && + Objects.equals(strategyName, that.strategyName) && + Objects.equals(distanceErrorPct, that.distanceErrorPct) && + Objects.equals(orientation, that.orientation); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), tree, strategyName, treeLevels, precisionInMeters, distanceErrorPct, defaultDistanceErrorPct, orientation); + } + + @Override + public void freeze() { + super.freeze(); + SpatialPrefixTree prefixTree; + if ("geohash".equals(tree)) { + prefixTree = new GeohashPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults.GEOHASH_LEVELS, true)); + } else if ("legacyquadtree".equals(tree)) { + prefixTree = new QuadPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults.QUADTREE_LEVELS, false)); + } else if ("quadtree".equals(tree)) { + prefixTree = new PackedQuadPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults.QUADTREE_LEVELS, false)); + } else { + throw new IllegalArgumentException("Unknown prefix tree type [" + tree + "]"); + } + + recursiveStrategy = new RecursivePrefixTreeStrategy(prefixTree, names().indexName()); + recursiveStrategy.setDistErrPct(distanceErrorPct()); + recursiveStrategy.setPruneLeafyBranches(false); + termStrategy = new TermQueryPrefixTreeStrategy(prefixTree, names().indexName()); + termStrategy.setDistErrPct(distanceErrorPct()); + defaultStrategy = resolveStrategy(strategyName); + } + + private static int getLevels(int treeLevels, double precisionInMeters, int defaultLevels, boolean geoHash) { + if (treeLevels > 0 || precisionInMeters >= 0) { + return Math.max(treeLevels, precisionInMeters >= 0 ? (geoHash ? GeoUtils.geoHashLevelsForPrecision(precisionInMeters) + : GeoUtils.quadTreeLevelsForPrecision(precisionInMeters)) : 0); + } + return defaultLevels; + } + + public String tree() { + return tree; + } + + public void setTree(String tree) { + checkIfFrozen(); + this.tree = tree; + } + + public String strategyName() { + return strategyName; + } + + public void setStrategyName(String strategyName) { + checkIfFrozen(); + this.strategyName = strategyName; + } + + public int treeLevels() { + return treeLevels; + } + + public void setTreeLevels(int treeLevels) { + checkIfFrozen(); + this.treeLevels = treeLevels; + } + + public double precisionInMeters() { + return precisionInMeters; + } + + public void setPrecisionInMeters(double precisionInMeters) { + checkIfFrozen(); + this.precisionInMeters = precisionInMeters; + } + + public double distanceErrorPct() { + return distanceErrorPct == null ? defaultDistanceErrorPct : distanceErrorPct; + } + + public void setDistanceErrorPct(double distanceErrorPct) { + checkIfFrozen(); + this.distanceErrorPct = distanceErrorPct; + } + + public void setDefaultDistanceErrorPct(double defaultDistanceErrorPct) { + checkIfFrozen(); + this.defaultDistanceErrorPct = defaultDistanceErrorPct; + } + + public Orientation orientation() { return this.orientation; } + + public void setOrientation(Orientation orientation) { + checkIfFrozen(); + this.orientation = orientation; + } + public PrefixTreeStrategy defaultStrategy() { return this.defaultStrategy; } @@ -271,26 +327,6 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { throw new IllegalArgumentException("Unknown prefix tree strategy [" + strategyName + "]"); } - public void setStrategies(String defaultStrategy, RecursivePrefixTreeStrategy recursiveStrategy, TermQueryPrefixTreeStrategy termStrategy) { - checkIfFrozen(); - this.recursiveStrategy = recursiveStrategy; - this.termStrategy = termStrategy; - this.defaultStrategy = resolveStrategy(defaultStrategy); - } - - public void setDistErrPct(double distErrPct) { - checkIfFrozen(); - this.recursiveStrategy.setDistErrPct(distErrPct); - this.termStrategy.setDistErrPct(distErrPct); - } - - public Orientation orientation() { return this.orientation; } - - public void setOrientation(Orientation orientation) { - checkIfFrozen(); - this.orientation = orientation; - } - @Override public String value(Object value) { throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values"); @@ -352,25 +388,24 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { return; } final GeoShapeFieldMapper fieldMergeWith = (GeoShapeFieldMapper) mergeWith; - final PrefixTreeStrategy mergeWithStrategy = fieldMergeWith.fieldType().defaultStrategy(); // prevent user from changing strategies - if (!(this.fieldType().defaultStrategy().getClass().equals(mergeWithStrategy.getClass()))) { + if (fieldType().strategyName().equals(fieldMergeWith.fieldType().strategyName()) == false) { mergeResult.addConflict("mapper [" + fieldType.names().fullName() + "] has different strategy"); } - final SpatialPrefixTree grid = this.fieldType().defaultStrategy().getGrid(); - final SpatialPrefixTree mergeGrid = mergeWithStrategy.getGrid(); - // prevent user from changing trees (changes encoding) - if (!grid.getClass().equals(mergeGrid.getClass())) { + if (fieldType().tree().equals(fieldMergeWith.fieldType().tree()) == false) { mergeResult.addConflict("mapper [" + fieldType.names().fullName() + "] has different tree"); } // TODO we should allow this, but at the moment levels is used to build bookkeeping variables // in lucene's SpatialPrefixTree implementations, need a patch to correct that first - if (grid.getMaxLevels() != mergeGrid.getMaxLevels()) { - mergeResult.addConflict("mapper [" + fieldType.names().fullName() + "] has different tree_levels or precision"); + if (fieldType().treeLevels() != fieldMergeWith.fieldType().treeLevels()) { + mergeResult.addConflict("mapper [" + fieldType.names().fullName() + "] has different tree_levels"); + } + if (fieldType().precisionInMeters() != fieldMergeWith.fieldType().precisionInMeters()) { + mergeResult.addConflict("mapper [" + fieldType.names().fullName() + "] has different precision"); } // bail if there were merge conflicts @@ -380,7 +415,7 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { // change distance error percent this.fieldType = this.fieldType.clone(); - this.fieldType().setDistErrPct(mergeWithStrategy.getDistErrPct()); + this.fieldType().setDistanceErrorPct(fieldMergeWith.fieldType().distanceErrorPct()); // change orientation - this is allowed because existing dateline spanning shapes // have already been unwound and segmented this.fieldType().setOrientation(fieldMergeWith.fieldType().orientation()); @@ -395,24 +430,21 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); - // TODO: Come up with a better way to get the name, maybe pass it from builder - if (fieldType().defaultStrategy().getGrid() instanceof GeohashPrefixTree) { - // Don't emit the tree name since GeohashPrefixTree is the default - // Only emit the tree levels if it isn't the default value - if (includeDefaults || fieldType().defaultStrategy().getGrid().getMaxLevels() != Defaults.GEOHASH_LEVELS) { - builder.field(Names.TREE_LEVELS, fieldType().defaultStrategy().getGrid().getMaxLevels()); - } - } else { - builder.field(Names.TREE, Names.TREE_QUADTREE); - if (includeDefaults || fieldType().defaultStrategy().getGrid().getMaxLevels() != Defaults.QUADTREE_LEVELS) { - builder.field(Names.TREE_LEVELS, fieldType().defaultStrategy().getGrid().getMaxLevels()); - } + if (includeDefaults || fieldType().tree().equals(Defaults.TREE) == false) { + builder.field(Names.TREE, fieldType().tree()); } - - if (includeDefaults || fieldType().defaultStrategy().getDistErrPct() != Defaults.DISTANCE_ERROR_PCT) { - builder.field(Names.DISTANCE_ERROR_PCT, fieldType().defaultStrategy().getDistErrPct()); + if (includeDefaults || fieldType().treeLevels() != 0) { + builder.field(Names.TREE_LEVELS, fieldType().treeLevels()); + } + if (includeDefaults || fieldType().precisionInMeters() != -1) { + builder.field(Names.TREE_PRESISION, DistanceUnit.METERS.toString(fieldType().precisionInMeters())); + } + if (includeDefaults || fieldType().strategyName() != Defaults.STRATEGY) { + builder.field(Names.STRATEGY, fieldType().strategyName()); + } + if (includeDefaults || fieldType().distanceErrorPct() != fieldType().defaultDistanceErrorPct) { + builder.field(Names.DISTANCE_ERROR_PCT, fieldType().distanceErrorPct()); } - if (includeDefaults || fieldType().orientation() != Defaults.ORIENTATION) { builder.field(Names.ORIENTATION, fieldType().orientation()); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java index 967ee1ad687..09ecbbdc4c9 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java @@ -46,6 +46,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; import static org.elasticsearch.index.mapper.MapperBuilders.fieldNames; @@ -148,6 +149,20 @@ public class FieldNamesFieldMapper extends AbstractFieldMapper implements RootMa this.enabled = ref.enabled; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof FieldNamesFieldType)) return false; + if (!super.equals(o)) return false; + FieldNamesFieldType that = (FieldNamesFieldType) o; + return Objects.equals(enabled, that.enabled); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), enabled); + } + public void setEnabled(boolean enabled) { checkIfFrozen(); this.enabled = enabled; @@ -288,7 +303,7 @@ public class FieldNamesFieldMapper extends AbstractFieldMapper implements RootMa } boolean includeDefaults = params.paramAsBoolean("include_defaults", false); - if (includeDefaults == false && fieldType().equals(Defaults.FIELD_TYPE) && fieldType().isEnabled() == Defaults.ENABLED) { + if (includeDefaults == false && fieldType().isEnabled() == Defaults.ENABLED) { return builder; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java index a0cb94bf939..e8a63792d73 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java @@ -229,12 +229,10 @@ public class ParentFieldMapper extends AbstractFieldMapper implements RootMapper } private final String type; - private final BytesRef typeAsBytes; protected ParentFieldMapper(MappedFieldType fieldType, String type, @Nullable Settings fieldDataSettings, Settings indexSettings) { super(fieldType, Version.indexCreated(indexSettings).onOrAfter(Version.V_2_0_0), fieldDataSettings, indexSettings); this.type = type; - this.typeAsBytes = type == null ? null : new BytesRef(type); } public ParentFieldMapper(Settings indexSettings) { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java new file mode 100644 index 00000000000..0dba5b58818 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -0,0 +1,126 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper; + +import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.fielddata.FieldDataType; +import org.elasticsearch.index.similarity.BM25SimilarityProvider; +import org.elasticsearch.test.ElasticsearchTestCase; + +/** Base test case for subclasses of MappedFieldType */ +public abstract class FieldTypeTestCase extends ElasticsearchTestCase { + + /** Create a default constructed fieldtype */ + protected abstract MappedFieldType createDefaultFieldType(); + + /** A dummy null value to use when modifying null value */ + protected Object dummyNullValue() { + return "dummyvalue"; + } + + /** Returns the number of properties that can be modified for the fieldtype */ + protected int numProperties() { + return 10; + } + + /** Modifies a property, identified by propNum, on the given fieldtype */ + protected void modifyProperty(MappedFieldType ft, int propNum) { + switch (propNum) { + case 0: ft.setNames(new MappedFieldType.Names("dummy")); break; + case 1: ft.setBoost(1.1f); break; + case 2: ft.setHasDocValues(!ft.hasDocValues()); break; + case 3: ft.setIndexAnalyzer(Lucene.STANDARD_ANALYZER); break; + case 4: ft.setSearchAnalyzer(Lucene.STANDARD_ANALYZER); break; + case 5: ft.setSearchQuoteAnalyzer(Lucene.STANDARD_ANALYZER); break; + case 6: ft.setSimilarity(new BM25SimilarityProvider("foo", Settings.EMPTY)); break; + case 7: ft.setNormsLoading(MappedFieldType.Loading.LAZY); break; + case 8: ft.setFieldDataType(new FieldDataType("foo", Settings.builder().put("loading", "eager").build())); break; + case 9: ft.setNullValue(dummyNullValue()); break; + default: fail("unknown fieldtype property number " + propNum); + } + } + + // TODO: remove this once toString is no longer final on FieldType... + protected void assertEquals(int i, MappedFieldType ft1, MappedFieldType ft2) { + assertEquals("prop " + i + "\nexpected: " + toString(ft1) + "; \nactual: " + toString(ft2), ft1, ft2); + } + + protected String toString(MappedFieldType ft) { + return "MappedFieldType{" + + "names=" + ft.names() + + ", boost=" + ft.boost() + + ", docValues=" + ft.hasDocValues() + + ", indexAnalyzer=" + ft.indexAnalyzer() + + ", searchAnalyzer=" + ft.searchAnalyzer() + + ", searchQuoteAnalyzer=" + ft.searchQuoteAnalyzer() + + ", similarity=" + ft.similarity() + + ", normsLoading=" + ft.normsLoading() + + ", fieldDataType=" + ft.fieldDataType() + + ", nullValue=" + ft.nullValue() + + ", nullValueAsString='" + ft.nullValueAsString() + "'" + + "} " + super.toString(); + } + + public void testClone() { + MappedFieldType fieldType = createDefaultFieldType(); + MappedFieldType clone = fieldType.clone(); + assertNotSame(clone, fieldType); + assertEquals(clone.getClass(), fieldType.getClass()); + assertEquals(clone, fieldType); + assertEquals(clone, clone.clone()); // transitivity + + for (int i = 0; i < numProperties(); ++i) { + fieldType = createDefaultFieldType(); + modifyProperty(fieldType, i); + clone = fieldType.clone(); + assertNotSame(clone, fieldType); + assertEquals(i, clone, fieldType); + } + } + + public void testEquals() { + MappedFieldType ft1 = createDefaultFieldType(); + MappedFieldType ft2 = createDefaultFieldType(); + assertEquals(ft1, ft1); // reflexive + assertEquals(ft1, ft2); // symmetric + assertEquals(ft2, ft1); + assertEquals(ft1.hashCode(), ft2.hashCode()); + + for (int i = 0; i < numProperties(); ++i) { + ft2 = createDefaultFieldType(); + modifyProperty(ft2, i); + assertNotEquals(ft1, ft2); + assertNotEquals(ft1.hashCode(), ft2.hashCode()); + } + } + + public void testFreeze() { + for (int i = 0; i < numProperties(); ++i) { + MappedFieldType fieldType = createDefaultFieldType(); + fieldType.freeze(); + try { + modifyProperty(fieldType, i); + fail("expected already frozen exception for property " + i); + } catch (IllegalStateException e) { + assertTrue(e.getMessage().contains("already frozen")); + } + } + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/MappedFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/MappedFieldTypeTests.java new file mode 100644 index 00000000000..6cdc809ca23 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/MappedFieldTypeTests.java @@ -0,0 +1,27 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper; + +public class MappedFieldTypeTests extends FieldTypeTestCase { + + @Override + public MappedFieldType createDefaultFieldType() { + return new MappedFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java new file mode 100644 index 00000000000..5633c8467d0 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class BinaryFieldTypeTests extends FieldTypeTestCase { + + @Override + protected MappedFieldType createDefaultFieldType() { + return new BinaryFieldMapper.BinaryFieldType(); + } + + @Override + protected int numProperties() { + return 1 + super.numProperties(); + } + + @Override + protected void modifyProperty(MappedFieldType ft, int propNum) { + BinaryFieldMapper.BinaryFieldType bft = (BinaryFieldMapper.BinaryFieldType)ft; + switch (propNum) { + case 0: bft.setTryUncompressing(!bft.tryUncompressing()); break; + default: super.modifyProperty(ft, numProperties() - propNum - 1); + } + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java new file mode 100644 index 00000000000..3485a383cd6 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class BooleanFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new BooleanFieldMapper.BooleanFieldType(); + } + + @Override + protected Object dummyNullValue() { + return true; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/ByteFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/ByteFieldTypeTests.java new file mode 100644 index 00000000000..33bf21e2710 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/ByteFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class ByteFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new ByteFieldMapper.ByteFieldType(); + } + + @Override + protected Object dummyNullValue() { + return (byte)10; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/CompletionFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/CompletionFieldTypeTests.java new file mode 100644 index 00000000000..b5afc1ae672 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/CompletionFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class CompletionFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new CompletionFieldMapper.CompletionFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java new file mode 100644 index 00000000000..087b57e7c0b --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java @@ -0,0 +1,53 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.common.joda.Joda; +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +import java.util.Locale; +import java.util.concurrent.TimeUnit; + +public class DateFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new DateFieldMapper.DateFieldType(); + } + + @Override + protected Object dummyNullValue() { + return 10; + } + + @Override + protected int numProperties() { + return 2 + super.numProperties(); + } + + @Override + protected void modifyProperty(MappedFieldType ft, int propNum) { + DateFieldMapper.DateFieldType dft = (DateFieldMapper.DateFieldType)ft; + switch (propNum) { + case 0: dft.setDateTimeFormatter(Joda.forPattern("basic_week_date", Locale.ROOT)); break; + case 1: dft.setTimeUnit(TimeUnit.HOURS); break; + default: super.modifyProperty(ft, numProperties() - propNum - 1); + } + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java new file mode 100644 index 00000000000..247f5e2364d --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class DoubleFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new DoubleFieldMapper.DoubleFieldType(); + } + + @Override + protected Object dummyNullValue() { + return 10.0D; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java new file mode 100644 index 00000000000..934f656ae76 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class FloatFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new DoubleFieldMapper.DoubleFieldType(); + } + + @Override + protected Object dummyNullValue() { + return 10.0; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java new file mode 100644 index 00000000000..6e4a49e758b --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class IntegerFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new IntegerFieldMapper.IntegerFieldType(); + } + + @Override + protected Object dummyNullValue() { + return 10; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java new file mode 100644 index 00000000000..671483e8978 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class LongFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new LongFieldMapper.LongFieldType(); + } + + @Override + protected Object dummyNullValue() { + return (long)10; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/ShortFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/ShortFieldTypeTests.java new file mode 100644 index 00000000000..8a10da5cac2 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/ShortFieldTypeTests.java @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class ShortFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new ShortFieldMapper.ShortFieldType(); + } + + @Override + protected Object dummyNullValue() { + return (short)10; + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/StringFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/StringFieldTypeTests.java new file mode 100644 index 00000000000..1280fd4acca --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/StringFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.core; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class StringFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new StringFieldMapper.StringFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java new file mode 100644 index 00000000000..7830d9c0b61 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java @@ -0,0 +1,48 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.geo; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class GeoPointFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new GeoPointFieldMapper.GeoPointFieldType(); + } + + @Override + protected int numProperties() { + return 6 + super.numProperties(); + } + + @Override + protected void modifyProperty(MappedFieldType ft, int propNum) { + GeoPointFieldMapper.GeoPointFieldType gft = (GeoPointFieldMapper.GeoPointFieldType)ft; + switch (propNum) { + case 0: gft.setGeohashEnabled(new MappedFieldType(), 1, true); break; + case 1: gft.setLatLonEnabled(new MappedFieldType(), new MappedFieldType()); break; + case 2: gft.setValidateLon(!gft.validateLon()); break; + case 3: gft.setValidateLat(!gft.validateLat()); break; + case 4: gft.setNormalizeLon(!gft.normalizeLon()); break; + case 5: gft.setNormalizeLat(!gft.normalizeLat()); break; + default: super.modifyProperty(ft, numProperties() - propNum - 1); + } + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapperTests.java index d31e2a1b01b..7080a6749f3 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapperTests.java @@ -57,7 +57,7 @@ public class GeoShapeFieldMapperTests extends ElasticsearchSingleNodeTest { GeoShapeFieldMapper geoShapeFieldMapper = (GeoShapeFieldMapper) fieldMapper; PrefixTreeStrategy strategy = geoShapeFieldMapper.fieldType().defaultStrategy(); - assertThat(strategy.getDistErrPct(), equalTo(GeoShapeFieldMapper.Defaults.DISTANCE_ERROR_PCT)); + assertThat(strategy.getDistErrPct(), equalTo(GeoShapeFieldMapper.Defaults.LEGACY_DISTANCE_ERROR_PCT)); assertThat(strategy.getGrid(), instanceOf(GeohashPrefixTree.class)); assertThat(strategy.getGrid().getMaxLevels(), equalTo(GeoShapeFieldMapper.Defaults.GEOHASH_LEVELS)); assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(GeoShapeFieldMapper.Defaults.ORIENTATION)); @@ -340,11 +340,12 @@ public class GeoShapeFieldMapperTests extends ElasticsearchSingleNodeTest { MergeResult mergeResult = stage1.merge(stage2.mapping(), false); // check correct conflicts assertThat(mergeResult.hasConflicts(), equalTo(true)); - assertThat(mergeResult.buildConflicts().length, equalTo(3)); + assertThat(mergeResult.buildConflicts().length, equalTo(4)); ArrayList conflicts = new ArrayList<>(Arrays.asList(mergeResult.buildConflicts())); assertThat("mapper [shape] has different strategy", isIn(conflicts)); assertThat("mapper [shape] has different tree", isIn(conflicts)); - assertThat("mapper [shape] has different tree_levels or precision", isIn(conflicts)); + assertThat("mapper [shape] has different tree_levels", isIn(conflicts)); + assertThat("mapper [shape] has different precision", isIn(conflicts)); // verify nothing changed FieldMapper fieldMapper = stage1.mappers().getMapper("shape"); @@ -362,12 +363,12 @@ public class GeoShapeFieldMapperTests extends ElasticsearchSingleNodeTest { // correct mapping stage2Mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("shape").field("type", "geo_shape").field("precision", "1m") - .field("distance_error_pct", 0.001).field("orientation", "cw").endObject().endObject().endObject().endObject().string(); + .field("tree_levels", 8).field("distance_error_pct", 0.001).field("orientation", "cw").endObject().endObject().endObject().endObject().string(); stage2 = parser.parse(stage2Mapping); mergeResult = stage1.merge(stage2.mapping(), false); // verify mapping changes, and ensure no failures - assertThat(mergeResult.hasConflicts(), equalTo(false)); + assertThat(Arrays.toString(mergeResult.buildConflicts()), mergeResult.hasConflicts(), equalTo(false)); fieldMapper = stage1.mappers().getMapper("shape"); assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class)); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java new file mode 100644 index 00000000000..81b8dc52bc4 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java @@ -0,0 +1,51 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.geo; + +import org.elasticsearch.common.geo.builders.ShapeBuilder; +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class GeoShapeFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + GeoShapeFieldMapper.GeoShapeFieldType gft = new GeoShapeFieldMapper.GeoShapeFieldType(); + gft.setNames(new MappedFieldType.Names("testgeoshape")); + return gft; + } + + @Override + protected int numProperties() { + return 6 + super.numProperties(); + } + + @Override + protected void modifyProperty(MappedFieldType ft, int propNum) { + GeoShapeFieldMapper.GeoShapeFieldType gft = (GeoShapeFieldMapper.GeoShapeFieldType)ft; + switch (propNum) { + case 0: gft.setTree("quadtree"); break; + case 1: gft.setStrategyName("term"); break; + case 2: gft.setTreeLevels(10); break; + case 3: gft.setPrecisionInMeters(20); break; + case 4: gft.setDefaultDistanceErrorPct(0.5); break; + case 5: gft.setOrientation(ShapeBuilder.Orientation.LEFT); break; + default: super.modifyProperty(ft, numProperties() - propNum - 1); + } + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/AllFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/AllFieldTypeTests.java new file mode 100644 index 00000000000..c71f6e06042 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/AllFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class AllFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new AllFieldMapper.AllFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java new file mode 100644 index 00000000000..a95f1d08d16 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java @@ -0,0 +1,43 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class FieldNamesFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new FieldNamesFieldMapper.FieldNamesFieldType(); + } + + @Override + protected int numProperties() { + return 1 + super.numProperties(); + } + + @Override + protected void modifyProperty(MappedFieldType ft, int propNum) { + FieldNamesFieldMapper.FieldNamesFieldType fnft = (FieldNamesFieldMapper.FieldNamesFieldType)ft; + switch (propNum) { + case 0: fnft.setEnabled(!fnft.isEnabled()); break; + default: super.modifyProperty(ft, numProperties() - propNum - 1); + } + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/IdFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/IdFieldTypeTests.java new file mode 100644 index 00000000000..f4da1c0dfe7 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/IdFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class IdFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new IdFieldMapper.IdFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/IndexFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/IndexFieldTypeTests.java new file mode 100644 index 00000000000..739ec0bdfb8 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/IndexFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class IndexFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new IndexFieldMapper.IndexFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/ParentFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/ParentFieldTypeTests.java new file mode 100644 index 00000000000..79cd0e85bda --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/ParentFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class ParentFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new ParentFieldMapper.ParentFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/RoutingFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/RoutingFieldTypeTests.java new file mode 100644 index 00000000000..44a5f7db7cf --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/RoutingFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class RoutingFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new RoutingFieldMapper.RoutingFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/SourceFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/SourceFieldTypeTests.java new file mode 100644 index 00000000000..2b0afae5f87 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/SourceFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class SourceFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new SourceFieldMapper.SourceFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/TimestampFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/TimestampFieldTypeTests.java new file mode 100644 index 00000000000..6f29cf82624 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/TimestampFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.core.DateFieldTypeTests; + +public class TimestampFieldTypeTests extends DateFieldTypeTests { + @Override + protected MappedFieldType createDefaultFieldType() { + return new TimestampFieldMapper.TimestampFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/TypeFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/TypeFieldTypeTests.java new file mode 100644 index 00000000000..c01b04584eb --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/TypeFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class TypeFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new TypeFieldMapper.TypeFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/UidFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/UidFieldTypeTests.java new file mode 100644 index 00000000000..93a56da74f9 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/UidFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class UidFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new UidFieldMapper.UidFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/VersionFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/VersionFieldTypeTests.java new file mode 100644 index 00000000000..8be4bb73d8a --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/VersionFieldTypeTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper.internal; + +import org.elasticsearch.index.mapper.FieldTypeTestCase; +import org.elasticsearch.index.mapper.MappedFieldType; + +public class VersionFieldTypeTests extends FieldTypeTestCase { + @Override + protected MappedFieldType createDefaultFieldType() { + return new VersionFieldMapper.VersionFieldType(); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java index 0583e289994..d875ac2d1e6 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java @@ -252,7 +252,7 @@ public class SimpleStringMappingTests extends ElasticsearchSingleNodeTest { DocumentMapper mapper = parser.parse(mapping); for (String fieldName : Lists.newArrayList("field1", "field2", "field3", "field4")) { Map serializedMap = getSerializedMap(fieldName, mapper); - assertFalse(serializedMap.containsKey("search_quote_analyzer")); + assertFalse(fieldName, serializedMap.containsKey("search_quote_analyzer")); } // Cases where search_quote_analyzer should be present. From 09a30e7eee6278ed7b9fde59360b88a5c7ceecde Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 12 Jun 2015 12:43:51 +0200 Subject: [PATCH 17/43] 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 --- .../TransportGetFieldMappingsIndexAction.java | 6 - .../action/search/SearchRequestBuilder.java | 54 -- .../action/update/UpdateRequestBuilder.java | 59 -- .../common/xcontent/ToXContent.java | 21 - .../index/query/QueryBuilders.java | 11 - .../index/query/ScriptQueryBuilder.java | 68 +- .../functionscore/ScoreFunctionBuilders.java | 37 +- .../script/ScriptScoreFunctionBuilder.java | 78 +- .../query/support/BaseInnerHitBuilder.java | 54 -- .../node/service/NodeService.java | 10 - .../org/elasticsearch/rest/RestRequest.java | 5 - .../java/org/elasticsearch/script/Script.java | 17 +- .../ValuesSourceAggregationBuilder.java | 79 -- .../heuristics/ScriptHeuristic.java | 64 +- .../scripted/ScriptedMetricBuilder.java | 242 ------ .../metrics/tophits/TopHitsBuilder.java | 54 -- .../search/builder/SearchSourceBuilder.java | 48 -- .../action/update/UpdateRequestTests.java | 79 -- .../org/elasticsearch/document/BulkTests.java | 274 ------- .../query/SimpleIndexQueryParserTests.java | 20 +- .../index/query/TemplateQueryBuilderTest.java | 15 - .../index/query/TemplateQueryTest.java | 279 ------- .../nested/SimpleNestedTests.java | 136 ---- .../script/IndexLookupTests.java | 501 ------------ .../script/IndexedScriptTests.java | 55 -- .../script/OnDiskScriptTests.java | 30 - .../script/ScriptFieldTests.java | 28 - .../bucket/DateHistogramTests.java | 374 --------- .../aggregations/bucket/DateRangeTests.java | 378 --------- .../aggregations/bucket/DoubleTermsTests.java | 290 ------- .../aggregations/bucket/HistogramTests.java | 251 ------ .../aggregations/bucket/IPv4RangeTests.java | 356 --------- .../aggregations/bucket/LongTermsTests.java | 257 ------ .../aggregations/bucket/RangeTests.java | 387 --------- ...ignificantTermsSignificanceScoreTests.java | 128 +-- .../aggregations/bucket/StringTermsTests.java | 318 -------- .../aggregations/bucket/TopHitsTests.java | 110 --- .../metrics/ScriptedMetricTests.java | 660 ---------------- .../basic/TransportTwoNodesSearchTests.java | 6 +- .../search/child/ChildQuerySearchTests.java | 8 +- .../search/fields/SearchFieldsTests.java | 215 ----- ...nctionScoreBackwardCompatibilityTests.java | 5 +- .../functionscore/FunctionScoreTests.java | 242 +----- .../RandomScoreFunctionTests.java | 74 +- .../search/geo/GeoDistanceTests.java | 75 -- .../search/innerhits/InnerHitsTests.java | 210 ----- .../search/rescore/QueryRescorerTests.java | 2 +- .../scriptfilter/ScriptQuerySearchTests.java | 54 +- .../search/sort/SimpleSortTests.java | 487 ------------ .../update/UpdateByNativeScriptTests.java | 21 - .../org/elasticsearch/update/UpdateTests.java | 732 ------------------ .../JavaScriptScriptSearchTests.java | 64 +- .../python/PythonScriptSearchTests.java | 70 +- 53 files changed, 109 insertions(+), 7989 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java b/core/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java index e9ef1538df2..5803c9e3a83 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java @@ -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 findFieldMappingsByType(DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) { diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java index 690ad43b466..1139670d527 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java @@ -430,60 +430,6 @@ public class SearchRequestBuilder extends ActionRequestBuilder 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 null). - * @deprecated Use {@link #addScriptField(String, Script)} instead. - */ - @Deprecated - public SearchRequestBuilder addScriptField(String name, String lang, String script, Map params) { - sourceBuilder().scriptField(name, lang, script, params); - return this; - } - /** * Adds a sort against the given field name and the sort ordering. * diff --git a/core/src/main/java/org/elasticsearch/action/update/UpdateRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/update/UpdateRequestBuilder.java index 7c30c47dd7c..d4d0ae18963 100644 --- a/core/src/main/java/org/elasticsearch/action/update/UpdateRequestBuilder.java +++ b/core/src/main/java/org/elasticsearch/action/update/UpdateRequestBuilder.java @@ -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 - * The script works with the variable ctx, which is bound to - * the entry, e.g. ctx._source.mycounter += 1. - * - * @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)
- * Default: groovy - *

- * 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 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. */ diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/ToXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/ToXContent.java index b4bfb668ccf..2d39578ea9c 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/ToXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/ToXContent.java @@ -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; diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java b/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java index c9070a24c37..fe2852d3a64 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java @@ -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. diff --git a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 8a6f72190c2..a9a35ac96e2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -31,65 +31,12 @@ public class ScriptQueryBuilder extends QueryBuilder { private Script script; - @Deprecated - private String scriptString; - - @Deprecated - private Map 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 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); } diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilders.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilders.java index ef9865395b3..ea8e255941e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilders.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilders.java @@ -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 params) { - return (new ScriptScoreFunctionBuilder()).script(script).lang(lang).params(params); - } - - /** - * @deprecated Use {@link #scriptFunction(Script)} instead. - */ - @Deprecated - public static ScriptScoreFunctionBuilder scriptFunction(String script, Map 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) { diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/script/ScriptScoreFunctionBuilder.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/script/ScriptScoreFunctionBuilder.java index 20dca88788a..023d8a6e5e1 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/script/ScriptScoreFunctionBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/script/ScriptScoreFunctionBuilder.java @@ -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 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 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(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/support/BaseInnerHitBuilder.java b/core/src/main/java/org/elasticsearch/index/query/support/BaseInnerHitBuilder.java index 8e991c8f130..48a2f59924e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/support/BaseInnerHitBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/support/BaseInnerHitBuilder.java @@ -154,60 +154,6 @@ public abstract class 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 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 null). - * @deprecated Use {@link #addScriptField(String, Script)} instead. - */ - @Deprecated - public T addScriptField(String name, String lang, String script, Map params) { - sourceBuilder().scriptField(name, lang, script, params); - return (T) this; - } - /** * Adds a sort against the given field name and the sort ordering. * diff --git a/core/src/main/java/org/elasticsearch/node/service/NodeService.java b/core/src/main/java/org/elasticsearch/node/service/NodeService.java index de5efa3791f..81dd3c109e7 100644 --- a/core/src/main/java/org/elasticsearch/node/service/NodeService.java +++ b/core/src/main/java/org/elasticsearch/node/service/NodeService.java @@ -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(); } diff --git a/core/src/main/java/org/elasticsearch/rest/RestRequest.java b/core/src/main/java/org/elasticsearch/rest/RestRequest.java index 272bfd038b4..2b326d59ea9 100644 --- a/core/src/main/java/org/elasticsearch/rest/RestRequest.java +++ b/core/src/main/java/org/elasticsearch/rest/RestRequest.java @@ -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); } diff --git a/core/src/main/java/org/elasticsearch/script/Script.java b/core/src/main/java/org/elasticsearch/script/Script.java index d826eaad8ed..1139e3cc9e6 100644 --- a/core/src/main/java/org/elasticsearch/script/Script.java +++ b/core/src/main/java/org/elasticsearch/script/Script.java @@ -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 params) { + public Script(String script, ScriptType type, @Nullable String lang, @Nullable Map 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)params; } /** diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/ValuesSourceAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/ValuesSourceAggregationBuilder.java index 0c305237f3d..b81573cbbc3 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/ValuesSourceAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/ValuesSourceAggregationBuilder.java @@ -80,85 +80,6 @@ public abstract class ValuesSourceAggregationBuildervalue script 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). - *

- * 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 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. */ diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index 24d5ef7126d..dbc46599eff 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -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 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 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; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricBuilder.java index 2e305aa3beb..0614cd7fb2a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricBuilder.java @@ -37,34 +37,6 @@ public class ScriptedMetricBuilder extends MetricsAggregationBuilder { private Script combineScript = null; private Script reduceScript = null; private Map params = null; - @Deprecated - private Map 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 reduce phase. - * - * @deprecated Use {@link #reduceScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder reduceParams(Map reduceParams) { - this.reduceParams = reduceParams; - return this; - } - - /** - * Set the init script. - * - * @deprecated Use {@link #initScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder initScript(String initScript) { - this.initScriptString = initScript; - return this; - } - - /** - * Set the map script. - * - * @deprecated Use {@link #mapScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder mapScript(String mapScript) { - this.mapScriptString = mapScript; - return this; - } - - /** - * Set the combine script. - * - * @deprecated Use {@link #combineScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder combineScript(String combineScript) { - this.combineScriptString = combineScript; - return this; - } - - /** - * Set the reduce script. - * - * @deprecated Use {@link #reduceScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder reduceScript(String reduceScript) { - this.reduceScriptString = reduceScript; - return this; - } - - /** - * Set the init script file. - * - * @deprecated Use {@link #initScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder initScriptFile(String initScriptFile) { - this.initScriptFile = initScriptFile; - return this; - } - - /** - * Set the map script file. - * - * @deprecated Use {@link #mapScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder mapScriptFile(String mapScriptFile) { - this.mapScriptFile = mapScriptFile; - return this; - } - - /** - * Set the combine script file. - * - * @deprecated Use {@link #combineScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder combineScriptFile(String combineScriptFile) { - this.combineScriptFile = combineScriptFile; - return this; - } - - /** - * Set the reduce script file. - * - * @deprecated Use {@link #reduceScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder reduceScriptFile(String reduceScriptFile) { - this.reduceScriptFile = reduceScriptFile; - return this; - } - - /** - * Set the indexed init script id. - * - * @deprecated Use {@link #initScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder initScriptId(String initScriptId) { - this.initScriptId = initScriptId; - return this; - } - - /** - * Set the indexed map script id. - * - * @deprecated Use {@link #mapScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder mapScriptId(String mapScriptId) { - this.mapScriptId = mapScriptId; - return this; - } - - /** - * Set the indexed combine script id. - * - * @deprecated Use {@link #combineScript(Script)} instead. - */ - @Deprecated - public ScriptedMetricBuilder combineScriptId(String combineScriptId) { - this.combineScriptId = combineScriptId; - return this; - } - - /** - * Set the indexed reduce 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); - } } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java index b59af08e634..62bd22ab082 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsBuilder.java @@ -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 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 null). - * @deprecated Use {@link #addScriptField(String, Script)} instead. - */ - @Deprecated - public TopHitsBuilder addScriptField(String name, String lang, String script, Map params) { - sourceBuilder().scriptField(name, lang, script, params); - return this; - } - /** * Adds a sort against the given field name and the sort ordering. * diff --git a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index 85ecbe200c1..0eb4c5cc882 100644 --- a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -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 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 null) - * @deprecated Use {@link #scriptField(String, Script)} instead. - */ - @Deprecated - public SearchSourceBuilder scriptField(String name, String lang, String script, Map 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. diff --git a/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java b/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java index 4292b7df613..b5b8d63a2a9 100644 --- a/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java +++ b/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java @@ -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 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 doc = request.doc().sourceAsMap(); - assertThat(doc.get("field1").toString(), equalTo("value1")); - assertThat(((Map) doc.get("compound")).get("field2").toString(), equalTo("value2")); - } } diff --git a/core/src/test/java/org/elasticsearch/document/BulkTests.java b/core/src/test/java/org/elasticsearch/document/BulkTests.java index edb38190f55..46cc704966f 100644 --- a/core/src/test/java/org/elasticsearch/document/BulkTests.java +++ b/core/src/test/java/org/elasticsearch/document/BulkTests.java @@ -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 { diff --git a/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java b/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java index 3b72223b46f..db40123bc28 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java @@ -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() diff --git a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTest.java index 426185cc06f..692f4147800 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTest.java @@ -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 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()); - } } diff --git a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java index 5f40004de3d..84cf99fe3fe 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java @@ -545,283 +545,4 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { assertHitCount(searchResponse, 5); } - /* - * TODO Remove in 2.0 - */ - @Test - public void testTemplateInBodyOldScriptAPI() throws IOException { - Map 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 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 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 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 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 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 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 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 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 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 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 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); - } - } diff --git a/core/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java b/core/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java index 86a698e07dd..06fac33e672 100644 --- a/core/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java +++ b/core/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java @@ -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 { diff --git a/core/src/test/java/org/elasticsearch/script/IndexLookupTests.java b/core/src/test/java/org/elasticsearch/script/IndexLookupTests.java index fda14ca3dd9..84322c73fb7 100644 --- a/core/src/test/java/org/elasticsearch/script/IndexLookupTests.java +++ b/core/src/test/java/org/elasticsearch/script/IndexLookupTests.java @@ -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 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> expectedPayloadsArray = new HashMap<>(); - script = createPositionsArrayScriptOldScriptAPI("float_payload_field", "b", includeAllFlag, "payloadAsFloat(-1)"); - float missingValue = -1; - List payloadsFor1 = new ArrayList<>(); - payloadsFor1.add(2f); - payloadsFor1.add(missingValue); - expectedPayloadsArray.put("1", payloadsFor1); - List 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> 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 expectedFieldVals, - String scoreScript, Map 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 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 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> 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())); - } } diff --git a/core/src/test/java/org/elasticsearch/script/IndexedScriptTests.java b/core/src/test/java/org/elasticsearch/script/IndexedScriptTests.java index d8d493acb3f..c88f1014094 100644 --- a/core/src/test/java/org/elasticsearch/script/IndexedScriptTests.java +++ b/core/src/test/java/org/elasticsearch/script/IndexedScriptTests.java @@ -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")); - } - } } diff --git a/core/src/test/java/org/elasticsearch/script/OnDiskScriptTests.java b/core/src/test/java/org/elasticsearch/script/OnDiskScriptTests.java index b49486f0509..d10e8888e04 100644 --- a/core/src/test/java/org/elasticsearch/script/OnDiskScriptTests.java +++ b/core/src/test/java/org/elasticsearch/script/OnDiskScriptTests.java @@ -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")); - } - } } diff --git a/core/src/test/java/org/elasticsearch/script/ScriptFieldTests.java b/core/src/test/java/org/elasticsearch/script/ScriptFieldTests.java index 81319f67798..d50b9573bc7 100644 --- a/core/src/test/java/org/elasticsearch/script/ScriptFieldTests.java +++ b/core/src/test/java/org/elasticsearch/script/ScriptFieldTests.java @@ -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 params) { diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java index 28c6aa38590..c8944aafb69 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java @@ -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 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 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 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 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 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 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 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())); - } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java index 589b2b39bc5..795c5829432 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java @@ -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 buckets = new ArrayList(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 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 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 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 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 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 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())); - } - - } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java index 69f3cff44e2..daa1914fce9 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsTests.java @@ -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)); - } - } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/HistogramTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/HistogramTests.java index 2d1012f2b51..2b8b57a3fec 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/HistogramTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/HistogramTests.java @@ -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 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 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 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 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 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 buckets = new ArrayList(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 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 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)); - } - } - } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java index 59674eac1c0..1b72e5bfa63 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java @@ -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 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 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 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 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 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 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 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"))); - } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsTests.java index b825259ee32..dee56814e70 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsTests.java @@ -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)); - } - } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java index c53b763a0a5..4ea14e5bf9c 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java @@ -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 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 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 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 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 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 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 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)); - } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreTests.java index 26d06dfb866..8ec26ce8e20 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreTests.java @@ -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 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 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); } -} +} \ No newline at end of file diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/StringTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/StringTermsTests.java index 7baf417c919..a2daafdc13f 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/StringTermsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/StringTermsTests.java @@ -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)); - } - } - } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java index d5292e6786c..899123447e9 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java @@ -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 text 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 comment")); - - // 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")); - } - } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricTests.java index 2fced92d8eb..4400f96273a 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricTests.java @@ -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 map = (Map) 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 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map params = new HashMap<>(); - params.put("_agg", new ArrayList<>()); - params.put("vars", varsMap); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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 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 varsMap = new HashMap<>(); - varsMap.put("multiplier", 1); - Map 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()); - } - } diff --git a/core/src/test/java/org/elasticsearch/search/basic/TransportTwoNodesSearchTests.java b/core/src/test/java/org/elasticsearch/search/basic/TransportTwoNodesSearchTests.java index 2f9533671e9..dd2f43f5227 100644 --- a/core/src/test/java/org/elasticsearch/search/basic/TransportTwoNodesSearchTests.java +++ b/core/src/test/java/org/elasticsearch/search/basic/TransportTwoNodesSearchTests.java @@ -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(); diff --git a/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchTests.java b/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchTests.java index 78b2d935350..c3362b9dcd5 100644 --- a/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchTests.java +++ b/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchTests.java @@ -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(); } diff --git a/core/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java b/core/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java index f9c02f52299..a5243b37e1d 100644 --- a/core/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java +++ b/core/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java @@ -610,219 +610,4 @@ public class SearchFieldsTests extends ElasticsearchIntegrationTest { assertThat(fields.get("md").getValues(), equalTo(Arrays. 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 params = MapBuilder. 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 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 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 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 fields = hit.getFields(); - assertThat(fields.get("s").getValues(), equalTo(Collections. singletonList(Integer.toString(id)))); - assertThat(fields.get("l").getValues(), equalTo(Collections. singletonList((long) id))); - assertThat(fields.get("d").getValues(), equalTo(Collections. singletonList((double) id))); - assertThat(fields.get("ms").getValues(), equalTo(Arrays. asList(Integer.toString(id), Integer.toString(id + 1)))); - assertThat(fields.get("ml").getValues(), equalTo(Arrays. asList((long) id, id + 1L))); - assertThat(fields.get("md").getValues(), equalTo(Arrays. asList((double) id, id + 1d))); - } - } } diff --git a/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreBackwardCompatibilityTests.java b/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreBackwardCompatibilityTests.java index 68f4a33d824..74aefb63f35 100644 --- a/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreBackwardCompatibilityTests.java +++ b/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreBackwardCompatibilityTests.java @@ -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); diff --git a/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java b/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java index 0ff64ad876b..a4f5449bf23 100644 --- a/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java +++ b/core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java @@ -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 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); - } } diff --git a/core/src/test/java/org/elasticsearch/search/functionscore/RandomScoreFunctionTests.java b/core/src/test/java/org/elasticsearch/search/functionscore/RandomScoreFunctionTests.java index 566c1887886..0008c94e762 100644 --- a/core/src/test/java/org/elasticsearch/search/functionscore/RandomScoreFunctionTests.java +++ b/core/src/test/java/org/elasticsearch/search/functionscore/RandomScoreFunctionTests.java @@ -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" }))); diff --git a/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceTests.java b/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceTests.java index 8ecde929122..79ae781997f 100644 --- a/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceTests.java +++ b/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceTests.java @@ -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 { diff --git a/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java b/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java index 8d64ef26602..c9f0e4a85a1 100644 --- a/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java +++ b/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java @@ -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 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("fox 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 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("fox 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( diff --git a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescorerTests.java b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescorerTests.java index 8bcf5f4477a..3ed24ac8168 100644 --- a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescorerTests.java +++ b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescorerTests.java @@ -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)); diff --git a/core/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchTests.java b/core/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchTests.java index a37bc269ae6..97417a53e7f 100644 --- a/core/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchTests.java +++ b/core/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchTests.java @@ -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() { diff --git a/core/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java b/core/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java index 8a369d8f5f9..b3259ae439a 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java @@ -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 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", diff --git a/core/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java b/core/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java index 71b4cb803d9..30ad56b2dd4 100644 --- a/core/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java +++ b/core/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java @@ -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 params = Maps.newHashMap(); - params.put("foo", "SETVALUE"); - client().prepareUpdate("test", "type", "1") - .setScript("custom", ScriptService.ScriptType.INLINE) - .setScriptLang(NativeScriptEngineService.NAME).setScriptParams(params).get(); - - Map 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 params) { diff --git a/core/src/test/java/org/elasticsearch/update/UpdateTests.java b/core/src/test/java/org/elasticsearch/update/UpdateTests.java index c3a72e5ad67..5c0e14c0db1 100644 --- a/core/src/test/java/org/elasticsearch/update/UpdateTests.java +++ b/core/src/test/java/org/elasticsearch/update/UpdateTests.java @@ -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 testMap = new HashMap<>(); - Map testMap2 = new HashMap<>(); - Map 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 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 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 failures = new CopyOnWriteArrayList<>(); - - final class UpdateThread extends Thread { - final Map 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 { - 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 { - 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 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 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"; } diff --git a/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java b/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java index 53cf8d198da..83396537304 100644 --- a/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java +++ b/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java @@ -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 sObj1 = (Map) 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.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)); diff --git a/plugins/lang-python/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java b/plugins/lang-python/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java index 0ff68beea3e..3729d27b9ff 100644 --- a/plugins/lang-python/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java +++ b/plugins/lang-python/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java @@ -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 sObj1 = (Map) 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.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(); From 364cbbd28232a2a111158de6993692875f7fe1d8 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sat, 13 Jun 2015 10:34:36 +0200 Subject: [PATCH 18/43] [TEST] Fix JavaScript tests - case matters here --- .../script/javascript/JavaScriptScriptSearchTests.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java b/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java index 83396537304..3ad1e9dc1d7 100644 --- a/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java +++ b/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptSearchTests.java @@ -75,9 +75,9 @@ public class JavaScriptScriptSearchTests extends ElasticsearchIntegrationTest { logger.info(" --> running doc['num1'].value > 1"); SearchResponse response = client().prepareSearch() - .setQuery(scriptQuery(new Script("doc['num1'].value > 1", ScriptService.ScriptType.INLINE, "JS", null))) + .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)) + .addScriptField("sNum1", new Script("doc['num1'].value", ScriptService.ScriptType.INLINE, "js", null)) .execute().actionGet(); assertThat(response.getHits().totalHits(), equalTo(2l)); From e88535a67e9594f3135465b5021ba9b502fef950 Mon Sep 17 00:00:00 2001 From: Clinton Gormley Date: Sat, 13 Jun 2015 11:28:05 +0200 Subject: [PATCH 19/43] Merge pull request #11614 from oyiadom/patch-1 Fix typo in upgrade docs --- docs/reference/setup/upgrade.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/setup/upgrade.asciidoc b/docs/reference/setup/upgrade.asciidoc index 9f46fbaf059..df7de9711d0 100644 --- a/docs/reference/setup/upgrade.asciidoc +++ b/docs/reference/setup/upgrade.asciidoc @@ -95,7 +95,7 @@ curl -XPUT localhost:9200/_cluster/settings -d '{ * There is no problem continuing to index while doing the upgrade. However, you can speed the process considerably by *temporarily* stopping non-essential indexing and issuing a manual <>. A synced flush is special kind of flush which can seriously speed up recovery of shards. Elasticsearch automatically -uses it when an index has been inactive for a while (default is `30m`) but you can manuallky trigger it using the following command: +uses it when an index has been inactive for a while (default is `30m`) but you can manually trigger it using the following command: [source,sh] -------------------------------------------------- From 58ccb39deea9afb136d8ad9ce6defc7c67b52cad Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sat, 13 Jun 2015 20:29:18 +0200 Subject: [PATCH 20/43] Use CopyOnWrite list since list is concurrently modified as well as iterated --- .../transport/ContextAndHeaderTransportTests.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java b/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java index 289659867d2..db9219785c8 100644 --- a/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java +++ b/core/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java @@ -57,6 +57,7 @@ import org.junit.Before; import org.junit.Test; import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.common.settings.Settings.settingsBuilder; @@ -70,7 +71,7 @@ import static org.hamcrest.Matchers.*; @ClusterScope(scope = SUITE) public class ContextAndHeaderTransportTests extends ElasticsearchIntegrationTest { - private static final List requests = Collections.synchronizedList(new ArrayList()); + private static final List requests = new CopyOnWriteArrayList<>(); private String randomHeaderKey = randomAsciiOfLength(10); private String randomHeaderValue = randomAsciiOfLength(20); private String queryIndex = "query-" + randomAsciiOfLength(10).toLowerCase(Locale.ROOT); From 0ac09fde95be6624837eaefd317d3ec1fc8b76f9 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sat, 13 Jun 2015 21:36:08 +0200 Subject: [PATCH 21/43] [TEST] Use NoMergePolicy in EngineTest#testSegments These tests rely on that there are no merges but since MP is now random this can break the test. --- .../index/engine/InternalEngineTests.java | 198 +++++++++--------- .../index/engine/ShadowEngineTests.java | 15 +- 2 files changed, 112 insertions(+), 101 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index dc39c9e1c6b..a3b88bed4e9 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -262,121 +262,125 @@ public class InternalEngineTests extends ElasticsearchTestCase { @Test public void testSegments() throws Exception { - List segments = engine.segments(false); - assertThat(segments.isEmpty(), equalTo(true)); - assertThat(engine.segmentsStats().getCount(), equalTo(0l)); - assertThat(engine.segmentsStats().getMemoryInBytes(), equalTo(0l)); - final boolean defaultCompound = defaultSettings.getAsBoolean(EngineConfig.INDEX_COMPOUND_ON_FLUSH, true); + IndexSettingsService indexSettingsService = new IndexSettingsService(shardId.index(), Settings.builder().put(defaultSettings).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build()); + try (Store store = createStore(); + Engine engine = createEngine(indexSettingsService, store, createTempDir(), new MergeSchedulerConfig(defaultSettings), NoMergePolicy.INSTANCE)) { + List segments = engine.segments(false); + assertThat(segments.isEmpty(), equalTo(true)); + assertThat(engine.segmentsStats().getCount(), equalTo(0l)); + assertThat(engine.segmentsStats().getMemoryInBytes(), equalTo(0l)); + final boolean defaultCompound = defaultSettings.getAsBoolean(EngineConfig.INDEX_COMPOUND_ON_FLUSH, true); - // create a doc and refresh - ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocumentWithTextField(), B_1, null); - engine.create(new Engine.Create(null, newUid("1"), doc)); + // create a doc and refresh + ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocumentWithTextField(), B_1, null); + engine.create(new Engine.Create(null, newUid("1"), doc)); - ParsedDocument doc2 = testParsedDocument("2", "2", "test", null, -1, -1, testDocumentWithTextField(), B_2, null); - engine.create(new Engine.Create(null, newUid("2"), doc2)); - engine.refresh("test"); + ParsedDocument doc2 = testParsedDocument("2", "2", "test", null, -1, -1, testDocumentWithTextField(), B_2, null); + engine.create(new Engine.Create(null, newUid("2"), doc2)); + engine.refresh("test"); - segments = engine.segments(false); - assertThat(segments.size(), equalTo(1)); - SegmentsStats stats = engine.segmentsStats(); - assertThat(stats.getCount(), equalTo(1l)); - assertThat(stats.getTermsMemoryInBytes(), greaterThan(0l)); - assertThat(stats.getStoredFieldsMemoryInBytes(), greaterThan(0l)); - assertThat(stats.getTermVectorsMemoryInBytes(), equalTo(0l)); - assertThat(stats.getNormsMemoryInBytes(), greaterThan(0l)); - assertThat(stats.getDocValuesMemoryInBytes(), greaterThan(0l)); - assertThat(segments.get(0).isCommitted(), equalTo(false)); - assertThat(segments.get(0).isSearch(), equalTo(true)); - assertThat(segments.get(0).getNumDocs(), equalTo(2)); - assertThat(segments.get(0).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); - assertThat(segments.get(0).ramTree, nullValue()); + segments = engine.segments(false); + assertThat(segments.size(), equalTo(1)); + SegmentsStats stats = engine.segmentsStats(); + assertThat(stats.getCount(), equalTo(1l)); + assertThat(stats.getTermsMemoryInBytes(), greaterThan(0l)); + assertThat(stats.getStoredFieldsMemoryInBytes(), greaterThan(0l)); + assertThat(stats.getTermVectorsMemoryInBytes(), equalTo(0l)); + assertThat(stats.getNormsMemoryInBytes(), greaterThan(0l)); + assertThat(stats.getDocValuesMemoryInBytes(), greaterThan(0l)); + assertThat(segments.get(0).isCommitted(), equalTo(false)); + assertThat(segments.get(0).isSearch(), equalTo(true)); + assertThat(segments.get(0).getNumDocs(), equalTo(2)); + assertThat(segments.get(0).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); + assertThat(segments.get(0).ramTree, nullValue()); - engine.flush(); + engine.flush(); - segments = engine.segments(false); - assertThat(segments.size(), equalTo(1)); - assertThat(engine.segmentsStats().getCount(), equalTo(1l)); - assertThat(segments.get(0).isCommitted(), equalTo(true)); - assertThat(segments.get(0).isSearch(), equalTo(true)); - assertThat(segments.get(0).getNumDocs(), equalTo(2)); - assertThat(segments.get(0).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); + segments = engine.segments(false); + assertThat(segments.size(), equalTo(1)); + assertThat(engine.segmentsStats().getCount(), equalTo(1l)); + assertThat(segments.get(0).isCommitted(), equalTo(true)); + assertThat(segments.get(0).isSearch(), equalTo(true)); + assertThat(segments.get(0).getNumDocs(), equalTo(2)); + assertThat(segments.get(0).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); - engine.config().setCompoundOnFlush(false); + engine.config().setCompoundOnFlush(false); - ParsedDocument doc3 = testParsedDocument("3", "3", "test", null, -1, -1, testDocumentWithTextField(), B_3, null); - engine.create(new Engine.Create(null, newUid("3"), doc3)); - engine.refresh("test"); + ParsedDocument doc3 = testParsedDocument("3", "3", "test", null, -1, -1, testDocumentWithTextField(), B_3, null); + engine.create(new Engine.Create(null, newUid("3"), doc3)); + engine.refresh("test"); - segments = engine.segments(false); - assertThat(segments.size(), equalTo(2)); - assertThat(engine.segmentsStats().getCount(), equalTo(2l)); - assertThat(engine.segmentsStats().getTermsMemoryInBytes(), greaterThan(stats.getTermsMemoryInBytes())); - assertThat(engine.segmentsStats().getStoredFieldsMemoryInBytes(), greaterThan(stats.getStoredFieldsMemoryInBytes())); - assertThat(engine.segmentsStats().getTermVectorsMemoryInBytes(), equalTo(0l)); - assertThat(engine.segmentsStats().getNormsMemoryInBytes(), greaterThan(stats.getNormsMemoryInBytes())); - assertThat(engine.segmentsStats().getDocValuesMemoryInBytes(), greaterThan(stats.getDocValuesMemoryInBytes())); - assertThat(segments.get(0).getGeneration() < segments.get(1).getGeneration(), equalTo(true)); - assertThat(segments.get(0).isCommitted(), equalTo(true)); - assertThat(segments.get(0).isSearch(), equalTo(true)); - assertThat(segments.get(0).getNumDocs(), equalTo(2)); - assertThat(segments.get(0).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); + segments = engine.segments(false); + assertThat(segments.size(), equalTo(2)); + assertThat(engine.segmentsStats().getCount(), equalTo(2l)); + assertThat(engine.segmentsStats().getTermsMemoryInBytes(), greaterThan(stats.getTermsMemoryInBytes())); + assertThat(engine.segmentsStats().getStoredFieldsMemoryInBytes(), greaterThan(stats.getStoredFieldsMemoryInBytes())); + assertThat(engine.segmentsStats().getTermVectorsMemoryInBytes(), equalTo(0l)); + assertThat(engine.segmentsStats().getNormsMemoryInBytes(), greaterThan(stats.getNormsMemoryInBytes())); + assertThat(engine.segmentsStats().getDocValuesMemoryInBytes(), greaterThan(stats.getDocValuesMemoryInBytes())); + assertThat(segments.get(0).getGeneration() < segments.get(1).getGeneration(), equalTo(true)); + assertThat(segments.get(0).isCommitted(), equalTo(true)); + assertThat(segments.get(0).isSearch(), equalTo(true)); + assertThat(segments.get(0).getNumDocs(), equalTo(2)); + assertThat(segments.get(0).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); - assertThat(segments.get(1).isCommitted(), equalTo(false)); - assertThat(segments.get(1).isSearch(), equalTo(true)); - assertThat(segments.get(1).getNumDocs(), equalTo(1)); - assertThat(segments.get(1).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(1).isCompound(), equalTo(false)); + assertThat(segments.get(1).isCommitted(), equalTo(false)); + assertThat(segments.get(1).isSearch(), equalTo(true)); + assertThat(segments.get(1).getNumDocs(), equalTo(1)); + assertThat(segments.get(1).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(1).isCompound(), equalTo(false)); - engine.delete(new Engine.Delete("test", "1", newUid("1"))); - engine.refresh("test"); + engine.delete(new Engine.Delete("test", "1", newUid("1"))); + engine.refresh("test"); - segments = engine.segments(false); - assertThat(segments.size(), equalTo(2)); - assertThat(engine.segmentsStats().getCount(), equalTo(2l)); - assertThat(segments.get(0).getGeneration() < segments.get(1).getGeneration(), equalTo(true)); - assertThat(segments.get(0).isCommitted(), equalTo(true)); - assertThat(segments.get(0).isSearch(), equalTo(true)); - assertThat(segments.get(0).getNumDocs(), equalTo(1)); - assertThat(segments.get(0).getDeletedDocs(), equalTo(1)); - assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); + segments = engine.segments(false); + assertThat(segments.size(), equalTo(2)); + assertThat(engine.segmentsStats().getCount(), equalTo(2l)); + assertThat(segments.get(0).getGeneration() < segments.get(1).getGeneration(), equalTo(true)); + assertThat(segments.get(0).isCommitted(), equalTo(true)); + assertThat(segments.get(0).isSearch(), equalTo(true)); + assertThat(segments.get(0).getNumDocs(), equalTo(1)); + assertThat(segments.get(0).getDeletedDocs(), equalTo(1)); + assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); - assertThat(segments.get(1).isCommitted(), equalTo(false)); - assertThat(segments.get(1).isSearch(), equalTo(true)); - assertThat(segments.get(1).getNumDocs(), equalTo(1)); - assertThat(segments.get(1).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(1).isCompound(), equalTo(false)); + assertThat(segments.get(1).isCommitted(), equalTo(false)); + assertThat(segments.get(1).isSearch(), equalTo(true)); + assertThat(segments.get(1).getNumDocs(), equalTo(1)); + assertThat(segments.get(1).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(1).isCompound(), equalTo(false)); - engine.config().setCompoundOnFlush(true); - ParsedDocument doc4 = testParsedDocument("4", "4", "test", null, -1, -1, testDocumentWithTextField(), B_3, null); - engine.create(new Engine.Create(null, newUid("4"), doc4)); - engine.refresh("test"); + engine.config().setCompoundOnFlush(true); + ParsedDocument doc4 = testParsedDocument("4", "4", "test", null, -1, -1, testDocumentWithTextField(), B_3, null); + engine.create(new Engine.Create(null, newUid("4"), doc4)); + engine.refresh("test"); - segments = engine.segments(false); - assertThat(segments.size(), equalTo(3)); - assertThat(engine.segmentsStats().getCount(), equalTo(3l)); - assertThat(segments.get(0).getGeneration() < segments.get(1).getGeneration(), equalTo(true)); - assertThat(segments.get(0).isCommitted(), equalTo(true)); - assertThat(segments.get(0).isSearch(), equalTo(true)); - assertThat(segments.get(0).getNumDocs(), equalTo(1)); - assertThat(segments.get(0).getDeletedDocs(), equalTo(1)); - assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); + segments = engine.segments(false); + assertThat(segments.size(), equalTo(3)); + assertThat(engine.segmentsStats().getCount(), equalTo(3l)); + assertThat(segments.get(0).getGeneration() < segments.get(1).getGeneration(), equalTo(true)); + assertThat(segments.get(0).isCommitted(), equalTo(true)); + assertThat(segments.get(0).isSearch(), equalTo(true)); + assertThat(segments.get(0).getNumDocs(), equalTo(1)); + assertThat(segments.get(0).getDeletedDocs(), equalTo(1)); + assertThat(segments.get(0).isCompound(), equalTo(defaultCompound)); - assertThat(segments.get(1).isCommitted(), equalTo(false)); - assertThat(segments.get(1).isSearch(), equalTo(true)); - assertThat(segments.get(1).getNumDocs(), equalTo(1)); - assertThat(segments.get(1).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(1).isCompound(), equalTo(false)); + assertThat(segments.get(1).isCommitted(), equalTo(false)); + assertThat(segments.get(1).isSearch(), equalTo(true)); + assertThat(segments.get(1).getNumDocs(), equalTo(1)); + assertThat(segments.get(1).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(1).isCompound(), equalTo(false)); - assertThat(segments.get(2).isCommitted(), equalTo(false)); - assertThat(segments.get(2).isSearch(), equalTo(true)); - assertThat(segments.get(2).getNumDocs(), equalTo(1)); - assertThat(segments.get(2).getDeletedDocs(), equalTo(0)); - assertThat(segments.get(2).isCompound(), equalTo(true)); + assertThat(segments.get(2).isCommitted(), equalTo(false)); + assertThat(segments.get(2).isSearch(), equalTo(true)); + assertThat(segments.get(2).getNumDocs(), equalTo(1)); + assertThat(segments.get(2).getDeletedDocs(), equalTo(0)); + assertThat(segments.get(2).isCompound(), equalTo(true)); + } } public void testVerboseSegments() throws Exception { diff --git a/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java index 31a9a7db1c2..e8a0205448f 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java @@ -208,18 +208,22 @@ public class ShadowEngineTests extends ElasticsearchTestCase { } protected ShadowEngine createShadowEngine(IndexSettingsService indexSettingsService, Store store) { - return new ShadowEngine(config(indexSettingsService, store, null, new MergeSchedulerConfig(indexSettingsService.indexSettings()))); + return new ShadowEngine(config(indexSettingsService, store, null, new MergeSchedulerConfig(indexSettingsService.indexSettings()), null)); } protected InternalEngine createInternalEngine(IndexSettingsService indexSettingsService, Store store, Path translogPath) { - return new InternalEngine(config(indexSettingsService, store, translogPath, new MergeSchedulerConfig(indexSettingsService.indexSettings())), true); + return createInternalEngine(indexSettingsService, store, translogPath, newMergePolicy()); } - public EngineConfig config(IndexSettingsService indexSettingsService, Store store, Path translogPath, MergeSchedulerConfig mergeSchedulerConfig) { + protected InternalEngine createInternalEngine(IndexSettingsService indexSettingsService, Store store, Path translogPath, MergePolicy mergePolicy) { + return new InternalEngine(config(indexSettingsService, store, translogPath, new MergeSchedulerConfig(indexSettingsService.indexSettings()), mergePolicy), true); + } + + public EngineConfig config(IndexSettingsService indexSettingsService, Store store, Path translogPath, MergeSchedulerConfig mergeSchedulerConfig, MergePolicy mergePolicy) { IndexWriterConfig iwc = newIndexWriterConfig(); TranslogConfig translogConfig = new TranslogConfig(shardId, translogPath, indexSettingsService.getSettings(), Translog.Durabilty.REQUEST, BigArrays.NON_RECYCLING_INSTANCE, threadPool); EngineConfig config = new EngineConfig(shardId, threadPool, new ShardIndexingService(shardId, EMPTY_SETTINGS, new ShardSlowLogIndexingService(shardId, EMPTY_SETTINGS, indexSettingsService)), indexSettingsService - , null, store, createSnapshotDeletionPolicy(),newMergePolicy(), mergeSchedulerConfig, + , null, store, createSnapshotDeletionPolicy(), mergePolicy, mergeSchedulerConfig, iwc.getAnalyzer(), iwc.getSimilarity() , new CodecService(shardId.index()), new Engine.FailedEngineListener() { @Override public void onFailedEngine(ShardId shardId, String reason, @Nullable Throwable t) { @@ -264,6 +268,9 @@ public class ShadowEngineTests extends ElasticsearchTestCase { @Test public void testSegments() throws Exception { + IndexSettingsService indexSettingsService = new IndexSettingsService(shardId.index(), Settings.builder().put(defaultSettings).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build()); + primaryEngine.close(); // recreate without merging + primaryEngine = createInternalEngine(indexSettingsService, store, createTempDir(), NoMergePolicy.INSTANCE); List segments = primaryEngine.segments(false); assertThat(segments.isEmpty(), equalTo(true)); assertThat(primaryEngine.segmentsStats().getCount(), equalTo(0l)); From 02e540384d600f4134f52794f4a12371272453fc Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sun, 14 Jun 2015 12:20:28 +0200 Subject: [PATCH 22/43] [TEST] Use NoMergePolicy in EngineTest#testVerboseSegments These tests rely on that there are no merges but since MP is now random this can break the test. --- .../index/engine/InternalEngineTests.java | 42 ++++++++++--------- .../index/engine/ShadowEngineTests.java | 3 ++ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index a3b88bed4e9..2cabd6d2fdd 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -384,29 +384,33 @@ public class InternalEngineTests extends ElasticsearchTestCase { } public void testVerboseSegments() throws Exception { - List segments = engine.segments(true); - assertThat(segments.isEmpty(), equalTo(true)); + IndexSettingsService indexSettingsService = new IndexSettingsService(shardId.index(), Settings.builder().put(defaultSettings).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build()); + try (Store store = createStore(); + Engine engine = createEngine(indexSettingsService, store, createTempDir(), new MergeSchedulerConfig(defaultSettings), NoMergePolicy.INSTANCE)) { + List segments = engine.segments(true); + assertThat(segments.isEmpty(), equalTo(true)); - ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocumentWithTextField(), B_1, null); - engine.create(new Engine.Create(null, newUid("1"), doc)); - engine.refresh("test"); + ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocumentWithTextField(), B_1, null); + engine.create(new Engine.Create(null, newUid("1"), doc)); + engine.refresh("test"); - segments = engine.segments(true); - assertThat(segments.size(), equalTo(1)); - assertThat(segments.get(0).ramTree, notNullValue()); + segments = engine.segments(true); + assertThat(segments.size(), equalTo(1)); + assertThat(segments.get(0).ramTree, notNullValue()); - ParsedDocument doc2 = testParsedDocument("2", "2", "test", null, -1, -1, testDocumentWithTextField(), B_2, null); - engine.create(new Engine.Create(null, newUid("2"), doc2)); - engine.refresh("test"); - ParsedDocument doc3 = testParsedDocument("3", "3", "test", null, -1, -1, testDocumentWithTextField(), B_3, null); - engine.create(new Engine.Create(null, newUid("3"), doc3)); - engine.refresh("test"); + ParsedDocument doc2 = testParsedDocument("2", "2", "test", null, -1, -1, testDocumentWithTextField(), B_2, null); + engine.create(new Engine.Create(null, newUid("2"), doc2)); + engine.refresh("test"); + ParsedDocument doc3 = testParsedDocument("3", "3", "test", null, -1, -1, testDocumentWithTextField(), B_3, null); + engine.create(new Engine.Create(null, newUid("3"), doc3)); + engine.refresh("test"); - segments = engine.segments(true); - assertThat(segments.size(), equalTo(3)); - assertThat(segments.get(0).ramTree, notNullValue()); - assertThat(segments.get(1).ramTree, notNullValue()); - assertThat(segments.get(2).ramTree, notNullValue()); + segments = engine.segments(true); + assertThat(segments.size(), equalTo(3)); + assertThat(segments.get(0).ramTree, notNullValue()); + assertThat(segments.get(1).ramTree, notNullValue()); + assertThat(segments.get(2).ramTree, notNullValue()); + } } diff --git a/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java index e8a0205448f..fe831a98fb3 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java @@ -440,6 +440,9 @@ public class ShadowEngineTests extends ElasticsearchTestCase { @Test public void testVerboseSegments() throws Exception { + IndexSettingsService indexSettingsService = new IndexSettingsService(shardId.index(), Settings.builder().put(defaultSettings).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build()); + primaryEngine.close(); // recreate without merging + primaryEngine = createInternalEngine(indexSettingsService, store, createTempDir(), NoMergePolicy.INSTANCE); List segments = primaryEngine.segments(true); assertThat(segments.isEmpty(), equalTo(true)); From 2376cc500d32471dc3955c38d9b90d7d3efab216 Mon Sep 17 00:00:00 2001 From: Clinton Gormley Date: Sun, 14 Jun 2015 22:14:26 +0200 Subject: [PATCH 23/43] Merge pull request #11649 from adjust/jdk_docs Clarify Java requirements --- docs/reference/getting-started.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/getting-started.asciidoc b/docs/reference/getting-started.asciidoc index d897b97ab27..2c47a8b513e 100755 --- a/docs/reference/getting-started.asciidoc +++ b/docs/reference/getting-started.asciidoc @@ -96,7 +96,7 @@ With that out of the way, let's get started with the fun part... == Installation -Elasticsearch requires Java 7. Specifically as of this writing, it is recommended that you use the Oracle JDK version {jdk}. Java installation varies from platform to platform so we won't go into those details here. Suffice to say, before you install Elasticsearch, please check your Java version first by running (and then install/upgrade accordingly if needed): +Elasticsearch requires at least Java 7. Specifically as of this writing, it is recommended that you use the Oracle JDK version {jdk}. Java installation varies from platform to platform so we won't go into those details here. Suffice to say, before you install Elasticsearch, please check your Java version first by running (and then install/upgrade accordingly if needed): [source,sh] -------------------------------------------------- From 87f0d09ace31b57270206e9183b50af183c6af86 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sun, 14 Jun 2015 23:43:45 +0200 Subject: [PATCH 24/43] Add version 1.7.0 it was added in 1.x, but not in master --- core/src/main/java/org/elasticsearch/Version.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/Version.java b/core/src/main/java/org/elasticsearch/Version.java index 7536f23d587..557cd40783c 100644 --- a/core/src/main/java/org/elasticsearch/Version.java +++ b/core/src/main/java/org/elasticsearch/Version.java @@ -242,6 +242,8 @@ public class Version { public static final Version V_1_6_0 = new Version(V_1_6_0_ID, false, org.apache.lucene.util.Version.LUCENE_4_10_4); public static final int V_1_6_1_ID = 1060199; public static final Version V_1_6_1 = new Version(V_1_6_1_ID, true, org.apache.lucene.util.Version.LUCENE_4_10_4); + public static final int V_1_7_0_ID = 1070099; + public static final Version V_1_7_0 = new Version(V_1_7_0_ID, true, org.apache.lucene.util.Version.LUCENE_4_10_4); public static final int V_2_0_0_ID = 2000099; public static final Version V_2_0_0 = new Version(V_2_0_0_ID, true, org.apache.lucene.util.Version.LUCENE_5_2_0); @@ -259,6 +261,8 @@ public class Version { switch (id) { case V_2_0_0_ID: return V_2_0_0; + case V_1_7_0_ID: + return V_1_7_0; case V_1_6_1_ID: return V_1_6_1; case V_1_6_0_ID: From 9210aeacea78f81e2a2bfe846512b5b6a7e0a242 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Mon, 15 Jun 2015 09:40:28 +0200 Subject: [PATCH 25/43] Restore interrupt status when InterruptedException is caught --- .../org/elasticsearch/cluster/InternalClusterInfoService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java b/core/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java index cd170bc7eb7..3cac23d87a5 100644 --- a/core/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java +++ b/core/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java @@ -390,12 +390,14 @@ public class InternalClusterInfoService extends AbstractComponent implements Clu try { nodeLatch.await(fetchTimeout.getMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // restore interrupt status logger.warn("Failed to update node information for ClusterInfoUpdateJob within 15s timeout"); } try { indicesLatch.await(fetchTimeout.getMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // restore interrupt status logger.warn("Failed to update shard information for ClusterInfoUpdateJob within 15s timeout"); } From 43ce27d3bddbcac5798da3698b9c551ea8eab715 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Mon, 15 Jun 2015 10:04:13 +0200 Subject: [PATCH 26/43] [TEST] Shards can also be RELOCATING --- rest-api-spec/test/cat.shards/10_basic.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest-api-spec/test/cat.shards/10_basic.yaml b/rest-api-spec/test/cat.shards/10_basic.yaml index c525cb8475c..5a0d8688449 100755 --- a/rest-api-spec/test/cat.shards/10_basic.yaml +++ b/rest-api-spec/test/cat.shards/10_basic.yaml @@ -93,7 +93,7 @@ - match: $body: | - /^(index1 \s+ \d \s+ (p|r) \s+ ((STARTED|INITIALIZING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){10}$/ + /^(index1 \s+ \d \s+ (p|r) \s+ ((STARTED|INITIALIZING|RELOCATING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){10}$/ - do: indices.create: @@ -112,7 +112,7 @@ v: false - match: $body: | - /^(index(1|2) \s+ \d \s+ (p|r) \s+ ((STARTED|INITIALIZING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){15}$/ + /^(index(1|2) \s+ \d \s+ (p|r) \s+ ((STARTED|INITIALIZING|RELOCATING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){15}$/ - do: cat.shards: @@ -120,7 +120,7 @@ v: false - match: $body: | - /^(index2 \s+ \d \s+ (p|r) \s+ ((STARTED|INITIALIZING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){5}$/ + /^(index2 \s+ \d \s+ (p|r) \s+ ((STARTED|INITIALIZING|RELOCATING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){5}$/ - do: indices.create: @@ -142,5 +142,5 @@ v: false - match: $body: | - /^(index3 \s+ \d \s+ (p|s) \s+ ((STARTED|INITIALIZING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){2}$/ + /^(index3 \s+ \d \s+ (p|s) \s+ ((STARTED|INITIALIZING|RELOCATING) \s+ (\d \s+ (\d+|\d+[.]\d+)(kb|b) \s+)? \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \s+ .+|UNASSIGNED \s+) \n?){2}$/ From ff279275b3072f71011aac2a6b94fda7664073ca Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 15 Jun 2015 10:51:05 +0200 Subject: [PATCH 27/43] Upgrade to Lucene 5.2.1. --- core/src/main/java/org/elasticsearch/Version.java | 2 +- .../org/elasticsearch/common/lucene/all/AllTermQuery.java | 6 +++--- .../index/search/child/ChildrenConstantScoreQuery.java | 2 +- .../org/elasticsearch/index/search/child/ChildrenQuery.java | 2 +- .../index/search/child/ParentConstantScoreQuery.java | 2 +- .../org/elasticsearch/index/search/child/ParentQuery.java | 2 +- .../elasticsearch/search/internal/ContextIndexSearcher.java | 2 +- .../common/lucene/IndexCacheableQueryTests.java | 4 ++-- .../org/elasticsearch/deps/lucene/SimpleLuceneTests.java | 1 + .../java/org/elasticsearch/test/IBMJ9HackThreadFilters.java | 2 +- .../org/elasticsearch/test/engine/MockEngineSupport.java | 4 ++-- pom.xml | 4 ++-- 12 files changed, 17 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/Version.java b/core/src/main/java/org/elasticsearch/Version.java index 557cd40783c..60ea851b065 100644 --- a/core/src/main/java/org/elasticsearch/Version.java +++ b/core/src/main/java/org/elasticsearch/Version.java @@ -245,7 +245,7 @@ public class Version { public static final int V_1_7_0_ID = 1070099; public static final Version V_1_7_0 = new Version(V_1_7_0_ID, true, org.apache.lucene.util.Version.LUCENE_4_10_4); public static final int V_2_0_0_ID = 2000099; - public static final Version V_2_0_0 = new Version(V_2_0_0_ID, true, org.apache.lucene.util.Version.LUCENE_5_2_0); + public static final Version V_2_0_0 = new Version(V_2_0_0_ID, true, org.apache.lucene.util.Version.LUCENE_5_2_1); public static final Version CURRENT = V_2_0_0; diff --git a/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java b/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java index 92e4048e962..f586d152524 100644 --- a/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java +++ b/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java @@ -58,13 +58,13 @@ public final class AllTermQuery extends PayloadTermQuery { public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { // TODO: needsScores // we should be able to just return a regular SpanTermWeight, at most here if needsScores == false? - return new AllTermWeight(this, searcher); + return new AllTermWeight(this, searcher, needsScores); } class AllTermWeight extends PayloadTermWeight { - AllTermWeight(AllTermQuery query, IndexSearcher searcher) throws IOException { - super(query, searcher); + AllTermWeight(AllTermQuery query, IndexSearcher searcher, boolean needsScores) throws IOException { + super(query, searcher, needsScores); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java b/core/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java index 544a4a9a0d7..82ba6265f1c 100644 --- a/core/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java @@ -104,7 +104,7 @@ public class ChildrenConstantScoreQuery extends IndexCacheableQuery { } IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); - indexSearcher.setSimilarity(searcher.getSimilarity()); + indexSearcher.setSimilarity(searcher.getSimilarity(true)); indexSearcher.setQueryCache(null); ParentOrdCollector collector = new ParentOrdCollector(globalIfd, valueCount, parentType); indexSearcher.search(childQuery, collector); diff --git a/core/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java b/core/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java index c04310a120a..ffa2e343251 100644 --- a/core/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java @@ -153,7 +153,7 @@ public final class ChildrenQuery extends IndexCacheableQuery { return new BooleanQuery().createWeight(searcher, needsScores); } IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); - indexSearcher.setSimilarity(searcher.getSimilarity()); + indexSearcher.setSimilarity(searcher.getSimilarity(true)); indexSearcher.setQueryCache(null); boolean abort = true; diff --git a/core/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java b/core/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java index 0beca2b2986..31ad5f66cb9 100644 --- a/core/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java @@ -93,7 +93,7 @@ public class ParentConstantScoreQuery extends IndexCacheableQuery { ParentOrdsCollector collector = new ParentOrdsCollector(globalIfd, maxOrd, parentType); IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); - indexSearcher.setSimilarity(searcher.getSimilarity()); + indexSearcher.setSimilarity(searcher.getSimilarity(true)); indexSearcher.setQueryCache(null); indexSearcher.search(parentQuery, collector); diff --git a/core/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java b/core/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java index cbea2da9736..4f74d9ad73c 100644 --- a/core/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java @@ -130,7 +130,7 @@ public class ParentQuery extends IndexCacheableQuery { try { collector = new ParentOrdAndScoreCollector(sc, globalIfd, parentType); IndexSearcher indexSearcher = new IndexSearcher(sc.searcher().getIndexReader()); - indexSearcher.setSimilarity(searcher.getSimilarity()); + indexSearcher.setSimilarity(searcher.getSimilarity(true)); indexSearcher.setQueryCache(null); indexSearcher.search(parentQuery, collector); if (collector.parentCount() == 0) { diff --git a/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java b/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java index e4c61209825..de7c02162c2 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java +++ b/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java @@ -70,7 +70,7 @@ public class ContextIndexSearcher extends IndexSearcher implements Releasable { super(searcher.reader()); in = searcher.searcher(); this.searchContext = searchContext; - setSimilarity(searcher.searcher().getSimilarity()); + setSimilarity(searcher.searcher().getSimilarity(true)); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/lucene/IndexCacheableQueryTests.java b/core/src/test/java/org/elasticsearch/common/lucene/IndexCacheableQueryTests.java index c96134d9222..330689d1c46 100644 --- a/core/src/test/java/org/elasticsearch/common/lucene/IndexCacheableQueryTests.java +++ b/core/src/test/java/org/elasticsearch/common/lucene/IndexCacheableQueryTests.java @@ -106,7 +106,7 @@ public class IndexCacheableQueryTests extends ElasticsearchTestCase { IndexReader reader = writer.getReader(); // IndexReader wrapping is disabled because of LUCENE-6500. // Add it back when we are on 5.3 - assert Version.LATEST == Version.LUCENE_5_2_0; + assert Version.LATEST == Version.LUCENE_5_2_1; IndexSearcher searcher = newSearcher(reader, false); reader = searcher.getIndexReader(); // reader might be wrapped searcher.setQueryCache(cache); @@ -125,7 +125,7 @@ public class IndexCacheableQueryTests extends ElasticsearchTestCase { IndexReader reader2 = writer.getReader(); // IndexReader wrapping is disabled because of LUCENE-6500. // Add it back when we are on 5.3 - assert Version.LATEST == Version.LUCENE_5_2_0; + assert Version.LATEST == Version.LUCENE_5_2_1; searcher = newSearcher(reader2, false); reader2 = searcher.getIndexReader(); // reader might be wrapped searcher.setQueryCache(cache); diff --git a/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java b/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java index 7158d2e526e..b11409bf942 100644 --- a/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java +++ b/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java @@ -73,6 +73,7 @@ public class SimpleLuceneTests extends ElasticsearchTestCase { indexWriter.prepareCommit(); // Returns null b/c no changes. + // nocommit: this fails assertThat(DirectoryReader.openIfChanged(reader), equalTo(null)); document = new Document(); diff --git a/core/src/test/java/org/elasticsearch/test/IBMJ9HackThreadFilters.java b/core/src/test/java/org/elasticsearch/test/IBMJ9HackThreadFilters.java index 842be2af7e2..45c8277dc02 100644 --- a/core/src/test/java/org/elasticsearch/test/IBMJ9HackThreadFilters.java +++ b/core/src/test/java/org/elasticsearch/test/IBMJ9HackThreadFilters.java @@ -30,7 +30,7 @@ public class IBMJ9HackThreadFilters implements ThreadFilter { static final boolean isJ9; static { - assert Version.LATEST.equals(Version.LUCENE_5_2_0) : "please remove this entire class for 5.3"; + assert Version.LATEST.equals(Version.LUCENE_5_2_1) : "please remove this entire class for 5.3"; isJ9 = Constants.JAVA_VENDOR.startsWith("IBM"); } diff --git a/core/src/test/java/org/elasticsearch/test/engine/MockEngineSupport.java b/core/src/test/java/org/elasticsearch/test/engine/MockEngineSupport.java index b321a0dfbb2..699d13b7082 100644 --- a/core/src/test/java/org/elasticsearch/test/engine/MockEngineSupport.java +++ b/core/src/test/java/org/elasticsearch/test/engine/MockEngineSupport.java @@ -128,7 +128,7 @@ public final class MockEngineSupport { } // this executes basic query checks and asserts that weights are normalized only once etc. final AssertingIndexSearcher assertingIndexSearcher = new AssertingIndexSearcher(mockContext.random, wrappedReader); - assertingIndexSearcher.setSimilarity(searcher.getSimilarity()); + assertingIndexSearcher.setSimilarity(searcher.getSimilarity(true)); assertingIndexSearcher.setQueryCache(filterCache); assertingIndexSearcher.setQueryCachingPolicy(filterCachingPolicy); return assertingIndexSearcher; @@ -180,7 +180,7 @@ public final class MockEngineSupport { public Engine.Searcher wrapSearcher(String source, Engine.Searcher engineSearcher, IndexSearcher searcher, SearcherManager manager) { final AssertingIndexSearcher assertingIndexSearcher = newSearcher(source, searcher, manager); - assertingIndexSearcher.setSimilarity(searcher.getSimilarity()); + assertingIndexSearcher.setSimilarity(searcher.getSimilarity(true)); // pass the original searcher to the super.newSearcher() method to make sure this is the searcher that will // be released later on. If we wrap an index reader here must not pass the wrapped version to the manager // on release otherwise the reader will be closed too early. - good news, stuff will fail all over the place if we don't get this right here diff --git a/pom.xml b/pom.xml index 304a060a6cc..e0e803278f4 100644 --- a/pom.xml +++ b/pom.xml @@ -40,8 +40,8 @@ 1.7 - 5.2.0 - 5.2.0 + 5.2.1 + 5.2.1 2.1.14 2.5.3 1.6.2 From c8f635d42939008d1bf0862571a018a0b8b1f6dd Mon Sep 17 00:00:00 2001 From: Mark Walkom Date: Mon, 15 Jun 2015 13:55:01 +1000 Subject: [PATCH 28/43] Docs: Updated groovy docs link Closes #11656 --- docs/groovy-api/index.asciidoc | 3 +-- docs/reference/modules/scripting.asciidoc | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/groovy-api/index.asciidoc b/docs/groovy-api/index.asciidoc index 5e06cd1f2f4..7869c5fa19f 100644 --- a/docs/groovy-api/index.asciidoc +++ b/docs/groovy-api/index.asciidoc @@ -5,7 +5,7 @@ [preface] == Preface -This section describes the http://groovy.codehaus.org/[Groovy] API +This section describes the http://groovy-lang.org/[Groovy] API elasticsearch provides. All elasticsearch APIs are executed using a <>, and are completely asynchronous in nature (they either accept a listener, or return a @@ -48,4 +48,3 @@ include::delete.asciidoc[] include::search.asciidoc[] include::count.asciidoc[] - diff --git a/docs/reference/modules/scripting.asciidoc b/docs/reference/modules/scripting.asciidoc index 3121388f43f..a8960dfaf88 100644 --- a/docs/reference/modules/scripting.asciidoc +++ b/docs/reference/modules/scripting.asciidoc @@ -6,7 +6,7 @@ expressions. For example, scripts can be used to return "script fields" as part of a search request, or can be used to evaluate a custom score for a query and so on. -The scripting module uses by default http://groovy.codehaus.org/[groovy] +The scripting module uses by default http://groovy-lang.org/[groovy] (previously http://mvel.codehaus.org/[mvel] in 1.3.x and earlier) as the scripting language with some extensions. Groovy is used since it is extremely fast and very simple to use. @@ -68,7 +68,7 @@ GET /_search Additional `lang` plugins are provided to allow to execute scripts in different languages. All places where a script can be used, a `lang` parameter -can be provided to define the language of the script. The following are the +can be provided to define the language of the script. The following are the supported scripting languages: [cols="<,<,<",options="header",] @@ -162,7 +162,7 @@ curl -XPOST localhost:9200/_scripts/groovy/indexedCalculateScore -d '{ This will create a document with id: `indexedCalculateScore` and type: `groovy` in the `.scripts` index. The type of the document is the language used by the script. -This script can be accessed at query time by using the `id` script parameter and passing +This script can be accessed at query time by using the `id` script parameter and passing the script id: [source,js] @@ -636,4 +636,3 @@ integer with the value of `8`, the result is `0` even though you were expecting it to be `0.125`. You may need to enforce precision by explicitly using a double like `1.0/num` in order to get the expected result. - From 36cab80cc81c3032536bc4b87082f4ac5ea35222 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 15 Jun 2015 11:26:24 +0200 Subject: [PATCH 29/43] Remove SimpleLuceneTests.testAddDocAfterPrepareCommit. It is unnecessary to test features we are not using. --- .../deps/lucene/SimpleLuceneTests.java | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java b/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java index b11409bf942..5929c7b5512 100644 --- a/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java +++ b/core/src/test/java/org/elasticsearch/deps/lucene/SimpleLuceneTests.java @@ -61,29 +61,6 @@ public class SimpleLuceneTests extends ElasticsearchTestCase { } } - @Test - public void testAddDocAfterPrepareCommit() throws Exception { - Directory dir = new RAMDirectory(); - IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)); - Document document = new Document(); - document.add(new TextField("_id", "1", Field.Store.YES)); - indexWriter.addDocument(document); - DirectoryReader reader = DirectoryReader.open(indexWriter, true); - assertThat(reader.numDocs(), equalTo(1)); - - indexWriter.prepareCommit(); - // Returns null b/c no changes. - // nocommit: this fails - assertThat(DirectoryReader.openIfChanged(reader), equalTo(null)); - - document = new Document(); - document.add(new TextField("_id", "2", Field.Store.YES)); - indexWriter.addDocument(document); - indexWriter.commit(); - reader = DirectoryReader.openIfChanged(reader); - assertThat(reader.numDocs(), equalTo(2)); - } - @Test public void testSimpleNumericOps() throws Exception { Directory dir = new RAMDirectory(); From 64ec18afa03fed36e57bf0951256afc559948059 Mon Sep 17 00:00:00 2001 From: Clinton Gormley Date: Mon, 15 Jun 2015 11:29:17 +0200 Subject: [PATCH 30/43] Merge pull request #11661 from pjcard/patch-1 Make explicit the requirement for intervals to be integers Conflicts: docs/reference/search/aggregations/bucket/histogram-aggregation.asciidoc --- .../aggregations/bucket/histogram-aggregation.asciidoc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/reference/aggregations/bucket/histogram-aggregation.asciidoc b/docs/reference/aggregations/bucket/histogram-aggregation.asciidoc index 99d7950e7de..9a553587da8 100644 --- a/docs/reference/aggregations/bucket/histogram-aggregation.asciidoc +++ b/docs/reference/aggregations/bucket/histogram-aggregation.asciidoc @@ -18,6 +18,8 @@ if (rem < 0) { bucket_key = value - rem -------------------------------------------------- +From the rounding function above it can be seen that the intervals themsevles **must** be integers. + The following snippet "buckets" the products based on their `price` by interval of `50`: [source,js] @@ -339,4 +341,4 @@ had a value. } -------------------------------------------------- -<1> Documents without a value in the `quantity` field will fall into the same bucket as documents that have the value `0`. +<1> Documents without a value in the `quantity` field will fall into the same bucket as documents that have the value `0`. From 14c9c239bcf27ac9ce1dd0ed64f978d689f63176 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 15 Jun 2015 14:05:23 +0200 Subject: [PATCH 31/43] Remove non-default fielddata formats. Now that doc values are the default for fielddata, specialized in-memory formats are becoming an esoteric option. This commit removes such formats: - `fst` on string fields, - `compressed` on geo points. I also removed documentation and tests that the fielddata cache is shared if you change the format, since this is only true for in-memory fielddata formats (given that for doc values, the caching is done directly in Lucene). --- .../fielddata/IndexFieldDataService.java | 5 - .../plain/DisabledIndexFieldData.java | 1 - .../plain/FSTBytesAtomicFieldData.java | 121 ------------- .../plain/FSTBytesIndexFieldData.java | 116 ------------ .../GeoPointCompressedAtomicFieldData.java | 169 ------------------ .../GeoPointCompressedIndexFieldData.java | 157 ---------------- .../index/fielddata/DuelFieldDataTests.java | 4 - .../FSTPackedBytesStringFieldDataTests.java | 33 ---- .../index/fielddata/FilterFieldDataTest.java | 4 +- .../fielddata/IndexFieldDataServiceTests.java | 19 +- .../index/mapper/geo/GeoMappingTests.java | 80 --------- .../mapper/multifield/MultiFieldTests.java | 1 - .../string/SimpleStringMappingTests.java | 2 +- .../update/all_mapping_create_index.json | 2 +- .../RandomExceptionCircuitBreakerTests.java | 3 - .../test/ElasticsearchIntegrationTest.java | 9 - .../index-modules/fielddata.asciidoc | 28 ++- 17 files changed, 19 insertions(+), 735 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesAtomicFieldData.java delete mode 100644 core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesIndexFieldData.java delete mode 100644 core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedAtomicFieldData.java delete mode 100644 core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedIndexFieldData.java delete mode 100644 core/src/test/java/org/elasticsearch/index/fielddata/FSTPackedBytesStringFieldDataTests.java delete mode 100644 core/src/test/java/org/elasticsearch/index/mapper/geo/GeoMappingTests.java diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java b/core/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java index 4fed6b6c857..f851420e036 100644 --- a/core/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java +++ b/core/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java @@ -31,7 +31,6 @@ import org.elasticsearch.common.util.concurrent.KeyedLock; import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.fielddata.plain.*; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.core.BooleanFieldMapper; import org.elasticsearch.index.mapper.internal.IndexFieldMapper; @@ -60,8 +59,6 @@ public class IndexFieldDataService extends AbstractIndexComponent { private static final String DOC_VALUES_FORMAT = "doc_values"; private static final String ARRAY_FORMAT = "array"; private static final String PAGED_BYTES_FORMAT = "paged_bytes"; - private static final String FST_FORMAT = "fst"; - private static final String COMPRESSED_FORMAT = "compressed"; private final static ImmutableMap buildersByType; private final static ImmutableMap docValuesBuildersByType; @@ -99,7 +96,6 @@ public class IndexFieldDataService extends AbstractIndexComponent { buildersByTypeAndFormat = MapBuilder., IndexFieldData.Builder>newMapBuilder() .put(Tuple.tuple("string", PAGED_BYTES_FORMAT), new PagedBytesIndexFieldData.Builder()) - .put(Tuple.tuple("string", FST_FORMAT), new FSTBytesIndexFieldData.Builder()) .put(Tuple.tuple("string", DOC_VALUES_FORMAT), new DocValuesIndexFieldData.Builder()) .put(Tuple.tuple("string", DISABLED_FORMAT), new DisabledIndexFieldData.Builder()) @@ -130,7 +126,6 @@ public class IndexFieldDataService extends AbstractIndexComponent { .put(Tuple.tuple("geo_point", ARRAY_FORMAT), new GeoPointDoubleArrayIndexFieldData.Builder()) .put(Tuple.tuple("geo_point", DOC_VALUES_FORMAT), new GeoPointBinaryDVIndexFieldData.Builder()) .put(Tuple.tuple("geo_point", DISABLED_FORMAT), new DisabledIndexFieldData.Builder()) - .put(Tuple.tuple("geo_point", COMPRESSED_FORMAT), new GeoPointCompressedIndexFieldData.Builder()) .put(Tuple.tuple("binary", DOC_VALUES_FORMAT), new BytesBinaryDVIndexFieldData.Builder()) .put(Tuple.tuple("binary", DISABLED_FORMAT), new DisabledIndexFieldData.Builder()) diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/DisabledIndexFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/DisabledIndexFieldData.java index e86ff0b1d9b..e8d37ff476f 100644 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/DisabledIndexFieldData.java +++ b/core/src/main/java/org/elasticsearch/index/fielddata/plain/DisabledIndexFieldData.java @@ -24,7 +24,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.fielddata.*; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType.Names; import org.elasticsearch.index.mapper.MapperService; diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesAtomicFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesAtomicFieldData.java deleted file mode 100644 index e617c8005f1..00000000000 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesAtomicFieldData.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.fielddata.plain; - -import org.apache.lucene.index.RandomAccessOrds; -import org.apache.lucene.index.SortedSetDocValues; -import org.apache.lucene.util.Accountable; -import org.apache.lucene.util.Accountables; -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.BytesRefBuilder; -import org.apache.lucene.util.IntsRef; -import org.apache.lucene.util.IntsRefBuilder; -import org.apache.lucene.util.fst.FST; -import org.apache.lucene.util.fst.FST.Arc; -import org.apache.lucene.util.fst.FST.BytesReader; -import org.apache.lucene.util.fst.Util; -import org.elasticsearch.index.fielddata.ordinals.Ordinals; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -/** - */ -public class FSTBytesAtomicFieldData extends AbstractAtomicOrdinalsFieldData { - - // 0 ordinal in values means no value (its null) - protected final Ordinals ordinals; - - private long size = -1; - - private final FST fst; - - public FSTBytesAtomicFieldData(FST fst, Ordinals ordinals) { - this.ordinals = ordinals; - this.fst = fst; - } - - @Override - public void close() { - } - - @Override - public long ramBytesUsed() { - if (size == -1) { - long size = ordinals.ramBytesUsed(); - // FST - size += fst == null ? 0 : fst.ramBytesUsed(); - this.size = size; - } - return size; - } - - @Override - public Collection getChildResources() { - List resources = new ArrayList<>(); - resources.add(Accountables.namedAccountable("ordinals", ordinals)); - if (fst != null) { - resources.add(Accountables.namedAccountable("terms", fst)); - } - return Collections.unmodifiableList(resources); - } - - @Override - public RandomAccessOrds getOrdinalsValues() { - return ordinals.ordinals(new ValuesHolder(fst)); - } - - private static class ValuesHolder implements Ordinals.ValuesHolder { - - private final FST fst; - - // per-thread resources - private final BytesRefBuilder scratch; - protected final BytesReader in; - protected final Arc firstArc = new Arc<>(); - protected final Arc scratchArc = new Arc<>(); - protected final IntsRefBuilder scratchInts = new IntsRefBuilder(); - - ValuesHolder(FST fst) { - this.fst = fst; - scratch = new BytesRefBuilder(); - in = fst.getBytesReader(); - } - - @Override - public BytesRef lookupOrd(long ord) { - assert ord != SortedSetDocValues.NO_MORE_ORDS; - in.setPosition(0); - fst.getFirstArc(firstArc); - try { - IntsRef output = Util.getByOutput(fst, ord, in, firstArc, scratchArc, scratchInts); - scratch.clear(); - scratch.grow(output.length); - Util.toBytesRef(output, scratch); - } catch (IOException ex) { - //bogus - } - return scratch.get(); - } - } - -} diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesIndexFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesIndexFieldData.java deleted file mode 100644 index f0d4f1fde9a..00000000000 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/FSTBytesIndexFieldData.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.fielddata.plain; - -import org.apache.lucene.index.*; -import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.IntsRefBuilder; -import org.apache.lucene.util.fst.FST; -import org.apache.lucene.util.fst.FST.INPUT_TYPE; -import org.apache.lucene.util.fst.PositiveIntOutputs; -import org.apache.lucene.util.fst.Util; -import org.elasticsearch.common.breaker.CircuitBreaker; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.fielddata.*; -import org.elasticsearch.index.fielddata.ordinals.Ordinals; -import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder; -import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.settings.IndexSettings; -import org.elasticsearch.indices.breaker.CircuitBreakerService; - -/** - */ -public class FSTBytesIndexFieldData extends AbstractIndexOrdinalsFieldData { - - private final CircuitBreakerService breakerService; - - public static class Builder implements IndexFieldData.Builder { - - @Override - public IndexOrdinalsFieldData build(Index index, @IndexSettings Settings indexSettings, MappedFieldType fieldType, - IndexFieldDataCache cache, CircuitBreakerService breakerService, MapperService mapperService) { - return new FSTBytesIndexFieldData(index, indexSettings, fieldType.names(), fieldType.fieldDataType(), cache, breakerService); - } - } - - FSTBytesIndexFieldData(Index index, @IndexSettings Settings indexSettings, MappedFieldType.Names fieldNames, FieldDataType fieldDataType, - IndexFieldDataCache cache, CircuitBreakerService breakerService) { - super(index, indexSettings, fieldNames, fieldDataType, cache, breakerService); - this.breakerService = breakerService; - } - - @Override - public AtomicOrdinalsFieldData loadDirect(LeafReaderContext context) throws Exception { - LeafReader reader = context.reader(); - - Terms terms = reader.terms(getFieldNames().indexName()); - AtomicOrdinalsFieldData data = null; - // TODO: Use an actual estimator to estimate before loading. - NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA)); - if (terms == null) { - data = AbstractAtomicOrdinalsFieldData.empty(); - estimator.afterLoad(null, data.ramBytesUsed()); - return data; - } - PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton(); - org.apache.lucene.util.fst.Builder fstBuilder = new org.apache.lucene.util.fst.Builder<>(INPUT_TYPE.BYTE1, outputs); - final IntsRefBuilder scratch = new IntsRefBuilder(); - - final long numTerms; - if (regex == null && frequency == null) { - numTerms = terms.size(); - } else { - numTerms = -1; - } - final float acceptableTransientOverheadRatio = fieldDataType.getSettings().getAsFloat("acceptable_transient_overhead_ratio", OrdinalsBuilder.DEFAULT_ACCEPTABLE_OVERHEAD_RATIO); - boolean success = false; - try (OrdinalsBuilder builder = new OrdinalsBuilder(numTerms, reader.maxDoc(), acceptableTransientOverheadRatio)) { - - // we don't store an ord 0 in the FST since we could have an empty string in there and FST don't support - // empty strings twice. ie. them merge fails for long output. - TermsEnum termsEnum = filter(terms, reader); - PostingsEnum docsEnum = null; - for (BytesRef term = termsEnum.next(); term != null; term = termsEnum.next()) { - final long termOrd = builder.nextOrdinal(); - fstBuilder.add(Util.toIntsRef(term, scratch), (long) termOrd); - docsEnum = termsEnum.postings(null, docsEnum, PostingsEnum.NONE); - for (int docId = docsEnum.nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = docsEnum.nextDoc()) { - builder.addDoc(docId); - } - } - - FST fst = fstBuilder.finish(); - - final Ordinals ordinals = builder.build(fieldDataType.getSettings()); - - data = new FSTBytesAtomicFieldData(fst, ordinals); - success = true; - return data; - } finally { - if (success) { - estimator.afterLoad(null, data.ramBytesUsed()); - } - - } - } -} diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedAtomicFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedAtomicFieldData.java deleted file mode 100644 index 2a5621b58af..00000000000 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedAtomicFieldData.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.fielddata.plain; - -import org.apache.lucene.index.DocValues; -import org.apache.lucene.index.RandomAccessOrds; -import org.apache.lucene.index.SortedDocValues; -import org.apache.lucene.util.Accountable; -import org.apache.lucene.util.Accountables; -import org.apache.lucene.util.BitSet; -import org.apache.lucene.util.RamUsageEstimator; -import org.apache.lucene.util.packed.PagedMutable; -import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.index.fielddata.FieldData; -import org.elasticsearch.index.fielddata.GeoPointValues; -import org.elasticsearch.index.fielddata.MultiGeoPointValues; -import org.elasticsearch.index.fielddata.ordinals.Ordinals; -import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -/** - * Field data atomic impl for geo points with lossy compression. - */ -public abstract class GeoPointCompressedAtomicFieldData extends AbstractAtomicGeoPointFieldData { - - @Override - public void close() { - } - - static class WithOrdinals extends GeoPointCompressedAtomicFieldData { - - private final GeoPointFieldMapper.Encoding encoding; - private final PagedMutable lon, lat; - private final Ordinals ordinals; - private final int maxDoc; - - public WithOrdinals(GeoPointFieldMapper.Encoding encoding, PagedMutable lon, PagedMutable lat, Ordinals ordinals, int maxDoc) { - super(); - this.encoding = encoding; - this.lon = lon; - this.lat = lat; - this.ordinals = ordinals; - this.maxDoc = maxDoc; - } - - @Override - public long ramBytesUsed() { - return RamUsageEstimator.NUM_BYTES_INT/*size*/ + lon.ramBytesUsed() + lat.ramBytesUsed(); - } - - @Override - public Collection getChildResources() { - List resources = new ArrayList<>(); - resources.add(Accountables.namedAccountable("latitude", lat)); - resources.add(Accountables.namedAccountable("longitude", lon)); - return Collections.unmodifiableList(resources); - } - - @Override - public MultiGeoPointValues getGeoPointValues() { - final RandomAccessOrds ords = ordinals.ordinals(); - final SortedDocValues singleOrds = DocValues.unwrapSingleton(ords); - if (singleOrds != null) { - final GeoPoint point = new GeoPoint(); - final GeoPointValues values = new GeoPointValues() { - @Override - public GeoPoint get(int docID) { - final int ord = singleOrds.getOrd(docID); - if (ord >= 0) { - encoding.decode(lat.get(ord), lon.get(ord), point); - } else { - point.reset(0, 0); - } - return point; - } - }; - return FieldData.singleton(values, DocValues.docsWithValue(singleOrds, maxDoc)); - } else { - final GeoPoint point = new GeoPoint(); - return new MultiGeoPointValues() { - - @Override - public GeoPoint valueAt(int index) { - final long ord = ords.ordAt(index); - encoding.decode(lat.get(ord), lon.get(ord), point); - return point; - } - - @Override - public void setDocument(int docId) { - ords.setDocument(docId); - } - - @Override - public int count() { - return ords.cardinality(); - } - }; - } - } - } - - /** - * Assumes unset values are marked in bitset, and docId is used as the index to the value array. - */ - public static class Single extends GeoPointCompressedAtomicFieldData { - - private final GeoPointFieldMapper.Encoding encoding; - private final PagedMutable lon, lat; - private final BitSet set; - - public Single(GeoPointFieldMapper.Encoding encoding, PagedMutable lon, PagedMutable lat, BitSet set) { - super(); - this.encoding = encoding; - this.lon = lon; - this.lat = lat; - this.set = set; - } - - @Override - public long ramBytesUsed() { - return RamUsageEstimator.NUM_BYTES_INT/*size*/ + lon.ramBytesUsed() + lat.ramBytesUsed() + (set == null ? 0 : set.ramBytesUsed()); - } - - @Override - public Collection getChildResources() { - List resources = new ArrayList<>(); - resources.add(Accountables.namedAccountable("latitude", lat)); - resources.add(Accountables.namedAccountable("longitude", lon)); - if (set != null) { - resources.add(Accountables.namedAccountable("missing bitset", set)); - } - return Collections.unmodifiableList(resources); - } - - @Override - public MultiGeoPointValues getGeoPointValues() { - final GeoPoint point = new GeoPoint(); - final GeoPointValues values = new GeoPointValues() { - @Override - public GeoPoint get(int docID) { - encoding.decode(lat.get(docID), lon.get(docID), point); - return point; - } - }; - return FieldData.singleton(values, set); - } - } -} \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedIndexFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedIndexFieldData.java deleted file mode 100644 index 2d38c714464..00000000000 --- a/core/src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointCompressedIndexFieldData.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.fielddata.plain; - -import org.apache.lucene.index.LeafReader; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.RandomAccessOrds; -import org.apache.lucene.index.Terms; -import org.apache.lucene.util.BitSet; -import org.apache.lucene.util.packed.PackedInts; -import org.apache.lucene.util.packed.PagedMutable; -import org.elasticsearch.common.breaker.CircuitBreaker; -import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.DistanceUnit; -import org.elasticsearch.common.unit.DistanceUnit.Distance; -import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.fielddata.*; -import org.elasticsearch.index.fielddata.ordinals.Ordinals; -import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder; -import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; -import org.elasticsearch.index.settings.IndexSettings; -import org.elasticsearch.indices.breaker.CircuitBreakerService; - -/** - */ -public class GeoPointCompressedIndexFieldData extends AbstractIndexGeoPointFieldData { - - private static final String PRECISION_KEY = "precision"; - private static final Distance DEFAULT_PRECISION_VALUE = new Distance(1, DistanceUnit.CENTIMETERS); - private final CircuitBreakerService breakerService; - - public static class Builder implements IndexFieldData.Builder { - - @Override - public IndexFieldData build(Index index, @IndexSettings Settings indexSettings, MappedFieldType fieldType, IndexFieldDataCache cache, - CircuitBreakerService breakerService, MapperService mapperService) { - FieldDataType type = fieldType.fieldDataType(); - final String precisionAsString = type.getSettings().get(PRECISION_KEY); - final Distance precision; - if (precisionAsString != null) { - precision = Distance.parseDistance(precisionAsString); - } else { - precision = DEFAULT_PRECISION_VALUE; - } - return new GeoPointCompressedIndexFieldData(index, indexSettings, fieldType.names(), fieldType.fieldDataType(), cache, precision, breakerService); - } - } - - private final GeoPointFieldMapper.Encoding encoding; - - public GeoPointCompressedIndexFieldData(Index index, @IndexSettings Settings indexSettings, MappedFieldType.Names fieldNames, - FieldDataType fieldDataType, IndexFieldDataCache cache, Distance precision, - CircuitBreakerService breakerService) { - super(index, indexSettings, fieldNames, fieldDataType, cache); - this.encoding = GeoPointFieldMapper.Encoding.of(precision); - this.breakerService = breakerService; - } - - @Override - public AtomicGeoPointFieldData loadDirect(LeafReaderContext context) throws Exception { - LeafReader reader = context.reader(); - - Terms terms = reader.terms(getFieldNames().indexName()); - AtomicGeoPointFieldData data = null; - // TODO: Use an actual estimator to estimate before loading. - NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA)); - if (terms == null) { - data = AbstractAtomicGeoPointFieldData.empty(reader.maxDoc()); - estimator.afterLoad(null, data.ramBytesUsed()); - return data; - } - final long initialSize; - if (terms.size() >= 0) { - initialSize = 1 + terms.size(); - } else { // codec doesn't expose size - initialSize = 1 + Math.min(1 << 12, reader.maxDoc()); - } - final int pageSize = Integer.highestOneBit(BigArrays.PAGE_SIZE_IN_BYTES * 8 / encoding.numBitsPerCoordinate() - 1) << 1; - PagedMutable lat = new PagedMutable(initialSize, pageSize, encoding.numBitsPerCoordinate(), PackedInts.COMPACT); - PagedMutable lon = new PagedMutable(initialSize, pageSize, encoding.numBitsPerCoordinate(), PackedInts.COMPACT); - final float acceptableTransientOverheadRatio = fieldDataType.getSettings().getAsFloat("acceptable_transient_overhead_ratio", OrdinalsBuilder.DEFAULT_ACCEPTABLE_OVERHEAD_RATIO); - boolean success = false; - try (OrdinalsBuilder builder = new OrdinalsBuilder(terms.size(), reader.maxDoc(), acceptableTransientOverheadRatio)) { - final GeoPointEnum iter = new GeoPointEnum(builder.buildFromTerms(terms.iterator())); - GeoPoint point; - while ((point = iter.next()) != null) { - final long ord = builder.currentOrdinal(); - if (lat.size() <= ord) { - final long newSize = BigArrays.overSize(ord + 1); - lat = lat.resize(newSize); - lon = lon.resize(newSize); - } - lat.set(ord, encoding.encodeCoordinate(point.getLat())); - lon.set(ord, encoding.encodeCoordinate(point.getLon())); - } - - Ordinals build = builder.build(fieldDataType.getSettings()); - RandomAccessOrds ordinals = build.ordinals(); - if (FieldData.isMultiValued(ordinals) || CommonSettings.getMemoryStorageHint(fieldDataType) == CommonSettings.MemoryStorageFormat.ORDINALS) { - if (lat.size() != ordinals.getValueCount()) { - lat = lat.resize(ordinals.getValueCount()); - lon = lon.resize(ordinals.getValueCount()); - } - data = new GeoPointCompressedAtomicFieldData.WithOrdinals(encoding, lon, lat, build, reader.maxDoc()); - } else { - int maxDoc = reader.maxDoc(); - PagedMutable sLat = new PagedMutable(reader.maxDoc(), pageSize, encoding.numBitsPerCoordinate(), PackedInts.COMPACT); - PagedMutable sLon = new PagedMutable(reader.maxDoc(), pageSize, encoding.numBitsPerCoordinate(), PackedInts.COMPACT); - final long missing = encoding.encodeCoordinate(0); - for (int i = 0; i < maxDoc; i++) { - ordinals.setDocument(i); - final long nativeOrdinal = ordinals.nextOrd(); - if (nativeOrdinal >= 0) { - sLat.set(i, lat.get(nativeOrdinal)); - sLon.set(i, lon.get(nativeOrdinal)); - } else { - sLat.set(i, missing); - sLon.set(i, missing); - } - } - BitSet set = builder.buildDocsWithValuesSet(); - data = new GeoPointCompressedAtomicFieldData.Single(encoding, sLon, sLat, set); - } - success = true; - return data; - } finally { - if (success) { - estimator.afterLoad(null, data.ramBytesUsed()); - } - - } - - } - - -} \ No newline at end of file diff --git a/core/src/test/java/org/elasticsearch/index/fielddata/DuelFieldDataTests.java b/core/src/test/java/org/elasticsearch/index/fielddata/DuelFieldDataTests.java index 1b3168af985..2fe127dd796 100644 --- a/core/src/test/java/org/elasticsearch/index/fielddata/DuelFieldDataTests.java +++ b/core/src/test/java/org/elasticsearch/index/fielddata/DuelFieldDataTests.java @@ -87,7 +87,6 @@ public class DuelFieldDataTests extends AbstractFieldDataTests { } LeafReaderContext context = refreshReader(); Map typeMap = new HashMap<>(); - typeMap.put(new FieldDataType("string", Settings.builder().put("format", "fst")), Type.Bytes); typeMap.put(new FieldDataType("string", Settings.builder().put("format", "paged_bytes")), Type.Bytes); typeMap.put(new FieldDataType("byte", Settings.builder().put("format", "array")), Type.Integer); typeMap.put(new FieldDataType("short", Settings.builder().put("format", "array")), Type.Integer); @@ -325,7 +324,6 @@ public class DuelFieldDataTests extends AbstractFieldDataTests { } LeafReaderContext context = refreshReader(); Map typeMap = new HashMap<>(); - typeMap.put(new FieldDataType("string", Settings.builder().put("format", "fst")), Type.Bytes); typeMap.put(new FieldDataType("string", Settings.builder().put("format", "paged_bytes")), Type.Bytes); typeMap.put(new FieldDataType("string", Settings.builder().put("format", "doc_values")), Type.Bytes); // TODO add filters @@ -384,7 +382,6 @@ public class DuelFieldDataTests extends AbstractFieldDataTests { refreshReader(); Map typeMap = new HashMap(); - typeMap.put(new FieldDataType("string", Settings.builder().put("format", "fst")), Type.Bytes); typeMap.put(new FieldDataType("string", Settings.builder().put("format", "paged_bytes")), Type.Bytes); typeMap.put(new FieldDataType("string", Settings.builder().put("format", "doc_values")), Type.Bytes); @@ -437,7 +434,6 @@ public class DuelFieldDataTests extends AbstractFieldDataTests { Map typeMap = new HashMap<>(); final Distance precision = new Distance(1, randomFrom(DistanceUnit.values())); typeMap.put(new FieldDataType("geo_point", Settings.builder().put("format", "array")), Type.GeoPoint); - typeMap.put(new FieldDataType("geo_point", Settings.builder().put("format", "compressed").put("precision", precision)), Type.GeoPoint); typeMap.put(new FieldDataType("geo_point", Settings.builder().put("format", "doc_values")), Type.GeoPoint); ArrayList> list = new ArrayList<>(typeMap.entrySet()); diff --git a/core/src/test/java/org/elasticsearch/index/fielddata/FSTPackedBytesStringFieldDataTests.java b/core/src/test/java/org/elasticsearch/index/fielddata/FSTPackedBytesStringFieldDataTests.java deleted file mode 100644 index a64ab8d98bd..00000000000 --- a/core/src/test/java/org/elasticsearch/index/fielddata/FSTPackedBytesStringFieldDataTests.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.fielddata; - -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder; - -/** - */ -public class FSTPackedBytesStringFieldDataTests extends AbstractStringFieldDataTests { - - @Override - protected FieldDataType getFieldDataType() { - return new FieldDataType("string", Settings.builder().put("format", "fst").put(OrdinalsBuilder.FORCE_MULTI_ORDINALS, randomBoolean())); - } -} diff --git a/core/src/test/java/org/elasticsearch/index/fielddata/FilterFieldDataTest.java b/core/src/test/java/org/elasticsearch/index/fielddata/FilterFieldDataTest.java index 3c13999eb37..e4b28223389 100644 --- a/core/src/test/java/org/elasticsearch/index/fielddata/FilterFieldDataTest.java +++ b/core/src/test/java/org/elasticsearch/index/fielddata/FilterFieldDataTest.java @@ -60,7 +60,7 @@ public class FilterFieldDataTest extends AbstractFieldDataTests { } writer.forceMerge(1, true); LeafReaderContext context = refreshReader(); - String[] formats = new String[] { "fst", "paged_bytes"}; + String[] formats = new String[] { "paged_bytes"}; for (String format : formats) { { @@ -153,7 +153,7 @@ public class FilterFieldDataTest extends AbstractFieldDataTests { logger.debug(hundred + " " + ten + " " + five); writer.forceMerge(1, true); LeafReaderContext context = refreshReader(); - String[] formats = new String[] { "fst", "paged_bytes"}; + String[] formats = new String[] { "paged_bytes"}; for (String format : formats) { { ifdService.clear(); diff --git a/core/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java b/core/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java index abccb4851c9..3af20da91ed 100644 --- a/core/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java +++ b/core/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java @@ -28,7 +28,6 @@ import org.apache.lucene.store.RAMDirectory; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.fielddata.plain.*; import org.elasticsearch.index.mapper.ContentPath; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper.BuilderContext; import org.elasticsearch.index.mapper.MapperBuilders; @@ -101,10 +100,10 @@ public class IndexFieldDataServiceTests extends ElasticsearchSingleNodeTest { final IndexService indexService = createIndex("test"); final IndexFieldDataService ifdService = indexService.fieldData(); final BuilderContext ctx = new BuilderContext(indexService.settingsService().getSettings(), new ContentPath(1)); - final MappedFieldType stringMapper = MapperBuilders.stringField("string").tokenized(false).fieldDataSettings(DOC_VALUES_SETTINGS).fieldDataSettings(Settings.builder().put("format", "fst").build()).build(ctx).fieldType(); + final MappedFieldType stringMapper = MapperBuilders.stringField("string").tokenized(false).fieldDataSettings(DOC_VALUES_SETTINGS).fieldDataSettings(Settings.builder().put("format", "disabled").build()).build(ctx).fieldType(); ifdService.clear(); IndexFieldData fd = ifdService.getForField(stringMapper); - assertTrue(fd instanceof FSTBytesIndexFieldData); + assertTrue(fd instanceof DisabledIndexFieldData); final Settings fdSettings = Settings.builder().put("format", "array").build(); for (MappedFieldType mapper : Arrays.asList( @@ -133,7 +132,7 @@ public class IndexFieldDataServiceTests extends ElasticsearchSingleNodeTest { final IndexService indexService = createIndex("test"); final IndexFieldDataService ifdService = indexService.fieldData(); final BuilderContext ctx = new BuilderContext(indexService.settingsService().getSettings(), new ContentPath(1)); - final MappedFieldType mapper1 = MapperBuilders.stringField("s").tokenized(false).fieldDataSettings(Settings.builder().put(FieldDataType.FORMAT_KEY, "paged_bytes").build()).build(ctx).fieldType(); + final MappedFieldType mapper1 = MapperBuilders.stringField("s").tokenized(false).docValues(true).fieldDataSettings(Settings.builder().put(FieldDataType.FORMAT_KEY, "paged_bytes").build()).build(ctx).fieldType(); final IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new KeywordAnalyzer())); Document doc = new Document(); doc.add(new StringField("s", "thisisastring", Store.NO)); @@ -150,18 +149,10 @@ public class IndexFieldDataServiceTests extends ElasticsearchSingleNodeTest { // write new segment writer.addDocument(doc); final IndexReader reader2 = DirectoryReader.open(writer, true); - final MappedFieldType mapper2 = MapperBuilders.stringField("s").tokenized(false).fieldDataSettings(Settings.builder().put(FieldDataType.FORMAT_KEY, "fst").build()).build(ctx).fieldType(); + final MappedFieldType mapper2 = MapperBuilders.stringField("s").tokenized(false).docValues(true).fieldDataSettings(Settings.builder().put(FieldDataType.FORMAT_KEY, "doc_values").build()).build(ctx).fieldType(); ifdService.onMappingUpdate(); ifd = ifdService.getForField(mapper2); - assertThat(ifd, instanceOf(FSTBytesIndexFieldData.class)); - for (LeafReaderContext arc : reader2.leaves()) { - AtomicFieldData afd = ifd.load(arc); - if (oldSegments.contains(arc.reader())) { - assertThat(afd, instanceOf(PagedBytesAtomicFieldData.class)); - } else { - assertThat(afd, instanceOf(FSTBytesAtomicFieldData.class)); - } - } + assertThat(ifd, instanceOf(SortedSetDVOrdinalsIndexFieldData.class)); reader1.close(); reader2.close(); writer.close(); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoMappingTests.java deleted file mode 100644 index 0c292d9779d..00000000000 --- a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoMappingTests.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper.geo; - -import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; -import org.elasticsearch.cluster.metadata.MappingMetaData; -import org.elasticsearch.common.collect.ImmutableOpenMap; -import org.elasticsearch.common.unit.DistanceUnit; -import org.elasticsearch.common.unit.DistanceUnit.Distance; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.test.ElasticsearchIntegrationTest; - -import java.util.Map; - -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; - -public class GeoMappingTests extends ElasticsearchIntegrationTest { - - public void testUpdatePrecision() throws Exception { - assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject() - .startObject("type1") - .startObject("properties") - .startObject("pin") - .field("type", "geo_point") - .startObject("fielddata") - .field("format", "compressed") - .field("precision", "2mm") - .endObject() - .endObject() - .endObject() - .endObject() - .endObject()).get()); - ensureYellow(); - assertPrecision(new Distance(2, DistanceUnit.MILLIMETERS)); - - assertAcked(client().admin().indices().preparePutMapping("test").setType("type1").setSource(XContentFactory.jsonBuilder().startObject() - .startObject("type1") - .startObject("properties") - .startObject("pin") - .field("type", "geo_point") - .startObject("fielddata") - .field("format", "compressed") - .field("precision", "11m") - .endObject() - .endObject() - .endObject() - .endObject() - .endObject()).get()); - - assertPrecision(new Distance(11, DistanceUnit.METERS)); - } - - private void assertPrecision(Distance expected) throws Exception { - ImmutableOpenMap> mappings = client().admin().indices().getMappings(new GetMappingsRequest().indices("test").types("type1")).actionGet().getMappings(); - assertNotNull(mappings); - Map properties = (Map) mappings.get("test").get("type1").getSourceAsMap().get("properties"); - Map pinProperties = (Map) properties.get("pin"); - Map pinFieldData = (Map) pinProperties.get("fielddata"); - Distance precision = Distance.parseDistance(pinFieldData.get("precision").toString()); - assertEquals(expected, precision); - } - -} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java b/core/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java index 2d68f801d27..d63a15b6afb 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/multifield/MultiFieldTests.java @@ -462,7 +462,6 @@ public class MultiFieldTests extends ElasticsearchSingleNodeTest { possibleSettings.put("filter.frequency.min", 1); possibleSettings.put("filter.frequency.max", 2); possibleSettings.put("filter.regex.pattern", ".*"); - possibleSettings.put("format", "fst"); possibleSettings.put("loading", "eager"); possibleSettings.put("foo", "bar"); possibleSettings.put("zetting", "zValue"); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java index 0583e289994..4c142e21239 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java @@ -381,7 +381,7 @@ public class SimpleStringMappingTests extends ElasticsearchSingleNodeTest { .startObject("str1") .field("type", "string") .startObject("fielddata") - .field("format", "fst") + .field("format", "paged_bytes") .endObject() .endObject() .startObject("str2") diff --git a/core/src/test/java/org/elasticsearch/index/mapper/update/all_mapping_create_index.json b/core/src/test/java/org/elasticsearch/index/mapper/update/all_mapping_create_index.json index 2b9c42d50b2..e9604ae458f 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/update/all_mapping_create_index.json +++ b/core/src/test/java/org/elasticsearch/index/mapper/update/all_mapping_create_index.json @@ -12,7 +12,7 @@ "search_analyzer": "whitespace", "similarity": "my_similarity", "fielddata": { - "format": "fst" + "format": "paged_bytes" } } } diff --git a/core/src/test/java/org/elasticsearch/indices/memory/breaker/RandomExceptionCircuitBreakerTests.java b/core/src/test/java/org/elasticsearch/indices/memory/breaker/RandomExceptionCircuitBreakerTests.java index c2da93b9ab3..7b2a12f8f98 100644 --- a/core/src/test/java/org/elasticsearch/indices/memory/breaker/RandomExceptionCircuitBreakerTests.java +++ b/core/src/test/java/org/elasticsearch/indices/memory/breaker/RandomExceptionCircuitBreakerTests.java @@ -70,9 +70,6 @@ public class RandomExceptionCircuitBreakerTests extends ElasticsearchIntegration .field("type", "string") .field("index", "not_analyzed") .field("doc_values", randomBoolean()) - .startObject("fielddata") - .field("format", randomBytesFieldDataFormat()) - .endObject() // fielddata .endObject() // test-str .startObject("test-num") // I don't use randomNumericType() here because I don't want "byte", and I want "float" and "double" diff --git a/core/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java b/core/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java index 174f2a97a12..5f0225f4f77 100644 --- a/core/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java +++ b/core/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java @@ -391,7 +391,6 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase .field("match_mapping_type", "string") .startObject("mapping") .startObject("fielddata") - .field(FieldDataType.FORMAT_KEY, randomFrom("paged_bytes", "fst")) .field(Loading.KEY, randomLoadingValues()) .endObject() .endObject() @@ -1769,14 +1768,6 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase return randomFrom(Arrays.asList("array", "doc_values")); } - /** - * Returns a random bytes field data format from the choices of - * "paged_bytes", "fst", or "doc_values". - */ - public static String randomBytesFieldDataFormat() { - return randomFrom(Arrays.asList("paged_bytes", "fst")); - } - /** * Returns a random JODA Time Zone based on Java Time Zones */ diff --git a/docs/reference/index-modules/fielddata.asciidoc b/docs/reference/index-modules/fielddata.asciidoc index 5698af6d4da..b54c45a04e0 100644 --- a/docs/reference/index-modules/fielddata.asciidoc +++ b/docs/reference/index-modules/fielddata.asciidoc @@ -86,13 +86,13 @@ breaker using The field data format controls how field data should be stored. Depending on the field type, there might be several field data types -available. In particular, string and numeric types support the `doc_values` +available. In particular, string, geo-point and numeric types support the `doc_values` format which allows for computing the field data data-structures at indexing time and storing them on disk. Although it will make the index larger and may be slightly slower, this implementation will be more near-realtime-friendly and will require much less memory from the JVM than other implementations. -Here is an example of how to configure the `tag` field to use the `fst` field +Here is an example of how to configure the `tag` field to use the `paged_bytes` field data format. [source,js] @@ -101,31 +101,23 @@ data format. "tag": { "type": "string", "fielddata": { - "format": "fst" + "format": "paged_bytes" } } } -------------------------------------------------- It is possible to change the field data format (and the field data settings -in general) on a live index by using the update mapping API. When doing so, -field data which had already been loaded for existing segments will remain -alive while new segments will use the new field data configuration. Thanks to -the background merging process, all segments will eventually use the new -field data format. +in general) on a live index by using the update mapping API. [float] ==== String field data types -`paged_bytes` (default):: +`paged_bytes` (default on analyzed string fields):: Stores unique terms sequentially in a large buffer and maps documents to the indices of the terms they contain in this large buffer. -`fst`:: - Stores terms in a FST. Slower to build than `paged_bytes` but can help lower - memory usage if many terms share common prefixes and/or suffixes. - -`doc_values`:: +`doc_values` (default when index is set to `not_analyzed`):: Computes and stores field data data-structures on disk at indexing time. Lowers memory usage but only works on non-analyzed strings (`index`: `no` or `not_analyzed`). @@ -133,19 +125,19 @@ field data format. [float] ==== Numeric field data types -`array` (default):: +`array`:: Stores field values in memory using arrays. -`doc_values`:: +`doc_values` (default unless doc values are disabled):: Computes and stores field data data-structures on disk at indexing time. [float] ==== Geo point field data types -`array` (default):: +`array`:: Stores latitudes and longitudes in arrays. -`doc_values`:: +`doc_values` (default unless doc values are disabled):: Computes and stores field data data-structures on disk at indexing time. [float] From bd5c7d0ea20d1aead6cb1ccf063169b762325786 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 15 Jun 2015 17:08:15 +0200 Subject: [PATCH 32/43] [maven] clean pom.xml In Maven parent project, in dependency management, we should only declare which versions of 3rd party jars we want to use but not force any scope. It makes then more obvious in modules what is exactly the scope of any dependency. For example, one could imagine importing `jimfs` as a `compile` dependency in another module/plugin with: ```xml com.google.jimfs jimfs ``` But it won't work as expected as the default maven `scope` should be `compile` but here it's `test` as defined in the parent project. So, if you want to use this lib for tests, you should simply define: ```xml com.google.jimfs jimfs test ``` We also remove `maven-s3-wagon` from gce plugin as it's not used. --- core/pom.xml | 50 ++------------------------------- plugins/cloud-gce/pom.xml | 7 ----- plugins/lang-javascript/pom.xml | 2 -- plugins/pom.xml | 4 +++ pom.xml | 22 +-------------- 5 files changed, 7 insertions(+), 78 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 426d4c4d125..70ccb56248f 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -66,69 +66,56 @@ org.apache.lucene lucene-core - compile org.apache.lucene lucene-backward-codecs - compile org.apache.lucene lucene-analyzers-common - compile org.apache.lucene lucene-queries - compile org.apache.lucene lucene-memory - compile org.apache.lucene lucene-highlighter - compile org.apache.lucene lucene-queryparser - compile org.apache.lucene lucene-suggest - compile org.apache.lucene lucene-join - compile org.apache.lucene lucene-spatial - compile org.apache.lucene lucene-expressions - compile true com.spatial4j spatial4j - compile true com.vividsolutions jts - compile true @@ -140,45 +127,34 @@ - + com.google.guava guava - compile - com.carrotsearch hppc - joda-time joda-time - compile org.joda joda-convert - compile - com.fasterxml.jackson.core jackson-core - compile - com.fasterxml.jackson.dataformat jackson-dataformat-smile - compile - com.fasterxml.jackson.dataformat jackson-dataformat-yaml - compile com.fasterxml.jackson.core @@ -186,83 +162,61 @@ - com.fasterxml.jackson.dataformat jackson-dataformat-cbor - compile - io.netty netty - compile - com.ning compress-lzf - compile - com.tdunning t-digest - compile - org.apache.commons commons-lang3 - commons-cli commons-cli - - + org.codehaus.groovy groovy-all indy - compile true - log4j log4j - compile true - log4j apache-log4j-extras - compile true - org.slf4j slf4j-api - compile true - net.java.dev.jna jna - compile true - org.fusesource sigar - compile true diff --git a/plugins/cloud-gce/pom.xml b/plugins/cloud-gce/pom.xml index ca9206abccc..926a94a2dab 100644 --- a/plugins/cloud-gce/pom.xml +++ b/plugins/cloud-gce/pom.xml @@ -47,13 +47,6 @@ governing permissions and limitations under the License. --> - - - org.kuali.maven.wagons - maven-s3-wagon - 1.1.19 - - org.apache.maven.plugins diff --git a/plugins/lang-javascript/pom.xml b/plugins/lang-javascript/pom.xml index 7179582953c..f9573271de1 100644 --- a/plugins/lang-javascript/pom.xml +++ b/plugins/lang-javascript/pom.xml @@ -26,8 +26,6 @@ org.mozilla rhino 1.7R4 - - diff --git a/plugins/pom.xml b/plugins/pom.xml index 7e54c2207be..bb314ec0f2d 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -27,14 +27,17 @@ org.hamcrest hamcrest-all + test junit junit + test org.apache.lucene lucene-test-framework + test org.elasticsearch @@ -44,6 +47,7 @@ org.elasticsearch elasticsearch test-jar + test log4j diff --git a/pom.xml b/pom.xml index e0e803278f4..c6bb63f7620 100644 --- a/pom.xml +++ b/pom.xml @@ -132,35 +132,30 @@ com.carrotsearch.randomizedtesting randomizedtesting-runner ${testframework.version} - test org.hamcrest hamcrest-all 1.3 - test junit junit 4.11 - test com.google.jimfs jimfs 1.0 - test org.apache.lucene lucene-test-framework ${lucene.maven.version} - test @@ -168,12 +163,12 @@ elasticsearch ${elasticsearch.version} + org.elasticsearch elasticsearch ${elasticsearch.version} test-jar - test @@ -255,13 +250,11 @@ com.spatial4j spatial4j 0.4.1 - true com.vividsolutions jts 1.13 - true xerces @@ -274,35 +267,29 @@ com.github.spullara.mustache.java compiler 0.8.13 - true - org.apache.lucene lucene-analyzers-phonetic ${lucene.maven.version} - org.apache.lucene lucene-analyzers-kuromoji ${lucene.maven.version} - org.apache.lucene lucene-analyzers-stempel ${lucene.maven.version} - org.apache.lucene lucene-analyzers-icu ${lucene.maven.version} - org.apache.lucene lucene-analyzers-smartcn @@ -396,35 +383,30 @@ groovy-all 2.4.0 indy - true log4j log4j ${log4j.version} - true log4j apache-log4j-extras ${log4j.version} - true org.slf4j slf4j-api ${slf4j.version} - true org.slf4j slf4j-log4j12 ${slf4j.version} - true @@ -438,7 +420,6 @@ org.fusesource sigar 1.6.4 - true @@ -459,7 +440,6 @@ org.jacoco.agent runtime 0.6.4.201312101107 - test From 8dd7dae5b986148be126a336c937e4c29c0bfd49 Mon Sep 17 00:00:00 2001 From: gmarz Date: Mon, 15 Jun 2015 11:10:51 -0400 Subject: [PATCH 33/43] [SPEC] Add `rewrite` query parameter to the `indices.validate_query` API --- rest-api-spec/api/indices.validate_query.json | 4 ++++ rest-api-spec/test/indices.validate_query/10_basic.yaml | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/rest-api-spec/api/indices.validate_query.json b/rest-api-spec/api/indices.validate_query.json index 5dd5cda5363..98af689833a 100644 --- a/rest-api-spec/api/indices.validate_query.json +++ b/rest-api-spec/api/indices.validate_query.json @@ -66,6 +66,10 @@ "lowercase_expanded_terms": { "type" : "boolean", "description" : "Specify whether query terms should be lowercased" + }, + "rewrite": { + "type": "boolean", + "description": "Provide a more detailed explanation showing the actual Lucene query that will be executed." } } }, diff --git a/rest-api-spec/test/indices.validate_query/10_basic.yaml b/rest-api-spec/test/indices.validate_query/10_basic.yaml index 2a9ed19221f..bb5a893834c 100644 --- a/rest-api-spec/test/indices.validate_query/10_basic.yaml +++ b/rest-api-spec/test/indices.validate_query/10_basic.yaml @@ -33,4 +33,13 @@ - match: {_shards.failed: 0} - match: {explanations.0.index: 'testing'} - match: {explanations.0.explanation: '*:*'} + + - do: + indices.validate_query: + rewrite: true + + - is_true: valid + - match: {_shards.failed: 0} + - match: {explanations.0.index: 'testing'} + - match: {explanations.0.explanation: 'ConstantScore(*:*)'} From 3b93653863f1a8014488ebcabed0e0da9ddf3cb8 Mon Sep 17 00:00:00 2001 From: gmarz Date: Mon, 15 Jun 2015 12:52:06 -0400 Subject: [PATCH 34/43] [TEST] Remove validate_query rewrite YAML test --- rest-api-spec/test/indices.validate_query/10_basic.yaml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/rest-api-spec/test/indices.validate_query/10_basic.yaml b/rest-api-spec/test/indices.validate_query/10_basic.yaml index bb5a893834c..2a9ed19221f 100644 --- a/rest-api-spec/test/indices.validate_query/10_basic.yaml +++ b/rest-api-spec/test/indices.validate_query/10_basic.yaml @@ -33,13 +33,4 @@ - match: {_shards.failed: 0} - match: {explanations.0.index: 'testing'} - match: {explanations.0.explanation: '*:*'} - - - do: - indices.validate_query: - rewrite: true - - - is_true: valid - - match: {_shards.failed: 0} - - match: {explanations.0.index: 'testing'} - - match: {explanations.0.explanation: 'ConstantScore(*:*)'} From a08f51e13b7a37fcdf31f6e2f6d892d7c4d771d6 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 15 Jun 2015 10:55:37 -0700 Subject: [PATCH 35/43] Address PR comments --- .../index/mapper/MappedFieldType.java | 6 ++---- .../index/mapper/core/BinaryFieldMapper.java | 2 -- .../index/mapper/core/DateFieldMapper.java | 6 ++---- .../index/mapper/geo/GeoPointFieldMapper.java | 14 ++++++-------- .../index/mapper/geo/GeoShapeFieldMapper.java | 12 ++++++------ .../mapper/internal/FieldNamesFieldMapper.java | 4 +--- .../index/mapper/core/BinaryFieldTypeTests.java | 2 +- .../index/mapper/core/DateFieldTypeTests.java | 2 +- .../index/mapper/geo/GeoPointFieldTypeTests.java | 2 +- .../index/mapper/geo/GeoShapeFieldTypeTests.java | 2 +- .../mapper/internal/FieldNamesFieldTypeTests.java | 2 +- 11 files changed, 22 insertions(+), 32 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index 6f2cab8ede4..8bd02946425 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -203,12 +203,10 @@ public class MappedFieldType extends FieldType { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof MappedFieldType)) return false; if (!super.equals(o)) return false; MappedFieldType fieldType = (MappedFieldType) o; - return Objects.equals(boost, fieldType.boost) && - Objects.equals(docValues, fieldType.docValues) && + return boost == fieldType.boost && + docValues == fieldType.docValues && Objects.equals(names, fieldType.names) && Objects.equals(indexAnalyzer, fieldType.indexAnalyzer) && Objects.equals(searchAnalyzer, fieldType.searchAnalyzer) && diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java index 02946df4446..20127b86bab 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java @@ -125,8 +125,6 @@ public class BinaryFieldMapper extends AbstractFieldMapper { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof BinaryFieldType)) return false; if (!super.equals(o)) return false; BinaryFieldType that = (BinaryFieldType) o; return Objects.equals(tryUncompressing, that.tryUncompressing); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index 62c77ee2f87..5fe0980c7f2 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -237,17 +237,15 @@ public class DateFieldMapper extends NumberFieldMapper { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof DateFieldType)) return false; if (!super.equals(o)) return false; DateFieldType that = (DateFieldType) o; return Objects.equals(dateTimeFormatter.format(), that.dateTimeFormatter.format()) && - Objects.equals(timeUnit, that.timeUnit); + Objects.equals(timeUnit, that.timeUnit); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), dateTimeFormatter, timeUnit); + return Objects.hash(super.hashCode(), dateTimeFormatter.format(), timeUnit); } public FormatDateTimeFormatter dateTimeFormatter() { diff --git a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java index 9ce4dba8270..a29597b76e6 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java @@ -314,16 +314,14 @@ public class GeoPointFieldMapper extends AbstractFieldMapper implements ArrayVal @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof GeoPointFieldType)) return false; if (!super.equals(o)) return false; GeoPointFieldType that = (GeoPointFieldType) o; - return java.util.Objects.equals(geohashPrecision, that.geohashPrecision) && - java.util.Objects.equals(geohashPrefixEnabled, that.geohashPrefixEnabled) && - java.util.Objects.equals(validateLon, that.validateLon) && - java.util.Objects.equals(validateLat, that.validateLat) && - java.util.Objects.equals(normalizeLon, that.normalizeLon) && - java.util.Objects.equals(normalizeLat, that.normalizeLat) && + return geohashPrecision == that.geohashPrecision && + geohashPrefixEnabled == that.geohashPrefixEnabled && + validateLon == that.validateLon && + validateLat == that.validateLat && + normalizeLon == that.normalizeLon && + normalizeLat == that.normalizeLat && java.util.Objects.equals(geohashFieldType, that.geohashFieldType) && java.util.Objects.equals(latFieldType, that.latFieldType) && java.util.Objects.equals(lonFieldType, that.lonFieldType); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java index aafda6d3cea..b496e6f648e 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldMapper.java @@ -208,17 +208,15 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof GeoShapeFieldType)) return false; if (!super.equals(o)) return false; GeoShapeFieldType that = (GeoShapeFieldType) o; - return Objects.equals(treeLevels, that.treeLevels) && - Objects.equals(precisionInMeters, that.precisionInMeters) && - Objects.equals(defaultDistanceErrorPct, that.defaultDistanceErrorPct) && + return treeLevels == that.treeLevels && + precisionInMeters == that.precisionInMeters && + defaultDistanceErrorPct == that.defaultDistanceErrorPct && Objects.equals(tree, that.tree) && Objects.equals(strategyName, that.strategyName) && Objects.equals(distanceErrorPct, that.distanceErrorPct) && - Objects.equals(orientation, that.orientation); + orientation == that.orientation; } @Override @@ -229,6 +227,8 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper { @Override public void freeze() { super.freeze(); + // This is a bit hackish: we need to setup the spatial tree and strategies once the field name is set, which + // must be by the time freeze is called. SpatialPrefixTree prefixTree; if ("geohash".equals(tree)) { prefixTree = new GeohashPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, getLevels(treeLevels, precisionInMeters, Defaults.GEOHASH_LEVELS, true)); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java index 09ecbbdc4c9..37de41b6f2e 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapper.java @@ -151,11 +151,9 @@ public class FieldNamesFieldMapper extends AbstractFieldMapper implements RootMa @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof FieldNamesFieldType)) return false; if (!super.equals(o)) return false; FieldNamesFieldType that = (FieldNamesFieldType) o; - return Objects.equals(enabled, that.enabled); + return enabled == that.enabled; } @Override diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java index 5633c8467d0..930b6eabb9b 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java @@ -38,7 +38,7 @@ public class BinaryFieldTypeTests extends FieldTypeTestCase { BinaryFieldMapper.BinaryFieldType bft = (BinaryFieldMapper.BinaryFieldType)ft; switch (propNum) { case 0: bft.setTryUncompressing(!bft.tryUncompressing()); break; - default: super.modifyProperty(ft, numProperties() - propNum - 1); + default: super.modifyProperty(ft, 1 + propNum); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java index 087b57e7c0b..7c6828c666b 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java @@ -47,7 +47,7 @@ public class DateFieldTypeTests extends FieldTypeTestCase { switch (propNum) { case 0: dft.setDateTimeFormatter(Joda.forPattern("basic_week_date", Locale.ROOT)); break; case 1: dft.setTimeUnit(TimeUnit.HOURS); break; - default: super.modifyProperty(ft, numProperties() - propNum - 1); + default: super.modifyProperty(ft, 2 + propNum); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java index 7830d9c0b61..a7f8c1e3c85 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java @@ -42,7 +42,7 @@ public class GeoPointFieldTypeTests extends FieldTypeTestCase { case 3: gft.setValidateLat(!gft.validateLat()); break; case 4: gft.setNormalizeLon(!gft.normalizeLon()); break; case 5: gft.setNormalizeLat(!gft.normalizeLat()); break; - default: super.modifyProperty(ft, numProperties() - propNum - 1); + default: super.modifyProperty(ft, 6 + propNum); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java index 81b8dc52bc4..9edd6bc9388 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java @@ -45,7 +45,7 @@ public class GeoShapeFieldTypeTests extends FieldTypeTestCase { case 3: gft.setPrecisionInMeters(20); break; case 4: gft.setDefaultDistanceErrorPct(0.5); break; case 5: gft.setOrientation(ShapeBuilder.Orientation.LEFT); break; - default: super.modifyProperty(ft, numProperties() - propNum - 1); + default: super.modifyProperty(ft, 6 + propNum); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java index a95f1d08d16..25107e26607 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java @@ -37,7 +37,7 @@ public class FieldNamesFieldTypeTests extends FieldTypeTestCase { FieldNamesFieldMapper.FieldNamesFieldType fnft = (FieldNamesFieldMapper.FieldNamesFieldType)ft; switch (propNum) { case 0: fnft.setEnabled(!fnft.isEnabled()); break; - default: super.modifyProperty(ft, numProperties() - propNum - 1); + default: super.modifyProperty(ft, 1 + propNum); } } } From e461852548b6ee1cbb18031989af4790f4922f48 Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Mon, 15 Jun 2015 20:26:15 +0200 Subject: [PATCH 36/43] Test: fix removal of e validate_query rewrite YAML test --- rest-api-spec/test/indices.validate_query/10_basic.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest-api-spec/test/indices.validate_query/10_basic.yaml b/rest-api-spec/test/indices.validate_query/10_basic.yaml index 2a9ed19221f..39afa47f25d 100644 --- a/rest-api-spec/test/indices.validate_query/10_basic.yaml +++ b/rest-api-spec/test/indices.validate_query/10_basic.yaml @@ -32,5 +32,5 @@ - is_true: valid - match: {_shards.failed: 0} - match: {explanations.0.index: 'testing'} - - match: {explanations.0.explanation: '*:*'} + - match: {explanations.0.explanation: 'ConstantScore(*:*)'} From e559eafa19f12b0c12b5d7d871fe3e059347d41f Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Mon, 15 Jun 2015 21:57:05 +0200 Subject: [PATCH 37/43] Revert "Test: fix removal of e validate_query rewrite YAML test" This reverts commit e461852548b6ee1cbb18031989af4790f4922f48. --- rest-api-spec/test/indices.validate_query/10_basic.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest-api-spec/test/indices.validate_query/10_basic.yaml b/rest-api-spec/test/indices.validate_query/10_basic.yaml index 39afa47f25d..2a9ed19221f 100644 --- a/rest-api-spec/test/indices.validate_query/10_basic.yaml +++ b/rest-api-spec/test/indices.validate_query/10_basic.yaml @@ -32,5 +32,5 @@ - is_true: valid - match: {_shards.failed: 0} - match: {explanations.0.index: 'testing'} - - match: {explanations.0.explanation: 'ConstantScore(*:*)'} + - match: {explanations.0.explanation: '*:*'} From 2fb8df09851e5a848d2b298e6e1c0f734686aecb Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Mon, 15 Jun 2015 23:40:52 +0200 Subject: [PATCH 38/43] Test: NetworkPartitionTests should make sure MockTransportService is used. --- .../test/disruption/NetworkPartitionTests.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java b/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java index cea9a0e6d7f..a6e522aeaa2 100644 --- a/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java +++ b/core/src/test/java/org/elasticsearch/test/disruption/NetworkPartitionTests.java @@ -22,14 +22,24 @@ package org.elasticsearch.test.disruption; import org.apache.lucene.util.LuceneTestCase; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.test.transport.MockTransportService; +import org.elasticsearch.transport.TransportModule; import org.junit.Test; import java.io.IOException; @LuceneTestCase.Slow -public class NetworkPartitionTests extends ElasticsearchIntegrationTest{ +public class NetworkPartitionTests extends ElasticsearchIntegrationTest { + + @Override + protected Settings nodeSettings(int nodeOrdinal) { + return Settings.builder() + .put(TransportModule.TRANSPORT_SERVICE_TYPE_KEY, MockTransportService.class.getName()) + .build(); + } @Test public void testNetworkPartitionWithNodeShutdown() throws IOException { From 5601cea0837a0b18bf554e57b6841fa28420d81a Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 15 Jun 2015 15:36:27 -0700 Subject: [PATCH 39/43] Mappings: Remove SmartNameObjectMapper This was previously a container for an ObjectMapper, along with the DocumentMapper that ObjectMapper came from. However, there was only one use of needing the associated DocumentMapper, and that wasn't actually used. --- .../index/mapper/MapperService.java | 45 +++---------------- .../index/query/ExistsQueryParser.java | 6 +-- .../index/query/MissingQueryParser.java | 13 +++--- .../index/query/QueryParseContext.java | 5 ++- .../support/NestedInnerQueryParseSupport.java | 9 +--- .../percolator/PercolateContext.java | 16 +++++-- .../bucket/nested/NestedAggregator.java | 6 +-- .../nested/ReverseNestedAggregator.java | 6 +-- .../search/fetch/FetchPhase.java | 3 +- .../innerhits/InnerHitsParseElement.java | 11 +++-- .../search/internal/DefaultSearchContext.java | 6 +-- .../internal/FilteredSearchContext.java | 6 +-- .../search/internal/SearchContext.java | 3 +- .../elasticsearch/test/TestSearchContext.java | 5 ++- 14 files changed, 51 insertions(+), 89 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 83a5a2866c3..eb36fa88a41 100755 --- a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -532,35 +532,26 @@ public class MapperService extends AbstractIndexComponent { return fields; } - public SmartNameObjectMapper smartNameObjectMapper(String smartName, @Nullable String[] types) { + public ObjectMapper getObjectMapper(String name, @Nullable String[] types) { if (types == null || types.length == 0 || types.length == 1 && types[0].equals("_all")) { - ObjectMappers mappers = fullPathObjectMappers.get(smartName); + ObjectMappers mappers = fullPathObjectMappers.get(name); if (mappers != null) { - return new SmartNameObjectMapper(mappers.mapper(), guessDocMapper(smartName)); + return mappers.mapper(); } return null; } for (String type : types) { DocumentMapper possibleDocMapper = mappers.get(type); if (possibleDocMapper != null) { - ObjectMapper mapper = possibleDocMapper.objectMappers().get(smartName); + ObjectMapper mapper = possibleDocMapper.objectMappers().get(name); if (mapper != null) { - return new SmartNameObjectMapper(mapper, possibleDocMapper); + return mapper; } } } return null; } - private DocumentMapper guessDocMapper(String path) { - for (DocumentMapper documentMapper : docMappers(false)) { - if (documentMapper.objectMappers().containsKey(path)) { - return documentMapper; - } - } - return null; - } - public MappedFieldType smartNameFieldType(String smartName) { MappedFieldType fieldType = fullName(smartName); if (fieldType != null) { @@ -663,32 +654,6 @@ public class MapperService extends AbstractIndexComponent { return META_FIELDS.contains(fieldName); } - public static class SmartNameObjectMapper { - private final ObjectMapper mapper; - private final DocumentMapper docMapper; - - public SmartNameObjectMapper(ObjectMapper mapper, @Nullable DocumentMapper docMapper) { - this.mapper = mapper; - this.docMapper = docMapper; - } - - public boolean hasMapper() { - return mapper != null; - } - - public ObjectMapper mapper() { - return mapper; - } - - public boolean hasDocMapper() { - return docMapper != null; - } - - public DocumentMapper docMapper() { - return docMapper; - } - } - final class SmartIndexNameSearchAnalyzer extends DelegatingAnalyzerWrapper { private final Analyzer defaultAnalyzer; diff --git a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java index bb86becb6bd..2617d143e43 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java @@ -23,10 +23,10 @@ import org.apache.lucene.search.*; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper; +import org.elasticsearch.index.mapper.object.ObjectMapper; import java.io.IOException; import java.util.Collection; @@ -84,8 +84,8 @@ public class ExistsQueryParser implements QueryParser { return Queries.newMatchNoDocsQuery(); } - MapperService.SmartNameObjectMapper smartNameObjectMapper = parseContext.smartObjectMapper(fieldPattern); - if (smartNameObjectMapper != null && smartNameObjectMapper.hasMapper()) { + ObjectMapper objectMapper = parseContext.getObjectMapper(fieldPattern); + if (objectMapper != null) { // automatic make the object mapper pattern fieldPattern = fieldPattern + ".*"; } diff --git a/core/src/main/java/org/elasticsearch/index/query/MissingQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/MissingQueryParser.java index d0f3e5522ed..f76478ffba0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MissingQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/MissingQueryParser.java @@ -19,14 +19,17 @@ package org.elasticsearch.index.query; -import org.apache.lucene.search.*; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermRangeQuery; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper; +import org.elasticsearch.index.mapper.object.ObjectMapper; import java.io.IOException; import java.util.Collection; @@ -96,8 +99,8 @@ public class MissingQueryParser implements QueryParser { return Queries.newMatchNoDocsQuery(); } - MapperService.SmartNameObjectMapper smartNameObjectMapper = parseContext.smartObjectMapper(fieldPattern); - if (smartNameObjectMapper != null && smartNameObjectMapper.hasMapper()) { + ObjectMapper objectMapper = parseContext.getObjectMapper(fieldPattern); + if (objectMapper != null) { // automatic make the object mapper pattern fieldPattern = fieldPattern + ".*"; } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java b/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java index ae374522245..75f0c630054 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java @@ -40,6 +40,7 @@ import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.mapper.core.StringFieldMapper; +import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.support.NestedScope; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; @@ -283,8 +284,8 @@ public class QueryParseContext { return failIfFieldMappingNotFound(name, indexQueryParser.mapperService.smartNameFieldType(name, getTypes())); } - public MapperService.SmartNameObjectMapper smartObjectMapper(String name) { - return indexQueryParser.mapperService.smartNameObjectMapper(name, getTypes()); + public ObjectMapper getObjectMapper(String name) { + return indexQueryParser.mapperService.getObjectMapper(name, getTypes()); } /** Gets the search analyzer for the given field, or the default if there is none present for the field diff --git a/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java b/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java index b716fa2db41..63da8a1b217 100644 --- a/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java +++ b/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.QueryParseContext; @@ -57,7 +56,6 @@ public class NestedInnerQueryParseSupport { protected BitDocIdSetFilter parentFilter; protected BitDocIdSetFilter childFilter; - protected DocumentMapper childDocumentMapper; protected ObjectMapper nestedObjectMapper; private ObjectMapper parentObjectMapper; @@ -157,12 +155,7 @@ public class NestedInnerQueryParseSupport { public void setPath(String path) { this.path = path; - MapperService.SmartNameObjectMapper smart = parseContext.smartObjectMapper(path); - if (smart == null) { - throw new QueryParsingException(parseContext, "[nested] failed to find nested object under path [" + path + "]"); - } - childDocumentMapper = smart.docMapper(); - nestedObjectMapper = smart.mapper(); + nestedObjectMapper = parseContext.getObjectMapper(path); if (nestedObjectMapper == null) { throw new QueryParsingException(parseContext, "[nested] failed to find nested object under path [" + path + "]"); } diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java index d90923a25db..3c9c678fc77 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -44,10 +44,10 @@ import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.IndexFieldDataService; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParsedDocument; +import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.shard.IndexShard; @@ -65,7 +65,11 @@ import org.elasticsearch.search.fetch.innerhits.InnerHitsContext; import org.elasticsearch.search.fetch.script.ScriptFieldsContext; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.highlight.SearchContextHighlight; -import org.elasticsearch.search.internal.*; +import org.elasticsearch.search.internal.ContextIndexSearcher; +import org.elasticsearch.search.internal.InternalSearchHit; +import org.elasticsearch.search.internal.InternalSearchHitField; +import org.elasticsearch.search.internal.SearchContext; +import org.elasticsearch.search.internal.ShardSearchRequest; import org.elasticsearch.search.lookup.LeafSearchLookup; import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.search.query.QuerySearchResult; @@ -73,7 +77,11 @@ import org.elasticsearch.search.rescore.RescoreSearchContext; import org.elasticsearch.search.scan.ScanContext; import org.elasticsearch.search.suggest.SuggestionSearchContext; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentMap; /** @@ -662,7 +670,7 @@ public class PercolateContext extends SearchContext { } @Override - public MapperService.SmartNameObjectMapper smartNameObjectMapper(String name) { + public ObjectMapper getObjectMapper(String name) { throw new UnsupportedOperationException(); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java index 7f540c250d5..9aaf1eb1cb6 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java @@ -156,11 +156,7 @@ public class NestedAggregator extends SingleBucketAggregator { if (collectsFromSingleBucket == false) { return asMultiBucketAggregator(this, context, parent); } - MapperService.SmartNameObjectMapper mapper = context.searchContext().smartNameObjectMapper(path); - if (mapper == null) { - return new Unmapped(name, context, parent, pipelineAggregators, metaData); - } - ObjectMapper objectMapper = mapper.mapper(); + ObjectMapper objectMapper = context.searchContext().getObjectMapper(path); if (objectMapper == null) { return new Unmapped(name, context, parent, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregator.java index c658f204840..38c65a250fc 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregator.java @@ -145,11 +145,7 @@ public class ReverseNestedAggregator extends SingleBucketAggregator { final ObjectMapper objectMapper; if (path != null) { - MapperService.SmartNameObjectMapper mapper = context.searchContext().smartNameObjectMapper(path); - if (mapper == null) { - return new Unmapped(name, context, parent, pipelineAggregators, metaData); - } - objectMapper = mapper.mapper(); + objectMapper = context.searchContext().getObjectMapper(path); if (objectMapper == null) { return new Unmapped(name, context, parent, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/core/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index f4baf8a07fe..b7ff05ea5e5 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -44,7 +44,6 @@ import org.elasticsearch.index.fieldvisitor.FieldsVisitor; import org.elasticsearch.index.fieldvisitor.JustUidFieldsVisitor; import org.elasticsearch.index.fieldvisitor.UidAndSourceFieldsVisitor; import org.elasticsearch.index.mapper.DocumentMapper; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.internal.SourceFieldMapper; import org.elasticsearch.index.mapper.object.ObjectMapper; @@ -145,7 +144,7 @@ public class FetchPhase implements SearchPhase { MappedFieldType fieldType = context.smartNameFieldType(fieldName); if (fieldType == null) { // Only fail if we know it is a object field, missing paths / fields shouldn't fail. - if (context.smartNameObjectMapper(fieldName) != null) { + if (context.getObjectMapper(fieldName) != null) { throw new IllegalArgumentException("field [" + fieldName + "] isn't a leaf field"); } } else if (fieldType.stored()) { diff --git a/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java b/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java index 65d6a6057fe..d4871b6ab72 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java @@ -154,19 +154,18 @@ public class InnerHitsParseElement implements SearchParseElement { } private InnerHitsContext.NestedInnerHits parseNested(XContentParser parser, QueryParseContext parseContext, SearchContext searchContext, String nestedPath) throws Exception { - MapperService.SmartNameObjectMapper smartNameObjectMapper = searchContext.smartNameObjectMapper(nestedPath); - if (smartNameObjectMapper == null || !smartNameObjectMapper.hasMapper()) { + ObjectMapper objectMapper = searchContext.getObjectMapper(nestedPath); + if (objectMapper == null) { throw new IllegalArgumentException("path [" + nestedPath +"] doesn't exist"); } - ObjectMapper childObjectMapper = smartNameObjectMapper.mapper(); - if (!childObjectMapper.nested().isNested()) { + if (!objectMapper.nested().isNested()) { throw new IllegalArgumentException("path [" + nestedPath +"] isn't nested"); } - ObjectMapper parentObjectMapper = parseContext.nestedScope().nextLevel(childObjectMapper); + ObjectMapper parentObjectMapper = parseContext.nestedScope().nextLevel(objectMapper); ParseResult parseResult = parseSubSearchContext(searchContext, parseContext, parser); parseContext.nestedScope().previousLevel(); - return new InnerHitsContext.NestedInnerHits(parseResult.context(), parseResult.query(), parseResult.childInnerHits(), parentObjectMapper, childObjectMapper); + return new InnerHitsContext.NestedInnerHits(parseResult.context(), parseResult.query(), parseResult.childInnerHits(), parentObjectMapper, objectMapper); } private ParseResult parseSubSearchContext(SearchContext searchContext, QueryParseContext parseContext, XContentParser parser) throws Exception { diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index cba33a6d726..07cde1b3258 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -49,9 +49,9 @@ import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.IndexFieldDataService; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.shard.IndexShard; @@ -723,8 +723,8 @@ public class DefaultSearchContext extends SearchContext { } @Override - public MapperService.SmartNameObjectMapper smartNameObjectMapper(String name) { - return mapperService().smartNameObjectMapper(name, request.types()); + public ObjectMapper getObjectMapper(String name) { + return mapperService().getObjectMapper(name, request.types()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index 76829ed481a..b45ea62343d 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -34,9 +34,9 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.fielddata.IndexFieldDataService; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.shard.IndexShard; @@ -542,8 +542,8 @@ public abstract class FilteredSearchContext extends SearchContext { } @Override - public MapperService.SmartNameObjectMapper smartNameObjectMapper(String name) { - return in.smartNameObjectMapper(name); + public ObjectMapper getObjectMapper(String name) { + return in.getObjectMapper(name); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index b42840a3cd3..b94d21e44f8 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -37,6 +37,7 @@ import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.query.QueryParseContext; @@ -346,7 +347,7 @@ public abstract class SearchContext implements Releasable, HasContextAndHeaders */ public abstract MappedFieldType smartNameFieldTypeFromAnyType(String name); - public abstract MapperService.SmartNameObjectMapper smartNameObjectMapper(String name); + public abstract ObjectMapper getObjectMapper(String name); public abstract Counter timeEstimateCounter(); diff --git a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java index 176b98d7089..42123493ee1 100644 --- a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java +++ b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java @@ -38,6 +38,7 @@ import org.elasticsearch.index.cache.filter.FilterCache; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.shard.IndexShard; @@ -567,9 +568,9 @@ public class TestSearchContext extends SearchContext { } @Override - public MapperService.SmartNameObjectMapper smartNameObjectMapper(String name) { + public ObjectMapper getObjectMapper(String name) { if (mapperService() != null) { - return mapperService().smartNameObjectMapper(name, types); + return mapperService().getObjectMapper(name, types); } return null; } From 6e75f9fc933d21a05f983bd4eee61d70773229fc Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 15 Jun 2015 17:03:32 -0700 Subject: [PATCH 40/43] fix field type tests --- .../elasticsearch/index/mapper/core/BinaryFieldTypeTests.java | 2 +- .../org/elasticsearch/index/mapper/core/DateFieldTypeTests.java | 2 +- .../elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java | 2 +- .../elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java | 2 +- .../index/mapper/internal/FieldNamesFieldTypeTests.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java index 930b6eabb9b..47403c8b1df 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/BinaryFieldTypeTests.java @@ -38,7 +38,7 @@ public class BinaryFieldTypeTests extends FieldTypeTestCase { BinaryFieldMapper.BinaryFieldType bft = (BinaryFieldMapper.BinaryFieldType)ft; switch (propNum) { case 0: bft.setTryUncompressing(!bft.tryUncompressing()); break; - default: super.modifyProperty(ft, 1 + propNum); + default: super.modifyProperty(ft, propNum - 1); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java index 7c6828c666b..b6e162c8c38 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java @@ -47,7 +47,7 @@ public class DateFieldTypeTests extends FieldTypeTestCase { switch (propNum) { case 0: dft.setDateTimeFormatter(Joda.forPattern("basic_week_date", Locale.ROOT)); break; case 1: dft.setTimeUnit(TimeUnit.HOURS); break; - default: super.modifyProperty(ft, 2 + propNum); + default: super.modifyProperty(ft, propNum - 2); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java index a7f8c1e3c85..71f6f88d9aa 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoPointFieldTypeTests.java @@ -42,7 +42,7 @@ public class GeoPointFieldTypeTests extends FieldTypeTestCase { case 3: gft.setValidateLat(!gft.validateLat()); break; case 4: gft.setNormalizeLon(!gft.normalizeLon()); break; case 5: gft.setNormalizeLat(!gft.normalizeLat()); break; - default: super.modifyProperty(ft, 6 + propNum); + default: super.modifyProperty(ft, propNum - 6); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java index 9edd6bc9388..527d79c0ee9 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/geo/GeoShapeFieldTypeTests.java @@ -45,7 +45,7 @@ public class GeoShapeFieldTypeTests extends FieldTypeTestCase { case 3: gft.setPrecisionInMeters(20); break; case 4: gft.setDefaultDistanceErrorPct(0.5); break; case 5: gft.setOrientation(ShapeBuilder.Orientation.LEFT); break; - default: super.modifyProperty(ft, 6 + propNum); + default: super.modifyProperty(ft, propNum - 6); } } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java index 25107e26607..8f0ea33d37e 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldTypeTests.java @@ -37,7 +37,7 @@ public class FieldNamesFieldTypeTests extends FieldTypeTestCase { FieldNamesFieldMapper.FieldNamesFieldType fnft = (FieldNamesFieldMapper.FieldNamesFieldType)ft; switch (propNum) { case 0: fnft.setEnabled(!fnft.isEnabled()); break; - default: super.modifyProperty(ft, 1 + propNum); + default: super.modifyProperty(ft, propNum - 1); } } } From a23c5b8bbcd245119ec993054b18f713709530c4 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 15 Jun 2015 23:59:09 -0700 Subject: [PATCH 41/43] Make conditional clearer --- .../search/fetch/innerhits/InnerHitsParseElement.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java b/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java index d4871b6ab72..8fe2fba2e0f 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java @@ -158,7 +158,7 @@ public class InnerHitsParseElement implements SearchParseElement { if (objectMapper == null) { throw new IllegalArgumentException("path [" + nestedPath +"] doesn't exist"); } - if (!objectMapper.nested().isNested()) { + if (objectMapper.nested().isNested() == false) { throw new IllegalArgumentException("path [" + nestedPath +"] isn't nested"); } ObjectMapper parentObjectMapper = parseContext.nestedScope().nextLevel(objectMapper); From 65b1ce9900b8479a11f1d264d66a772920303cd1 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Wed, 3 Jun 2015 17:08:32 +0200 Subject: [PATCH 42/43] [doc] fix outdated java api examples * QueryBuilders.queryString is now QueryBuilders.queryStringQuery * DateHistogram.Interval is now DateHistogramInterval * Refactoring of buckets in aggs * FilterBuilders has been replaced by QueryBuilders Closes #9976. --- .../bucket/datehistogram-aggregation.asciidoc | 8 ++++---- .../aggregations/bucket/daterange-aggregation.asciidoc | 8 ++++---- .../aggregations/bucket/filter-aggregation.asciidoc | 2 +- .../aggregations/bucket/filters-aggregation.asciidoc | 6 +++--- .../aggregations/bucket/geodistance-aggregation.asciidoc | 8 ++++---- .../aggregations/bucket/geohashgrid-aggregation.asciidoc | 8 ++++---- .../aggregations/bucket/histogram-aggregation.asciidoc | 7 ++++--- .../aggregations/bucket/iprange-aggregation.asciidoc | 4 ++-- .../aggregations/bucket/range-aggregation.asciidoc | 4 ++-- docs/java-api/aggs.asciidoc | 2 +- docs/java-api/query-dsl-filters.asciidoc | 2 +- docs/java-api/search.asciidoc | 4 ++-- 12 files changed, 32 insertions(+), 31 deletions(-) diff --git a/docs/java-api/aggregations/bucket/datehistogram-aggregation.asciidoc b/docs/java-api/aggregations/bucket/datehistogram-aggregation.asciidoc index dd61767e512..9617fbc50f7 100644 --- a/docs/java-api/aggregations/bucket/datehistogram-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/datehistogram-aggregation.asciidoc @@ -47,11 +47,11 @@ Histogram agg = sr.getAggregations().get("agg"); // For each entry for (Histogram.Bucket entry : agg.getBuckets()) { - DateTime keyAsDate = (DateTime) entry.getKey(); // Key - String key = entry.getKeyAsString(); // Key as String - long docCount = entry.getDocCount(); // Doc count + DateTime key = (DateTime) entry.getKey(); // Key + String keyAsString = entry.getKeyAsString(); // Key as String + long docCount = entry.getDocCount(); // Doc count - logger.info("key [{}], date [{}], doc_count [{}]", key, keyAsDate.getYear(), docCount); + logger.info("key [{}], date [{}], doc_count [{}]", keyAsString, key.getYear(), docCount); } -------------------------------------------------- diff --git a/docs/java-api/aggregations/bucket/daterange-aggregation.asciidoc b/docs/java-api/aggregations/bucket/daterange-aggregation.asciidoc index b86bec68086..fa8f31e8cd0 100644 --- a/docs/java-api/aggregations/bucket/daterange-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/daterange-aggregation.asciidoc @@ -39,10 +39,10 @@ Range agg = sr.getAggregations().get("agg"); // For each entry for (Range.Bucket entry : agg.getBuckets()) { - String key = entry.getKey(); // Date range as key - DateTime fromAsDate = (DateTime) entry.getFrom(); // Date bucket from as a Date - DateTime toAsDate = (DateTime) entry.getTo(); // Date bucket to as a Date - long docCount = entry.getDocCount(); // Doc count + String key = entry.getKeyAsString(); // Date range as key + DateTime fromAsDate = (DateTime) entry.getFrom(); // Date bucket from as a Date + DateTime toAsDate = (DateTime) entry.getTo(); // Date bucket to as a Date + long docCount = entry.getDocCount(); // Doc count logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, fromAsDate, toAsDate, docCount); } diff --git a/docs/java-api/aggregations/bucket/filter-aggregation.asciidoc b/docs/java-api/aggregations/bucket/filter-aggregation.asciidoc index 5e2655c6a4e..8c462ed50bd 100644 --- a/docs/java-api/aggregations/bucket/filter-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/filter-aggregation.asciidoc @@ -14,7 +14,7 @@ Here is an example on how to create the aggregation request: -------------------------------------------------- AggregationBuilders .filter("agg") - .filter(FilterBuilders.termFilter("gender", "male")); + .filter(QueryBuilders.termQuery("gender", "male")); -------------------------------------------------- diff --git a/docs/java-api/aggregations/bucket/filters-aggregation.asciidoc b/docs/java-api/aggregations/bucket/filters-aggregation.asciidoc index b4c97c9ac11..903fa158679 100644 --- a/docs/java-api/aggregations/bucket/filters-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/filters-aggregation.asciidoc @@ -15,8 +15,8 @@ Here is an example on how to create the aggregation request: AggregationBuilder aggregation = AggregationBuilders .filters("agg") - .filter("men", FilterBuilders.termFilter("gender", "male")) - .filter("women", FilterBuilders.termFilter("gender", "female")); + .filter("men", QueryBuilders.termQuery("gender", "male")) + .filter("women", QueryBuilders.termQuery("gender", "female")); -------------------------------------------------- @@ -36,7 +36,7 @@ Filters agg = sr.getAggregations().get("agg"); // For each entry for (Filters.Bucket entry : agg.getBuckets()) { - String key = entry.getKey(); // bucket key + String key = entry.getKeyAsString(); // bucket key long docCount = entry.getDocCount(); // Doc count logger.info("key [{}], doc_count [{}]", key, docCount); } diff --git a/docs/java-api/aggregations/bucket/geodistance-aggregation.asciidoc b/docs/java-api/aggregations/bucket/geodistance-aggregation.asciidoc index e3a9d95c0e3..9d0ff65fdab 100644 --- a/docs/java-api/aggregations/bucket/geodistance-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/geodistance-aggregation.asciidoc @@ -40,10 +40,10 @@ Range agg = sr.getAggregations().get("agg"); // For each entry for (Range.Bucket entry : agg.getBuckets()) { - String key = entry.getKey(); // key as String - Number from = (Number) entry.getFrom(); // bucket from value - Number to = (Number) entry.getTo(); // bucket to value - long docCount = entry.getDocCount(); // Doc count + String key = entry.getKeyAsString(); // key as String + Number from = (Number) entry.getFrom(); // bucket from value + Number to = (Number) entry.getTo(); // bucket to value + long docCount = entry.getDocCount(); // Doc count logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, from, to, docCount); } diff --git a/docs/java-api/aggregations/bucket/geohashgrid-aggregation.asciidoc b/docs/java-api/aggregations/bucket/geohashgrid-aggregation.asciidoc index b322ca37776..19e3f033493 100644 --- a/docs/java-api/aggregations/bucket/geohashgrid-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/geohashgrid-aggregation.asciidoc @@ -36,11 +36,11 @@ GeoHashGrid agg = sr.getAggregations().get("agg"); // For each entry for (GeoHashGrid.Bucket entry : agg.getBuckets()) { - String key = entry.getKey(); // key as String - GeoPoint keyAsGeoPoint = entry.getKeyAsGeoPoint(); // key as geo point - long docCount = entry.getDocCount(); // Doc count + String keyAsString = entry.getKeyAsString(); // key as String + GeoPoint key = (GeoPoint) entry.getKey(); // key as geo point + long docCount = entry.getDocCount(); // Doc count - logger.info("key [{}], point {}, doc_count [{}]", key, keyAsGeoPoint, docCount); + logger.info("key [{}], point {}, doc_count [{}]", keyAsString, key, docCount); } -------------------------------------------------- diff --git a/docs/java-api/aggregations/bucket/histogram-aggregation.asciidoc b/docs/java-api/aggregations/bucket/histogram-aggregation.asciidoc index f5c8abe2e7e..bc1803e8617 100644 --- a/docs/java-api/aggregations/bucket/histogram-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/histogram-aggregation.asciidoc @@ -36,8 +36,9 @@ Histogram agg = sr.getAggregations().get("agg"); // For each entry for (Histogram.Bucket entry : agg.getBuckets()) { - Number key = (Number) entry.getKey(); // Key - String keyAsString = entry.getKeyAsString(); // Key As String - long docCount = entry.getDocCount(); // Doc count + Long key = (Long) entry.getKey(); // Key + long docCount = entry.getDocCount(); // Doc count + + logger.info("key [{}], doc_count [{}]", key, docCount); } -------------------------------------------------- diff --git a/docs/java-api/aggregations/bucket/iprange-aggregation.asciidoc b/docs/java-api/aggregations/bucket/iprange-aggregation.asciidoc index 3cf301ffaee..eb095a2898c 100644 --- a/docs/java-api/aggregations/bucket/iprange-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/iprange-aggregation.asciidoc @@ -46,11 +46,11 @@ import org.elasticsearch.search.aggregations.bucket.range.Range; [source,java] -------------------------------------------------- // sr is here your SearchResponse object -IPv4Range agg = sr.getAggregations().get("agg"); +Range agg = sr.getAggregations().get("agg"); // For each entry for (Range.Bucket entry : agg.getBuckets()) { - String key = entry.getKey(); // Ip range as key + String key = entry.getKeyAsString(); // Ip range as key String fromAsString = entry.getFromAsString(); // Ip bucket from as a String String toAsString = entry.getToAsString(); // Ip bucket to as a String long docCount = entry.getDocCount(); // Doc count diff --git a/docs/java-api/aggregations/bucket/range-aggregation.asciidoc b/docs/java-api/aggregations/bucket/range-aggregation.asciidoc index 3b0f8174eae..b30c856ebea 100644 --- a/docs/java-api/aggregations/bucket/range-aggregation.asciidoc +++ b/docs/java-api/aggregations/bucket/range-aggregation.asciidoc @@ -38,10 +38,10 @@ Range agg = sr.getAggregations().get("agg"); // For each entry for (Range.Bucket entry : agg.getBuckets()) { - String key = entry.getKey(); // Range as key + String key = entry.getKeyAsString(); // Range as key Number from = (Number) entry.getFrom(); // Bucket from Number to = (Number) entry.getTo(); // Bucket to - long docCount = entry.getDocCount(); // Doc count + long docCount = entry.getDocCount(); // Doc count logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, from, to, docCount); } diff --git a/docs/java-api/aggs.asciidoc b/docs/java-api/aggs.asciidoc index 891c782f21f..f6b1b4d79b7 100644 --- a/docs/java-api/aggs.asciidoc +++ b/docs/java-api/aggs.asciidoc @@ -47,7 +47,7 @@ SearchResponse sr = node.client().prepareSearch() AggregationBuilders.terms("by_country").field("country") .subAggregation(AggregationBuilders.dateHistogram("by_year") .field("dateOfBirth") - .interval((DateHistogram.Interval.YEAR) + .interval((DateHistogramInterval.YEAR) .subAggregation(AggregationBuilders.avg("avg_children").field("children")) ) ) diff --git a/docs/java-api/query-dsl-filters.asciidoc b/docs/java-api/query-dsl-filters.asciidoc index 2e8e8221a8a..660ba70d0cc 100644 --- a/docs/java-api/query-dsl-filters.asciidoc +++ b/docs/java-api/query-dsl-filters.asciidoc @@ -416,7 +416,7 @@ See {ref}/query-dsl-query-filter.html[Query Filter] [source,java] -------------------------------------------------- FilterBuilder filter = queryFilter( - queryString("this AND that OR thus") <1> + queryStringQuery("this AND that OR thus") <1> ); -------------------------------------------------- <1> query you want to wrap as a filter diff --git a/docs/java-api/search.asciidoc b/docs/java-api/search.asciidoc index e6896d8745f..1d34b6c46c0 100644 --- a/docs/java-api/search.asciidoc +++ b/docs/java-api/search.asciidoc @@ -87,7 +87,7 @@ documentation [source,java] -------------------------------------------------- SearchRequestBuilder srb1 = node.client() - .prepareSearch().setQuery(QueryBuilders.queryString("elasticsearch")).setSize(1); + .prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1); SearchRequestBuilder srb2 = node.client() .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1); @@ -120,7 +120,7 @@ SearchResponse sr = node.client().prepareSearch() .addAggregation( AggregationBuilders.dateHistogram("agg2") .field("birth") - .interval(DateHistogram.Interval.YEAR) + .interval(DateHistogramInterval.YEAR) ) .execute().actionGet(); From cdd13253deee8c4f0cafe5a048161e4efb9ffce7 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 16 Jun 2015 10:34:51 +0200 Subject: [PATCH 43/43] Tests: Add test case from #11692 --- .../aggregations/bucket/DateHistogramTests.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java index c8944aafb69..7278c9c07db 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramTests.java @@ -56,6 +56,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcke import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNull.notNullValue; @@ -97,10 +98,10 @@ public class DateHistogramTests extends ElasticsearchIntegrationTest { @Override public void setupSuiteScopeCluster() throws Exception { - createIndex("idx"); + assertAcked(prepareCreate("idx").addMapping("type", "_timestamp", "enabled=true")); createIndex("idx_unmapped"); // TODO: would be nice to have more random data here - prepareCreate("empty_bucket_idx").addMapping("type", "value", "type=integer").execute().actionGet(); + assertAcked(prepareCreate("empty_bucket_idx").addMapping("type", "value", "type=integer")); List builders = new ArrayList<>(); for (int i = 0; i < 2; i++) { builders.add(client().prepareIndex("empty_bucket_idx", "type", ""+i).setSource(jsonBuilder() @@ -1314,4 +1315,12 @@ public class DateHistogramTests extends ElasticsearchIntegrationTest { assertThat(e.toString(), containsString("ElasticsearchParseException")); } } + + @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/pull/11482") + public void testTimestampField() { + SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").field("_timestamp").interval(randomFrom(DateHistogramInterval.DAY, DateHistogramInterval.MONTH))).get(); + assertSearchResponse(response); + Histogram histo = response.getAggregations().get("histo"); + assertThat(histo.getBuckets().size(), greaterThan(0)); + } }