mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-23 13:26:02 +00:00
Clean up more messy tests
After #13834 many tests that used Groovy scripts (for good or bad reason) in their tests have been moved in the lang-groovy module and the issue #13837 has been created to track these messy tests in order to clean them up. This commit moves more tests back in core, removes the dependency on Groovy, changes the scripts in order to use the mocked script engine, and change the tests to integration tests.
This commit is contained in:
parent
b62ec1d300
commit
f745c96949
@ -1106,18 +1106,12 @@
|
||||
<suppress files="modules[/\\]lang-expression[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]script[/\\]expression[/\\]MoreExpressionTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]script[/\\]groovy[/\\]GroovyPlugin.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]script[/\\]groovy[/\\]GroovyScriptEngineService.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]BulkTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]DoubleTermsTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]IPv4RangeTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]IndexLookupTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]LongTermsTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]MinDocCountTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]MinTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]RandomScoreFunctionTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]ScriptedMetricTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]SearchFieldsTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]SimpleSortTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]StringTermsTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]package-info.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]script[/\\]groovy[/\\]GroovyScriptTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]script[/\\]groovy[/\\]GroovySecurityTests.java" checks="LineLength" />
|
||||
|
@ -17,13 +17,9 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.action.bulk;
|
||||
|
||||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
||||
import org.elasticsearch.action.bulk.BulkItemResponse;
|
||||
import org.elasticsearch.action.bulk.BulkRequest;
|
||||
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
||||
import org.elasticsearch.action.bulk.BulkResponse;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
@ -38,17 +34,21 @@ import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.VersionType;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.MockScriptPlugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.script.ScriptException;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||
@ -58,10 +58,56 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
public class BulkTests extends ESIntegTestCase {
|
||||
public class BulkWithUpdatesIT extends ESIntegTestCase {
|
||||
|
||||
@Override
|
||||
protected Settings nodeSettings(int nodeOrdinal) {
|
||||
return Settings.builder()
|
||||
.put(super.nodeSettings(nodeOrdinal))
|
||||
.put("script.default_lang", CustomScriptPlugin.NAME)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
public static class CustomScriptPlugin extends MockScriptPlugin {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
|
||||
|
||||
scripts.put("ctx._source.field += 1", vars -> srcScript(vars, source -> {
|
||||
Integer field = (Integer) source.get("field");
|
||||
return source.replace("field", field + 1);
|
||||
}));
|
||||
|
||||
scripts.put("ctx._source.counter += 1", vars -> srcScript(vars, source -> {
|
||||
Integer counter = (Integer) source.get("counter");
|
||||
return source.replace("counter", counter + 1);
|
||||
}));
|
||||
|
||||
scripts.put("ctx._source.field2 = 'value2'", vars -> srcScript(vars, source -> source.replace("field2", "value2")));
|
||||
|
||||
scripts.put("throw script exception on unknown var", vars -> {
|
||||
throw new ScriptException("message", null, Collections.emptyList(), "exception on unknown var", CustomScriptPlugin.NAME);
|
||||
});
|
||||
|
||||
scripts.put("ctx.op = \"none\"", vars -> ((Map<String, Object>) vars.get("ctx")).put("op", "none"));
|
||||
scripts.put("ctx.op = \"delete\"", vars -> ((Map<String, Object>) vars.get("ctx")).put("op", "delete"));
|
||||
return scripts;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static Object srcScript(Map<String, Object> vars, Function<Map<String, Object>, Object> f) {
|
||||
Map<?, ?> ctx = (Map<?, ?>) vars.get("ctx");
|
||||
|
||||
Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
|
||||
return f.apply(source);
|
||||
}
|
||||
}
|
||||
|
||||
public void testBulkUpdateSimple() throws Exception {
|
||||
@ -82,25 +128,26 @@ public class BulkTests extends ESIntegTestCase {
|
||||
assertThat(bulkItemResponse.getIndex(), equalTo("test"));
|
||||
}
|
||||
|
||||
final Script script = new Script("ctx._source.field += 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
bulkResponse = client().prepareBulk()
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("1")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null)))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null)).setRetryOnConflict(3))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("1").setScript(script))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2").setScript(script).setRetryOnConflict(3))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("3")
|
||||
.setDoc(jsonBuilder().startObject().field("field1", "test").endObject())).execute().actionGet();
|
||||
.setDoc(jsonBuilder().startObject().field("field1", "test").endObject()))
|
||||
.get();
|
||||
|
||||
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));
|
||||
assertThat(bulkResponse.getItems()[0].getResponse().getId(), equalTo("1"));
|
||||
assertThat(bulkResponse.getItems()[0].getResponse().getVersion(), equalTo(2L));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getId(), equalTo("2"));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(2L));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getId(), equalTo("3"));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(2L));
|
||||
|
||||
GetResponse getResponse = client().prepareGet().setIndex("test").setType("type1").setId("1").setFields("field").execute()
|
||||
.actionGet();
|
||||
@ -120,26 +167,23 @@ public class BulkTests extends ESIntegTestCase {
|
||||
|
||||
bulkResponse = client()
|
||||
.prepareBulk()
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("6")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("6").setScript(script)
|
||||
.setUpsert(jsonBuilder().startObject().field("field", 0).endObject()))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("7")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null)))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null))).execute()
|
||||
.actionGet();
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("7").setScript(script))
|
||||
.add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2").setScript(script))
|
||||
.get();
|
||||
|
||||
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()[0].getResponse().getId(), equalTo("6"));
|
||||
assertThat(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));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getId(), equalTo("2"));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getIndex(), equalTo("test"));
|
||||
assertThat(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));
|
||||
@ -164,11 +208,11 @@ public class BulkTests extends ESIntegTestCase {
|
||||
.add(client().prepareIndex("test", "type", "1").setSource("field", "2")).get();
|
||||
|
||||
assertTrue(((IndexResponse) bulkResponse.getItems()[0].getResponse()).isCreated());
|
||||
assertThat(((IndexResponse) bulkResponse.getItems()[0].getResponse()).getVersion(), equalTo(1L));
|
||||
assertThat(bulkResponse.getItems()[0].getResponse().getVersion(), equalTo(1L));
|
||||
assertTrue(((IndexResponse) bulkResponse.getItems()[1].getResponse()).isCreated());
|
||||
assertThat(((IndexResponse) bulkResponse.getItems()[1].getResponse()).getVersion(), equalTo(1L));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(1L));
|
||||
assertFalse(((IndexResponse) bulkResponse.getItems()[2].getResponse()).isCreated());
|
||||
assertThat(((IndexResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(2L));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(2L));
|
||||
|
||||
bulkResponse = client().prepareBulk()
|
||||
.add(client().prepareUpdate("test", "type", "1").setVersion(4L).setDoc("field", "2"))
|
||||
@ -176,29 +220,37 @@ public class BulkTests extends ESIntegTestCase {
|
||||
.add(client().prepareUpdate("test", "type", "1").setVersion(2L).setDoc("field", "3")).get();
|
||||
|
||||
assertThat(bulkResponse.getItems()[0].getFailureMessage(), containsString("version conflict"));
|
||||
assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getVersion(), equalTo(2L));
|
||||
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(3L));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(2L));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(3L));
|
||||
|
||||
bulkResponse = client().prepareBulk()
|
||||
.add(client().prepareIndex("test", "type", "e1").setSource("field", "1").setVersion(10).setVersionType(VersionType.EXTERNAL))
|
||||
.add(client().prepareIndex("test", "type", "e2").setSource("field", "1").setVersion(10).setVersionType(VersionType.EXTERNAL))
|
||||
.add(client().prepareIndex("test", "type", "e1").setSource("field", "2").setVersion(12).setVersionType(VersionType.EXTERNAL)).get();
|
||||
.add(client().prepareIndex("test", "type", "e1")
|
||||
.setSource("field", "1").setVersion(10).setVersionType(VersionType.EXTERNAL))
|
||||
.add(client().prepareIndex("test", "type", "e2")
|
||||
.setSource("field", "1").setVersion(10).setVersionType(VersionType.EXTERNAL))
|
||||
.add(client().prepareIndex("test", "type", "e1")
|
||||
.setSource("field", "2").setVersion(12).setVersionType(VersionType.EXTERNAL))
|
||||
.get();
|
||||
|
||||
assertTrue(((IndexResponse) bulkResponse.getItems()[0].getResponse()).isCreated());
|
||||
assertThat(((IndexResponse) bulkResponse.getItems()[0].getResponse()).getVersion(), equalTo(10L));
|
||||
assertThat(bulkResponse.getItems()[0].getResponse().getVersion(), equalTo(10L));
|
||||
assertTrue(((IndexResponse) bulkResponse.getItems()[1].getResponse()).isCreated());
|
||||
assertThat(((IndexResponse) bulkResponse.getItems()[1].getResponse()).getVersion(), equalTo(10L));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(10L));
|
||||
assertFalse(((IndexResponse) bulkResponse.getItems()[2].getResponse()).isCreated());
|
||||
assertThat(((IndexResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(12L));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(12L));
|
||||
|
||||
bulkResponse = client().prepareBulk()
|
||||
.add(client().prepareUpdate("test", "type", "e1").setDoc("field", "2").setVersion(10)) // INTERNAL
|
||||
.add(client().prepareUpdate("test", "type", "e1").setDoc("field", "3").setVersion(20).setVersionType(VersionType.FORCE))
|
||||
.add(client().prepareUpdate("test", "type", "e1").setDoc("field", "4").setVersion(20).setVersionType(VersionType.INTERNAL)).get();
|
||||
.add(client().prepareUpdate("test", "type", "e1")
|
||||
.setDoc("field", "2").setVersion(10)) // INTERNAL
|
||||
.add(client().prepareUpdate("test", "type", "e1")
|
||||
.setDoc("field", "3").setVersion(20).setVersionType(VersionType.FORCE))
|
||||
.add(client().prepareUpdate("test", "type", "e1")
|
||||
.setDoc("field", "4").setVersion(20).setVersionType(VersionType.INTERNAL))
|
||||
.get();
|
||||
|
||||
assertThat(bulkResponse.getItems()[0].getFailureMessage(), containsString("version conflict"));
|
||||
assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getVersion(), equalTo(20L));
|
||||
assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(21L));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(20L));
|
||||
assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(21L));
|
||||
}
|
||||
|
||||
public void testBulkUpdateMalformedScripts() throws Exception {
|
||||
@ -215,12 +267,12 @@ public class BulkTests extends ESIntegTestCase {
|
||||
assertThat(bulkResponse.getItems().length, equalTo(3));
|
||||
|
||||
bulkResponse = client().prepareBulk()
|
||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1")
|
||||
.setScript(new Script("ctx._source.field += a", ScriptService.ScriptType.INLINE, null, null)).setFields("field"))
|
||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptService.ScriptType.INLINE, null, null)).setFields("field"))
|
||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3")
|
||||
.setScript(new Script("ctx._source.field += a", ScriptService.ScriptType.INLINE, null, null)).setFields("field"))
|
||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1").setFields("field")
|
||||
.setScript(new Script("throw script exception on unknown var", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2").setFields("field")
|
||||
.setScript(new Script("ctx._source.field += 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3").setFields("field")
|
||||
.setScript(new Script("throw script exception on unknown var", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(bulkResponse.hasFailures(), equalTo(true));
|
||||
@ -229,10 +281,9 @@ public class BulkTests extends ESIntegTestCase {
|
||||
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].getResponse().getId(), equalTo("2"));
|
||||
assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(2L));
|
||||
assertThat(((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"));
|
||||
@ -248,17 +299,19 @@ public class BulkTests extends ESIntegTestCase {
|
||||
if (numDocs % 2 == 1) {
|
||||
numDocs++; // this test needs an even num of docs
|
||||
}
|
||||
logger.info("Bulk-Indexing {} docs", numDocs);
|
||||
|
||||
final Script script = new Script("ctx._source.counter += 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
BulkRequestBuilder builder = client().prepareBulk();
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
builder.add(
|
||||
client().prepareUpdate()
|
||||
.setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||
.setScript(new Script("ctx._source.counter += 1", ScriptService.ScriptType.INLINE, null, null)).setFields("counter")
|
||||
.setUpsert(jsonBuilder().startObject().field("counter", 1).endObject()));
|
||||
.setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter")
|
||||
.setScript(script)
|
||||
.setUpsert(jsonBuilder().startObject().field("counter", 1).endObject()));
|
||||
}
|
||||
|
||||
BulkResponse response = builder.execute().actionGet();
|
||||
BulkResponse response = builder.get();
|
||||
assertThat(response.hasFailures(), equalTo(false));
|
||||
assertThat(response.getItems().length, equalTo(numDocs));
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
@ -267,10 +320,9 @@ public class BulkTests extends ESIntegTestCase {
|
||||
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));
|
||||
assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
|
||||
assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(1L));
|
||||
assertThat(((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()
|
||||
@ -286,7 +338,7 @@ public class BulkTests extends ESIntegTestCase {
|
||||
UpdateRequestBuilder updateBuilder = client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||
.setFields("counter");
|
||||
if (i % 2 == 0) {
|
||||
updateBuilder.setScript(new Script("ctx._source.counter += 1", ScriptService.ScriptType.INLINE, null, null));
|
||||
updateBuilder.setScript(script);
|
||||
} else {
|
||||
updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
|
||||
}
|
||||
@ -306,17 +358,15 @@ public class BulkTests extends ESIntegTestCase {
|
||||
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));
|
||||
assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
|
||||
assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(2L));
|
||||
assertThat(((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(new Script("ctx._source.counter += 1", ScriptService.ScriptType.INLINE, null, null)));
|
||||
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(script));
|
||||
}
|
||||
response = builder.execute().actionGet();
|
||||
assertThat(response.hasFailures(), equalTo(true));
|
||||
@ -338,7 +388,7 @@ public class BulkTests extends ESIntegTestCase {
|
||||
builder = client().prepareBulk();
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||
.setScript(new Script("ctx.op = \"none\"", ScriptService.ScriptType.INLINE, null, null)));
|
||||
.setScript(new Script("ctx.op = \"none\"", ScriptType.INLINE, CustomScriptPlugin.NAME, null)));
|
||||
}
|
||||
response = builder.execute().actionGet();
|
||||
assertThat(response.buildFailureMessage(), response.hasFailures(), equalTo(false));
|
||||
@ -354,7 +404,7 @@ public class BulkTests extends ESIntegTestCase {
|
||||
builder = client().prepareBulk();
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||
.setScript(new Script("ctx.op = \"delete\"", ScriptService.ScriptType.INLINE, null, null)));
|
||||
.setScript(new Script("ctx.op = \"delete\"", ScriptType.INLINE, CustomScriptPlugin.NAME, null)));
|
||||
}
|
||||
response = builder.execute().actionGet();
|
||||
assertThat(response.hasFailures(), equalTo(false));
|
||||
@ -416,11 +466,38 @@ public class BulkTests extends ESIntegTestCase {
|
||||
|
||||
BulkRequestBuilder builder = client().prepareBulk();
|
||||
|
||||
byte[] addParent = new BytesArray("{\"index\" : { \"_index\" : \"test\", \"_type\" : \"parent\", \"_id\" : \"parent1\"}}\n" +
|
||||
"{\"field1\" : \"value1\"}\n").array();
|
||||
// It's important to use JSON parsing here and request objects: issue 3444 is related to incomplete option parsing
|
||||
byte[] addParent = new BytesArray(
|
||||
"{" +
|
||||
" \"index\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"parent\"," +
|
||||
" \"_id\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"field1\" : \"value1\"" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
byte[] addChild = new BytesArray("{ \"update\" : { \"_index\" : \"test\", \"_type\" : \"child\", \"_id\" : \"child1\", \"parent\" : \"parent1\"}}\n" +
|
||||
"{\"doc\" : { \"field1\" : \"value1\"}, \"doc_as_upsert\" : \"true\"}\n").array();
|
||||
byte[] addChild = new BytesArray(
|
||||
"{" +
|
||||
" \"update\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"child\"," +
|
||||
" \"_id\" : \"child1\"," +
|
||||
" \"parent\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"doc\" : {" +
|
||||
" \"field1\" : \"value1\"" +
|
||||
" }," +
|
||||
" \"doc_as_upsert\" : \"true\"" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
builder.add(addParent, 0, addParent.length);
|
||||
builder.add(addChild, 0, addChild.length);
|
||||
@ -452,14 +529,57 @@ public class BulkTests extends ESIntegTestCase {
|
||||
|
||||
BulkRequestBuilder builder = client().prepareBulk();
|
||||
|
||||
byte[] addParent = new BytesArray("{\"index\" : { \"_index\" : \"test\", \"_type\" : \"parent\", \"_id\" : \"parent1\"}}\n" +
|
||||
"{\"field1\" : \"value1\"}\n").array();
|
||||
byte[] addParent = new BytesArray(
|
||||
"{" +
|
||||
" \"index\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"parent\"," +
|
||||
" \"_id\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"field1\" : \"value1\"" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
byte[] addChild1 = new BytesArray("{\"update\" : { \"_id\" : \"child1\", \"_type\" : \"child\", \"_index\" : \"test\", \"parent\" : \"parent1\"} }\n" +
|
||||
"{ \"script\" : {\"inline\" : \"ctx._source.field2 = 'value2'\"}, \"upsert\" : {\"field1\" : \"value1\"}}\n").array();
|
||||
byte[] addChild1 = new BytesArray(
|
||||
"{" +
|
||||
" \"update\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"child\"," +
|
||||
" \"_id\" : \"child1\"," +
|
||||
" \"parent\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"script\" : {" +
|
||||
" \"inline\" : \"ctx._source.field2 = 'value2'\"" +
|
||||
" }," +
|
||||
" \"upsert\" : {" +
|
||||
" \"field1\" : \"value1'\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
byte[] addChild2 = new BytesArray("{\"update\" : { \"_id\" : \"child1\", \"_type\" : \"child\", \"_index\" : \"test\", \"parent\" : \"parent1\"} }\n" +
|
||||
"{ \"script\" : \"ctx._source.field2 = 'value2'\", \"upsert\" : {\"field1\" : \"value1\"}}\n").array();
|
||||
byte[] addChild2 = new BytesArray(
|
||||
"{" +
|
||||
" \"update\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"child\"," +
|
||||
" \"_id\" : \"child1\"," +
|
||||
" \"parent\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"script\" : \"ctx._source.field2 = 'value2'\"," +
|
||||
" \"upsert\" : {" +
|
||||
" \"field1\" : \"value1'\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
builder.add(addParent, 0, addParent.length);
|
||||
builder.add(addChild1, 0, addChild1.length);
|
||||
@ -490,15 +610,48 @@ public class BulkTests extends ESIntegTestCase {
|
||||
|
||||
BulkRequestBuilder builder = client().prepareBulk();
|
||||
|
||||
byte[] addParent = new BytesArray("{\"index\" : { \"_index\" : \"test\", \"_type\" : \"parent\", \"_id\" : \"parent1\"}}\n"
|
||||
+ "{\"field1\" : \"value1\"}\n").array();
|
||||
byte[] addParent = new BytesArray(
|
||||
"{" +
|
||||
" \"index\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"parent\"," +
|
||||
" \"_id\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"field1\" : \"value1\"" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
byte[] addChildOK = new BytesArray(
|
||||
"{\"index\" : { \"_id\" : \"child1\", \"_type\" : \"child\", \"_index\" : \"test\", \"parent\" : \"parent1\"} }\n"
|
||||
+ "{ \"field1\" : \"value1\"}\n").array();
|
||||
"{" +
|
||||
" \"index\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"child\"," +
|
||||
" \"_id\" : \"child1\"," +
|
||||
" \"parent\" : \"parent1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"field1\" : \"value1\"" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
byte[] addChildMissingRouting = new BytesArray(
|
||||
"{\"index\" : { \"_id\" : \"child2\", \"_type\" : \"child\", \"_index\" : \"test\"} }\n" + "{ \"field1\" : \"value1\"}\n")
|
||||
.array();
|
||||
"{" +
|
||||
" \"index\" : {" +
|
||||
" \"_index\" : \"test\"," +
|
||||
" \"_type\" : \"child\"," +
|
||||
" \"_id\" : \"child1\"" +
|
||||
" }" +
|
||||
"}" +
|
||||
"\n" +
|
||||
"{" +
|
||||
" \"field1\" : \"value1\"" +
|
||||
"}" +
|
||||
"\n").array();
|
||||
|
||||
builder.add(addParent, 0, addParent.length);
|
||||
builder.add(addChildOK, 0, addChildOK.length);
|
||||
@ -523,19 +676,16 @@ public class BulkTests extends ESIntegTestCase {
|
||||
|
||||
for (int i = 0; i < responses.length; i++) {
|
||||
final int threadID = i;
|
||||
threads[threadID] = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
cyclicBarrier.await();
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
BulkRequestBuilder requestBuilder = client().prepareBulk();
|
||||
requestBuilder.add(client().prepareUpdate("test", "type", "1").setVersion(1).setDoc("field", threadID));
|
||||
responses[threadID] = requestBuilder.get();
|
||||
|
||||
threads[threadID] = new Thread(() -> {
|
||||
try {
|
||||
cyclicBarrier.await();
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
BulkRequestBuilder requestBuilder = client().prepareBulk();
|
||||
requestBuilder.add(client().prepareUpdate("test", "type", "1").setVersion(1).setDoc("field", threadID));
|
||||
responses[threadID] = requestBuilder.get();
|
||||
|
||||
});
|
||||
threads[threadID].start();
|
||||
|
||||
@ -613,7 +763,6 @@ public class BulkTests extends ESIntegTestCase {
|
||||
assertThat(bulkItemResponse.getItems()[5].getOpType(), is("delete"));
|
||||
}
|
||||
|
||||
|
||||
private static String indexOrAlias() {
|
||||
return randomBoolean() ? "test" : "alias";
|
||||
}
|
@ -49,15 +49,10 @@ public class AggregationTestScriptsPlugin extends MockScriptPlugin {
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
|
||||
|
||||
scripts.put("20 - _value", vars -> {
|
||||
double value = (double) vars.get("_value");
|
||||
return 20.0d - value;
|
||||
});
|
||||
|
||||
scripts.put("_value - 1", vars -> {
|
||||
double value = (double) vars.get("_value");
|
||||
return value - 1.0d;
|
||||
});
|
||||
scripts.put("20 - _value", vars -> 20.0d - (double) vars.get("_value"));
|
||||
scripts.put("_value - 1", vars -> (double) vars.get("_value") - 1);
|
||||
scripts.put("_value + 1", vars -> (double) vars.get("_value") + 1);
|
||||
scripts.put("_value * -1", vars -> (double) vars.get("_value") * -1);
|
||||
|
||||
scripts.put("_value - dec", vars -> {
|
||||
double value = (double) vars.get("_value");
|
||||
@ -65,6 +60,12 @@ public class AggregationTestScriptsPlugin extends MockScriptPlugin {
|
||||
return value - dec;
|
||||
});
|
||||
|
||||
scripts.put("_value + inc", vars -> {
|
||||
double value = (double) vars.get("_value");
|
||||
int inc = (int) vars.get("inc");
|
||||
return value + inc;
|
||||
});
|
||||
|
||||
scripts.put("doc['value'].value", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
return doc.get("value");
|
||||
@ -77,6 +78,13 @@ public class AggregationTestScriptsPlugin extends MockScriptPlugin {
|
||||
return value.getValue() - dec;
|
||||
});
|
||||
|
||||
scripts.put("doc['value'].value + inc", vars -> {
|
||||
int inc = (int) vars.get("inc");
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Longs value = (ScriptDocValues.Longs) doc.get("value");
|
||||
return value.getValue() + inc;
|
||||
});
|
||||
|
||||
scripts.put("doc['values'].values", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
return doc.get("values");
|
||||
@ -94,7 +102,17 @@ public class AggregationTestScriptsPlugin extends MockScriptPlugin {
|
||||
return res;
|
||||
});
|
||||
|
||||
scripts.put("_value * -1", vars -> (double) vars.get("_value") * -1);
|
||||
scripts.put("[ doc['value'].value, doc['value'].value - dec ]", vars -> {
|
||||
Long a = ((ScriptDocValues.Longs) scripts.get("doc['value'].value").apply(vars)).getValue();
|
||||
Long b = (Long) scripts.get("doc['value'].value - dec").apply(vars);
|
||||
return new Long[]{a, b};
|
||||
});
|
||||
|
||||
scripts.put("[ doc['value'].value, doc['value'].value + inc ]", vars -> {
|
||||
Long a = ((ScriptDocValues.Longs) scripts.get("doc['value'].value").apply(vars)).getValue();
|
||||
Long b = (Long) scripts.get("doc['value'].value + inc").apply(vars);
|
||||
return new Long[]{a, b};
|
||||
});
|
||||
|
||||
return scripts;
|
||||
}
|
||||
|
@ -16,18 +16,18 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.bucket;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.ScoreAccessor;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
|
||||
import org.elasticsearch.search.aggregations.bucket.AbstractTermsTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
@ -50,10 +50,13 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
|
||||
import static org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.avg;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
|
||||
@ -69,15 +72,47 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@ESIntegTestCase.SuiteScopeTestCase
|
||||
public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
public class DoubleTermsIT extends AbstractTermsTestCase {
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
public static class CustomScriptPlugin extends AggregationTestScriptsPlugin {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = super.pluginScripts();
|
||||
|
||||
scripts.put("(long) (_value / 1000 + 1)", vars -> (long) ((double) vars.get("_value") / 1000 + 1));
|
||||
|
||||
scripts.put("doc['" + MULTI_VALUED_FIELD_NAME + "']", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
return doc.get(MULTI_VALUED_FIELD_NAME);
|
||||
});
|
||||
|
||||
scripts.put("doc['" + MULTI_VALUED_FIELD_NAME + "'].value", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Doubles value = (ScriptDocValues.Doubles) doc.get(MULTI_VALUED_FIELD_NAME);
|
||||
return value.getValue();
|
||||
});
|
||||
|
||||
scripts.put("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Doubles value = (ScriptDocValues.Doubles) doc.get(SINGLE_VALUED_FIELD_NAME);
|
||||
return value.getValue();
|
||||
});
|
||||
|
||||
scripts.put("ceil(_score.doubleValue()/3)", vars -> {
|
||||
ScoreAccessor score = (ScoreAccessor) vars.get("_score");
|
||||
return Math.ceil(score.doubleValue() / 3);
|
||||
});
|
||||
|
||||
return scripts;
|
||||
}
|
||||
}
|
||||
|
||||
private static final int NUM_DOCS = 5; // TODO: randomize the size?
|
||||
@ -415,8 +450,8 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
.addAggregation(terms("terms")
|
||||
.field(SINGLE_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("_value + 1")))
|
||||
.execute().actionGet();
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -468,8 +503,8 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
.addAggregation(terms("terms")
|
||||
.field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("_value + 1")))
|
||||
.execute().actionGet();
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -497,8 +532,8 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
.addAggregation(terms("terms")
|
||||
.field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("(long) (_value / 1000 + 1)")))
|
||||
.execute().actionGet();
|
||||
.script(new Script("(long) (_value / 1000 + 1)", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -537,8 +572,11 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
|
||||
new Script("doc['" + MULTI_VALUED_FIELD_NAME + "'].value"))).execute().actionGet();
|
||||
terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("doc['" + MULTI_VALUED_FIELD_NAME + "'].value", ScriptType.INLINE,
|
||||
CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -561,8 +599,11 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
|
||||
new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']"))).execute().actionGet();
|
||||
terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']", ScriptType.INLINE,
|
||||
CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -1035,14 +1076,20 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
}
|
||||
|
||||
public void testScriptScore() {
|
||||
Script scoringScript =
|
||||
new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", ScriptType.INLINE, CustomScriptPlugin .NAME, null);
|
||||
|
||||
Script aggregationScript = new Script("ceil(_score.doubleValue()/3)", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client()
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.setQuery(
|
||||
functionScoreQuery(ScoreFunctionBuilders.scriptFunction(new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value"))))
|
||||
.setQuery(functionScoreQuery(scriptFunction(scoringScript)))
|
||||
.addAggregation(
|
||||
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).script(
|
||||
new Script("ceil(_score.doubleValue()/3)"))).execute().actionGet();
|
||||
terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(aggregationScript))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -1087,7 +1134,9 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||
|
||||
public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception {
|
||||
double[] expectedKeys = new double[] { 2, 1, 4, 5, 3, 6, 7 };
|
||||
assertMultiSortResponse(expectedKeys, Terms.Order.count(false), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false));
|
||||
assertMultiSortResponse(expectedKeys, Terms.Order.count(false),
|
||||
Terms.Order.aggregation("sum_d", false),
|
||||
Terms.Order.aggregation("avg_l", false));
|
||||
}
|
||||
|
||||
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscAsCompound() throws Exception {
|
@ -16,17 +16,17 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.bucket;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
|
||||
import org.elasticsearch.search.aggregations.bucket.AbstractTermsTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
@ -49,9 +49,11 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.avg;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
|
||||
@ -67,15 +69,36 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@ESIntegTestCase.SuiteScopeTestCase
|
||||
public class LongTermsTests extends AbstractTermsTestCase {
|
||||
public class LongTermsIT extends AbstractTermsTestCase {
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
public static class CustomScriptPlugin extends AggregationTestScriptsPlugin {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = super.pluginScripts();
|
||||
|
||||
scripts.put("floor(_value / 1000 + 1)", vars -> Math.floor((double) vars.get("_value") / 1000 + 1));
|
||||
|
||||
scripts.put("doc['" + MULTI_VALUED_FIELD_NAME + "']", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
return doc.get(MULTI_VALUED_FIELD_NAME);
|
||||
});
|
||||
|
||||
scripts.put("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Longs value = (ScriptDocValues.Longs) doc.get(SINGLE_VALUED_FIELD_NAME);
|
||||
return value.getValue();
|
||||
});
|
||||
|
||||
return scripts;
|
||||
}
|
||||
}
|
||||
|
||||
private static final int NUM_DOCS = 5; // TODO randomize the size?
|
||||
@ -418,8 +441,8 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
.addAggregation(terms("terms")
|
||||
.field(SINGLE_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("_value + 1")))
|
||||
.execute().actionGet();
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -471,8 +494,8 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
.addAggregation(terms("terms")
|
||||
.field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("_value - 1")))
|
||||
.execute().actionGet();
|
||||
.script(new Script("_value - 1", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -500,8 +523,8 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
.addAggregation(terms("terms")
|
||||
.field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("floor(_value / 1000 + 1)")))
|
||||
.execute().actionGet();
|
||||
.script(new Script("floor(_value / 1000 + 1)", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -536,12 +559,13 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
*/
|
||||
|
||||
public void testScriptSingleValue() throws Exception {
|
||||
Script script = new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||
.addAggregation(terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(
|
||||
new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value")))
|
||||
.execute().actionGet();
|
||||
.script(script))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -561,12 +585,13 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
}
|
||||
|
||||
public void testScriptMultiValued() throws Exception {
|
||||
Script script = new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||
.addAggregation(terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(
|
||||
new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']")))
|
||||
.execute().actionGet();
|
||||
.script(script))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -711,9 +736,11 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
.field(SINGLE_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.order(Terms.Order.aggregation("avg_i", asc))
|
||||
.subAggregation(avg("avg_i").field(SINGLE_VALUED_FIELD_NAME)).subAggregation(terms("subTerms").field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values())))
|
||||
).execute().actionGet();
|
||||
.subAggregation(
|
||||
avg("avg_i").field(SINGLE_VALUED_FIELD_NAME))
|
||||
.subAggregation(
|
||||
terms("subTerms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())))
|
||||
).get();
|
||||
|
||||
|
||||
assertSearchResponse(response);
|
||||
@ -753,8 +780,8 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
.field("num_tag")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.order(Terms.Order.aggregation("filter", asc))
|
||||
.subAggregation(filter("filter", QueryBuilders.matchAllQuery()))
|
||||
).execute().actionGet();
|
||||
.subAggregation(filter("filter", QueryBuilders.matchAllQuery()))
|
||||
).get();
|
||||
|
||||
|
||||
assertSearchResponse(response);
|
||||
@ -1064,7 +1091,9 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||
|
||||
public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception {
|
||||
long[] expectedKeys = new long[] { 2, 1, 4, 5, 3, 6, 7 };
|
||||
assertMultiSortResponse(expectedKeys, Terms.Order.count(false), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false));
|
||||
assertMultiSortResponse(expectedKeys, Terms.Order.count(false),
|
||||
Terms.Order.aggregation("sum_d", false),
|
||||
Terms.Order.aggregation("avg_l", false));
|
||||
}
|
||||
|
||||
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscAsCompound() throws Exception {
|
@ -17,7 +17,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.bucket;
|
||||
|
||||
import com.carrotsearch.hppc.LongHashSet;
|
||||
import com.carrotsearch.hppc.LongSet;
|
||||
@ -25,12 +25,13 @@ import com.carrotsearch.randomizedtesting.generators.RandomStrings;
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
|
||||
import org.elasticsearch.search.aggregations.bucket.AbstractTermsTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
@ -44,10 +45,13 @@ import org.joda.time.format.DateTimeFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -58,18 +62,45 @@ import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllSuccessful;
|
||||
|
||||
|
||||
@ESIntegTestCase.SuiteScopeTestCase
|
||||
public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
public class MinDocCountIT extends AbstractTermsTestCase {
|
||||
|
||||
private static final QueryBuilder QUERY = QueryBuilders.termQuery("match", true);
|
||||
private static int cardinality;
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
private static final QueryBuilder QUERY = QueryBuilders.termQuery("match", true);
|
||||
public static class CustomScriptPlugin extends AggregationTestScriptsPlugin {
|
||||
|
||||
private static int cardinality;
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
|
||||
|
||||
scripts.put("doc['d'].values", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Doubles value = (ScriptDocValues.Doubles) doc.get("d");
|
||||
return value.getValues();
|
||||
});
|
||||
|
||||
scripts.put("doc['l'].values", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Longs value = (ScriptDocValues.Longs) doc.get("l");
|
||||
return value.getValues();
|
||||
});
|
||||
|
||||
scripts.put("doc['s'].values", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Strings value = (ScriptDocValues.Strings) doc.get("s");
|
||||
return value.getValues();
|
||||
});
|
||||
|
||||
return scripts;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupSuiteScopeCluster() throws Exception {
|
||||
@ -90,7 +121,8 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
longTerm = randomInt(cardinality * 2);
|
||||
} while (!longTerms.add(longTerm));
|
||||
double doubleTerm = longTerm * Math.PI;
|
||||
String dateTerm = DateTimeFormat.forPattern("yyyy-MM-dd").print(new DateTime(2014, 1, ((int) longTerm % 20) + 1, 0, 0, DateTimeZone.UTC));
|
||||
String dateTerm = DateTimeFormat.forPattern("yyyy-MM-dd")
|
||||
.print(new DateTime(2014, 1, ((int) longTerm % 20) + 1, 0, 0, DateTimeZone.UTC));
|
||||
final int frequency = randomBoolean() ? 1 : randomIntBetween(2, 20);
|
||||
for (int j = 0; j < frequency; ++j) {
|
||||
indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder()
|
||||
@ -119,7 +151,8 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
YES {
|
||||
@Override
|
||||
TermsAggregationBuilder apply(TermsAggregationBuilder builder, String field) {
|
||||
return builder.script(new org.elasticsearch.script.Script("doc['" + field + "'].values"));
|
||||
return builder.script(new org.elasticsearch.script.Script("doc['" + field + "'].values", ScriptService.ScriptType.INLINE,
|
||||
CustomScriptPlugin.NAME, null));
|
||||
}
|
||||
};
|
||||
abstract TermsAggregationBuilder apply(TermsAggregationBuilder builder, String field);
|
||||
@ -272,7 +305,7 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
testMinDocCountOnTerms(field, script, order, null, true);
|
||||
}
|
||||
|
||||
private void testMinDocCountOnTerms(String field, Script script, Terms.Order order, String include, boolean retryOnFailure) throws Exception {
|
||||
private void testMinDocCountOnTerms(String field, Script script, Terms.Order order, String include, boolean retry) throws Exception {
|
||||
// all terms
|
||||
final SearchResponse allTermsResponse = client().prepareSearch("idx").setTypes("type")
|
||||
.setSize(0)
|
||||
@ -307,7 +340,7 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
assertAllSuccessful(response);
|
||||
assertSubset(allTerms, (Terms) response.getAggregations().get("terms"), minDocCount, size, include);
|
||||
} catch (AssertionError ae) {
|
||||
if (!retryOnFailure) {
|
||||
if (!retry) {
|
||||
throw ae;
|
||||
}
|
||||
logger.info("test failed. trying to see if it recovers after 1m.", ae);
|
||||
@ -380,8 +413,13 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
final SearchResponse allResponse = client().prepareSearch("idx").setTypes("type")
|
||||
.setSize(0)
|
||||
.setQuery(QUERY)
|
||||
.addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).order(order).minDocCount(0))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
dateHistogram("histo")
|
||||
.field("date")
|
||||
.dateHistogramInterval(DateHistogramInterval.DAY)
|
||||
.order(order)
|
||||
.minDocCount(0))
|
||||
.get();
|
||||
|
||||
final Histogram allHisto = allResponse.getAggregations().get("histo");
|
||||
|
||||
@ -389,9 +427,14 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||
final SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||
.setSize(0)
|
||||
.setQuery(QUERY)
|
||||
.addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).order(order).minDocCount(minDocCount))
|
||||
.execute().actionGet();
|
||||
assertSubset(allHisto, (Histogram) response.getAggregations().get("histo"), minDocCount);
|
||||
.addAggregation(
|
||||
dateHistogram("histo")
|
||||
.field("date")
|
||||
.dateHistogramInterval(DateHistogramInterval.DAY)
|
||||
.order(order)
|
||||
.minDocCount(minDocCount))
|
||||
.get();
|
||||
assertSubset(allHisto, response.getAggregations().get("histo"), minDocCount);
|
||||
}
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.bucket;
|
||||
|
||||
import org.apache.lucene.util.automaton.RegExp;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
@ -24,14 +24,14 @@ import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
||||
import org.elasticsearch.index.mapper.internal.IndexFieldMapper;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationExecutionException;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
|
||||
import org.elasticsearch.search.aggregations.bucket.AbstractTermsTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
@ -56,10 +56,12 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.avg;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.count;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats;
|
||||
@ -76,18 +78,41 @@ import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
import static org.hamcrest.core.IsNull.nullValue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@ESIntegTestCase.SuiteScopeTestCase
|
||||
public class StringTermsTests extends AbstractTermsTestCase {
|
||||
public class StringTermsIT extends AbstractTermsTestCase {
|
||||
|
||||
private static final String SINGLE_VALUED_FIELD_NAME = "s_value";
|
||||
private static final String MULTI_VALUED_FIELD_NAME = "s_values";
|
||||
private static Map<String, Map<String, Object>> expectedMultiSortBuckets;
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
public static class CustomScriptPlugin extends AggregationTestScriptsPlugin {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = super.pluginScripts();
|
||||
|
||||
scripts.put("'foo_' + _value", vars -> "foo_" + (String) vars.get("_value"));
|
||||
scripts.put("_value.substring(0,3)", vars -> ((String) vars.get("_value")).substring(0, 3));
|
||||
|
||||
scripts.put("doc['" + MULTI_VALUED_FIELD_NAME + "']", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
return doc.get(MULTI_VALUED_FIELD_NAME);
|
||||
});
|
||||
|
||||
scripts.put("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Strings value = (ScriptDocValues.Strings) doc.get(SINGLE_VALUED_FIELD_NAME);
|
||||
return value.getValue();
|
||||
});
|
||||
|
||||
return scripts;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -435,8 +460,11 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("high_card_type")
|
||||
.addAggregation(
|
||||
terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).size(20)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.term(true))) // we need to sort by terms cause we're checking the first 20 values
|
||||
terms("terms")
|
||||
.executionHint(randomExecutionHint())
|
||||
.field(SINGLE_VALUED_FIELD_NAME).size(20)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.order(Terms.Order.term(true))) // we need to sort by terms cause we're checking the first 20 values
|
||||
.execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
@ -542,9 +570,12 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script("'foo_' + _value"))).execute()
|
||||
.actionGet();
|
||||
terms("terms")
|
||||
.executionHint(randomExecutionHint())
|
||||
.field(SINGLE_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("'foo_' + _value", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -566,9 +597,12 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script("_value.substring(0,3)")))
|
||||
.execute().actionGet();
|
||||
terms("terms")
|
||||
.executionHint(randomExecutionHint())
|
||||
.field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("_value.substring(0,3)", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -615,8 +649,11 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").executionHint(randomExecutionHint()).script(new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']"))
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
|
||||
terms("terms")
|
||||
.executionHint(randomExecutionHint())
|
||||
.script(new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']", ScriptType.INLINE, CustomScriptPlugin.NAME, null))
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values())))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -642,9 +679,12 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script("'foo_' + _value"))).execute()
|
||||
.actionGet();
|
||||
terms("terms")
|
||||
.executionHint(randomExecutionHint())
|
||||
.field(MULTI_VALUED_FIELD_NAME)
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.script(new Script("'foo_' + _value", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -678,12 +718,17 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
*/
|
||||
|
||||
public void testScriptSingleValue() throws Exception {
|
||||
Script script = new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client()
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
|
||||
.script(new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value"))).execute().actionGet();
|
||||
terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.executionHint(randomExecutionHint())
|
||||
.script(script))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -701,12 +746,17 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
}
|
||||
|
||||
public void testScriptSingleValueExplicitSingleValue() throws Exception {
|
||||
Script script = new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client()
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
|
||||
.script(new Script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value"))).execute().actionGet();
|
||||
terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.executionHint(randomExecutionHint())
|
||||
.script(script))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -728,8 +778,11 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("idx")
|
||||
.setTypes("type")
|
||||
.addAggregation(
|
||||
terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint())
|
||||
.script(new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']"))).execute().actionGet();
|
||||
terms("terms")
|
||||
.collectMode(randomFrom(SubAggCollectionMode.values()))
|
||||
.executionHint(randomExecutionHint())
|
||||
.script(new Script("doc['" + MULTI_VALUED_FIELD_NAME + "']", ScriptType.INLINE, CustomScriptPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
@ -821,7 +874,11 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.prepareSearch("empty_bucket_idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(1L).minDocCount(0).subAggregation(terms("terms").field("value")))
|
||||
histogram("histo")
|
||||
.field(SINGLE_VALUED_FIELD_NAME)
|
||||
.interval(1L)
|
||||
.minDocCount(0)
|
||||
.subAggregation(terms("terms").field("value")))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
|
||||
@ -1143,7 +1200,8 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||
.subAggregation(terms("values").field("i").collectMode(randomFrom(SubAggCollectionMode.values()))))
|
||||
.execute().actionGet();
|
||||
|
||||
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics or single-bucket type");
|
||||
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation " +
|
||||
"which is not of a metrics or single-bucket type");
|
||||
|
||||
} catch (ElasticsearchException e) {
|
||||
// expected
|
@ -16,20 +16,19 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.global.Global;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.missing.Missing;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
|
||||
import org.elasticsearch.search.aggregations.metrics.AbstractNumericTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
|
||||
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats.Bounds;
|
||||
|
||||
@ -53,13 +52,11 @@ import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
public class ExtendedStatsIT extends AbstractNumericTestCase {
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(AggregationTestScriptsPlugin.class);
|
||||
}
|
||||
|
||||
private static double stdDev(int... vals) {
|
||||
@ -298,7 +295,11 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
double sigma = randomDouble() * randomIntBetween(1, 10);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(extendedStats("stats").field("value").script(new Script("_value + 1")).sigma(sigma))
|
||||
.addAggregation(
|
||||
extendedStats("stats")
|
||||
.field("value")
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, null))
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -325,7 +326,9 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
extendedStats("stats").field("value").script(new Script("_value + inc", ScriptType.INLINE, null, params))
|
||||
extendedStats("stats")
|
||||
.field("value")
|
||||
.script(new Script("_value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params))
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
||||
@ -374,7 +377,11 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
double sigma = randomDouble() * randomIntBetween(1, 10);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(extendedStats("stats").field("values").script(new Script("_value - 1")).sigma(sigma))
|
||||
.addAggregation(
|
||||
extendedStats("stats")
|
||||
.field("values")
|
||||
.script(new Script("_value - 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, null))
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -401,9 +408,11 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
extendedStats("stats").field("values").script(new Script("_value - dec", ScriptType.INLINE, null, params))
|
||||
extendedStats("stats")
|
||||
.field("values")
|
||||
.script(new Script("_value - dec", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params))
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -426,7 +435,10 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
double sigma = randomDouble() * randomIntBetween(1, 10);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(extendedStats("stats").script(new Script("doc['value'].value")).sigma(sigma))
|
||||
.addAggregation(
|
||||
extendedStats("stats")
|
||||
.script(new Script("doc['value'].value", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, null))
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -449,11 +461,16 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
public void testScriptSingleValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
|
||||
Script script = new Script("doc['value'].value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
double sigma = randomDouble() * randomIntBetween(1, 10);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
extendedStats("stats").script(new Script("doc['value'].value + inc", ScriptType.INLINE, null, params)).sigma(sigma))
|
||||
extendedStats("stats")
|
||||
.script(script)
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -477,7 +494,10 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
double sigma = randomDouble() * randomIntBetween(1, 10);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(extendedStats("stats").script(new Script("doc['values'].values")).sigma(sigma))
|
||||
.addAggregation(
|
||||
extendedStats("stats")
|
||||
.script(new Script("doc['values'].values", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, null))
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -500,12 +520,16 @@ public class ExtendedStatsTests extends AbstractNumericTestCase {
|
||||
public void testScriptMultiValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("dec", 1);
|
||||
|
||||
Script script = new Script("[ doc['value'].value, doc['value'].value - dec ]", ScriptType.INLINE,
|
||||
AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
double sigma = randomDouble() * randomIntBetween(1, 10);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
extendedStats("stats").script(
|
||||
new Script("[ doc['value'].value, doc['value'].value - dec ]", ScriptType.INLINE, null, params))
|
||||
extendedStats("stats")
|
||||
.script(script)
|
||||
.sigma(sigma))
|
||||
.execute().actionGet();
|
||||
|
@ -16,26 +16,27 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.global.Global;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
|
||||
import org.elasticsearch.search.aggregations.metrics.AbstractNumericTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.max.Max;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
|
||||
@ -50,10 +51,10 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class MaxTests extends AbstractNumericTestCase {
|
||||
public class MaxIT extends AbstractNumericTestCase {
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(AggregationTestScriptsPlugin.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -161,7 +162,10 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
public void testSingleValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").field("value").script(new Script("_value + 1")))
|
||||
.addAggregation(
|
||||
max("max")
|
||||
.field("value")
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -178,8 +182,11 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
params.put("inc", 1);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").field("value").script(new Script("_value + inc", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
max("max")
|
||||
.field("value")
|
||||
.script(new Script("_value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -208,8 +215,11 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
public void testMultiValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").field("values").script(new Script("_value + 1")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
max("max")
|
||||
.field("values")
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -225,8 +235,11 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
params.put("inc", 1);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").field("values").script(new Script("_value + inc", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
max("max")
|
||||
.field("values")
|
||||
.script(new Script("_value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -240,7 +253,9 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
public void testScriptSingleValued() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").script(new Script("doc['value'].value")))
|
||||
.addAggregation(
|
||||
max("max")
|
||||
.script(new Script("doc['value'].value", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.execute().actionGet();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
@ -255,10 +270,13 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
public void testScriptSingleValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
|
||||
Script script = new Script("doc['value'].value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").script(new Script("doc['value'].value + inc", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(max("max").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -272,8 +290,10 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
public void testScriptMultiValued() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(max("max").script(new Script("doc['values'].values")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
max("max")
|
||||
.script(new Script("doc['values'].values", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, null)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -287,10 +307,13 @@ public class MaxTests extends AbstractNumericTestCase {
|
||||
public void testScriptMultiValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
|
||||
Script script = new Script("[ doc['value'].value, doc['value'].value + inc ]", ScriptType.INLINE,
|
||||
AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
max("max").script(new Script("[ doc['value'].value, doc['value'].value + inc ]", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(max("max").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
@ -16,19 +16,18 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.global.Global;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
|
||||
import org.elasticsearch.search.aggregations.metrics.AbstractNumericTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.min.Min;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -37,6 +36,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
|
||||
@ -51,10 +51,10 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class MinTests extends AbstractNumericTestCase {
|
||||
public class MinIT extends AbstractNumericTestCase {
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(AggregationTestScriptsPlugin.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -163,8 +163,11 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
public void testSingleValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").field("value").script(new Script("_value - 1")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
min("min")
|
||||
.field("value")
|
||||
.script(new Script("_value - 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -178,10 +181,13 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("dec", 1);
|
||||
|
||||
Script script = new Script("_value - dec", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").field("value").script(new Script("_value - dec", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(min("min").field("value").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -210,7 +216,11 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
public void testMultiValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").field("values").script(new Script("_value - 1"))).execute().actionGet();
|
||||
.addAggregation(
|
||||
min("min")
|
||||
.field("values")
|
||||
.script(new Script("_value - 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -224,7 +234,11 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
// test what happens when values arrive in reverse order since the min
|
||||
// aggregator is optimized to work on sorted values
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").field("values").script(new Script("_value * -1"))).execute().actionGet();
|
||||
.addAggregation(
|
||||
min("min")
|
||||
.field("values")
|
||||
.script(new Script("_value * -1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -238,9 +252,12 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("dec", 1);
|
||||
|
||||
Script script = new Script("_value - dec", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").field("values").script(new Script("_value - dec", ScriptType.INLINE, null, params))).execute()
|
||||
.actionGet();
|
||||
.addAggregation(min("min").field("values").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -252,8 +269,11 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
|
||||
@Override
|
||||
public void testScriptSingleValued() throws Exception {
|
||||
Script script = new Script("doc['value'].value", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap());
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").script(new Script("doc['value'].value"))).execute().actionGet();
|
||||
.addAggregation(min("min").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -267,9 +287,12 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
public void testScriptSingleValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("dec", 1);
|
||||
|
||||
Script script = new Script("doc['value'].value - dec", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").script(new Script("doc['value'].value - dec", ScriptType.INLINE, null, params))).execute()
|
||||
.actionGet();
|
||||
.addAggregation(min("min").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -281,8 +304,10 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
|
||||
@Override
|
||||
public void testScriptMultiValued() throws Exception {
|
||||
Script script = new Script("doc['values'].values", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap());
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(min("min").script(new Script("doc['values'].values"))).execute().actionGet();
|
||||
.addAggregation(min("min").script(script))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
@ -296,14 +321,12 @@ public class MinTests extends AbstractNumericTestCase {
|
||||
public void testScriptMultiValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("dec", 1);
|
||||
|
||||
SearchResponse searchResponse = client()
|
||||
.prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
min("min")
|
||||
.script(new Script(
|
||||
"List values = doc['values'].values; double[] res = new double[values.size()]; for (int i = 0; i < res.length; i++) { res[i] = values.get(i) - dec; }; return res;",
|
||||
ScriptType.INLINE, null, params))).execute().actionGet();
|
||||
.addAggregation(min("min").script(AggregationTestScriptsPlugin.DECREMENT_ALL_VALUES))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
@ -16,20 +16,19 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.ShardSearchFailure;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.AggregationTestScriptsPlugin;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.global.Global;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
|
||||
import org.elasticsearch.search.aggregations.metrics.AbstractNumericTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -38,6 +37,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
|
||||
@ -51,13 +51,10 @@ import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class StatsTests extends AbstractNumericTestCase {
|
||||
public class StatsIT extends AbstractNumericTestCase {
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(AggregationTestScriptsPlugin.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -224,8 +221,11 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
public void testSingleValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").field("value").script(new Script("_value + 1")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
stats("stats")
|
||||
.field("value")
|
||||
.script(new Script("_value + 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -247,8 +247,11 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
params.put("inc", 1);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").field("value").script(new Script("_value + inc", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
stats("stats")
|
||||
.field("value")
|
||||
.script(new Script("_value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params)))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -289,8 +292,11 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
public void testMultiValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").field("values").script(new Script("_value - 1")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
stats("stats")
|
||||
.field("values")
|
||||
.script(new Script("_value - 1", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -312,8 +318,11 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
params.put("dec", 1);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").field("values").script(new Script("_value - dec", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
stats("stats")
|
||||
.field("values")
|
||||
.script(new Script("_value - dec", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params)))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -333,8 +342,10 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
public void testScriptSingleValued() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").script(new Script("doc['value'].value")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(
|
||||
stats("stats")
|
||||
.script(new Script("doc['value'].value", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap())))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -354,10 +365,13 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
public void testScriptSingleValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
|
||||
Script script = new Script("doc['value'].value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").script(new Script("doc['value'].value + inc", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
.addAggregation(stats("stats").script(script))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -375,10 +389,12 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
|
||||
@Override
|
||||
public void testScriptMultiValued() throws Exception {
|
||||
Script script = new Script("doc['values'].values", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, emptyMap());
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(stats("stats").script(new Script("doc['values'].values")))
|
||||
.execute().actionGet();
|
||||
.addAggregation(stats("stats").script(script))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
||||
@ -398,12 +414,16 @@ public class StatsTests extends AbstractNumericTestCase {
|
||||
public void testScriptMultiValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("dec", 1);
|
||||
|
||||
Script script = new Script("[ doc['value'].value, doc['value'].value - dec ]", ScriptType.INLINE,
|
||||
AggregationTestScriptsPlugin.NAME, params);
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(
|
||||
stats("stats").script(
|
||||
new Script("[ doc['value'].value, doc['value'].value - dec ]", ScriptType.INLINE, null, params)))
|
||||
.execute().actionGet();
|
||||
stats("stats")
|
||||
.script(script))
|
||||
.get();
|
||||
|
||||
assertShardExecutionState(searchResponse, 0);
|
||||
|
@ -17,32 +17,38 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.messy.tests;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
package org.elasticsearch.search.functionscore;
|
||||
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.lucene.search.function.CombineFunction;
|
||||
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
|
||||
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
||||
import org.elasticsearch.index.query.MatchAllQueryBuilder;
|
||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.MockScriptPlugin;
|
||||
import org.elasticsearch.script.ScoreAccessor;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.client.Requests.searchRequest;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
|
||||
import static org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
@ -50,27 +56,54 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSear
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class FunctionScoreTests extends ESIntegTestCase {
|
||||
public class FunctionScoreIT extends ESIntegTestCase {
|
||||
|
||||
static final String TYPE = "type";
|
||||
static final String INDEX = "index";
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
return Collections.singleton(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
public static class CustomScriptPlugin extends MockScriptPlugin {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
|
||||
scripts.put("1", vars -> 1.0d);
|
||||
scripts.put("get score value", vars -> ((ScoreAccessor) vars.get("_score")).doubleValue());
|
||||
scripts.put("return (doc['num'].value)", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Longs num = (ScriptDocValues.Longs) doc.get("num");
|
||||
return num.getValue();
|
||||
});
|
||||
scripts.put("doc['random_score']", vars -> {
|
||||
Map<?, ?> doc = (Map) vars.get("doc");
|
||||
ScriptDocValues.Doubles randomScore = (ScriptDocValues.Doubles) doc.get("random_score");
|
||||
return randomScore.getValue();
|
||||
});
|
||||
return scripts;
|
||||
}
|
||||
}
|
||||
|
||||
public void testScriptScoresNested() throws IOException {
|
||||
createIndex(INDEX);
|
||||
index(INDEX, TYPE, "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
|
||||
refresh();
|
||||
|
||||
Script scriptOne = new Script("1", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
Script scriptTwo = new Script("get score value", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client().search(
|
||||
searchRequest().source(
|
||||
searchSource().query(
|
||||
functionScoreQuery(
|
||||
functionScoreQuery(
|
||||
functionScoreQuery(scriptFunction(new Script("1"))),
|
||||
scriptFunction(new Script("_score.doubleValue()"))),
|
||||
scriptFunction(new Script("_score.doubleValue()"))
|
||||
functionScoreQuery(scriptFunction(scriptOne)),
|
||||
scriptFunction(scriptTwo)),
|
||||
scriptFunction(scriptTwo)
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -83,10 +116,14 @@ public class FunctionScoreTests extends ESIntegTestCase {
|
||||
createIndex(INDEX);
|
||||
index(INDEX, TYPE, "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
|
||||
refresh();
|
||||
|
||||
Script script = new Script("get score value", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
|
||||
SearchResponse response = client().search(
|
||||
searchRequest().source(
|
||||
searchSource().query(functionScoreQuery(scriptFunction(new Script("_score.doubleValue()")))).aggregation(
|
||||
terms("score_agg").script(new Script("_score.doubleValue()")))
|
||||
searchSource()
|
||||
.query(functionScoreQuery(scriptFunction(script)))
|
||||
.aggregation(terms("score_agg").script(script))
|
||||
)
|
||||
).actionGet();
|
||||
assertSearchResponse(response);
|
||||
@ -100,10 +137,17 @@ public class FunctionScoreTests extends ESIntegTestCase {
|
||||
refresh();
|
||||
float score = randomFloat();
|
||||
float minScore = randomFloat();
|
||||
|
||||
index(INDEX, TYPE, jsonBuilder().startObject()
|
||||
.field("num", 2)
|
||||
.field("random_score", score) // Pass the random score as a document field so that it can be extracted in the script
|
||||
.endObject());
|
||||
refresh();
|
||||
ensureYellow();
|
||||
|
||||
Script script = new Script("doc['random_score']", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
SearchResponse searchResponse = client().search(
|
||||
searchRequest().source(
|
||||
searchSource().query(
|
||||
functionScoreQuery(scriptFunction(new Script(Float.toString(score)))).setMinScore(minScore)))
|
||||
searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)))
|
||||
).actionGet();
|
||||
if (score < minScore) {
|
||||
assertThat(searchResponse.getHits().getTotalHits(), is(0L));
|
||||
@ -113,8 +157,8 @@ public class FunctionScoreTests extends ESIntegTestCase {
|
||||
|
||||
searchResponse = client().search(
|
||||
searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] {
|
||||
new FilterFunctionBuilder(scriptFunction(new Script(Float.toString(score)))),
|
||||
new FilterFunctionBuilder(scriptFunction(new Script(Float.toString(score))))
|
||||
new FilterFunctionBuilder(scriptFunction(script)),
|
||||
new FilterFunctionBuilder(scriptFunction(script))
|
||||
}).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)))
|
||||
).actionGet();
|
||||
if (score < minScore) {
|
||||
@ -133,7 +177,7 @@ public class FunctionScoreTests extends ESIntegTestCase {
|
||||
docs.add(client().prepareIndex(INDEX, TYPE, Integer.toString(i)).setSource("num", i + scoreOffset));
|
||||
}
|
||||
indexRandom(true, docs);
|
||||
Script script = new Script("return (doc['num'].value)");
|
||||
Script script = new Script("return (doc['num'].value)", ScriptType.INLINE, CustomScriptPlugin.NAME, null);
|
||||
int numMatchingDocs = numDocs + scoreOffset - minScore;
|
||||
if (numMatchingDocs < 0) {
|
||||
numMatchingDocs = 0;
|
@ -42,18 +42,12 @@
|
||||
^^^^^ note: the methods from this test using mustache were moved to the mustache module under its messy tests package.
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DateHistogramTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DateRangeTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/DoubleTermsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DoubleTermsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/ExtendedStatsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/FunctionScoreTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/geo/GeoDistanceIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/GeoDistanceTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IPv4RangeTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/script/IndexLookupIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IndexLookupTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/script/IndexedScriptIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IndexedScriptTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/InnerHitsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/LongTermsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MaxTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MinDocCountTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/metrics/MinIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MinTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/percolator/PercolatorIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/PercolatorTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/functionscore/RandomScoreFunctionIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/RandomScoreFunctionTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/RangeTests.java
|
||||
@ -65,8 +59,6 @@
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/SignificantTermsSignificanceScoreTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/nested/SimpleNestedIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/SimpleNestedTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/sort/SimpleSortIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/SimpleSortTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/StatsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/StringTermsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/StringTermsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/metrics/SumIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/SumTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/TopHitsTests.java
|
||||
renamed: core/src/test/java/org/elasticsearch/index/mapper/TransformOnIndexMapperIT.java -> plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/TransformOnIndexMapperTests.java
|
||||
|
@ -20,6 +20,7 @@
|
||||
package org.elasticsearch.script;
|
||||
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.search.Scorer;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.search.lookup.LeafSearchLookup;
|
||||
@ -183,6 +184,12 @@ public class MockScriptEngine implements ScriptEngineService {
|
||||
public void setNextVar(String name, Object value) {
|
||||
ctx.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScorer(Scorer scorer) {
|
||||
super.setScorer(scorer);
|
||||
ctx.put("_score", new ScoreAccessor(scorer));
|
||||
}
|
||||
};
|
||||
leafSearchScript.setLookup(leafLookup);
|
||||
return leafSearchScript;
|
||||
@ -190,7 +197,7 @@ public class MockScriptEngine implements ScriptEngineService {
|
||||
|
||||
@Override
|
||||
public boolean needsScores() {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user