test: fixed some messy tests

This commit is contained in:
Martijn van Groningen 2015-09-30 21:48:17 +02:00
parent d715dfd16c
commit dc858d2008
6 changed files with 91 additions and 165 deletions

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.messy.tests;
package org.elasticsearch.percolator;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.ShardOperationFailedException;
@ -32,7 +32,6 @@ import org.elasticsearch.action.percolate.PercolateSourceBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
@ -47,11 +46,7 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryShardException;
import org.elasticsearch.index.query.functionscore.weight.WeightBuilder;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.percolator.PercolatorService;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.groovy.GroovyPlugin;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.test.ESIntegTestCase;
@ -65,20 +60,15 @@ import static org.elasticsearch.common.settings.Settings.builder;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.fieldValueFactorFunction;
import static org.elasticsearch.percolator.PercolatorTestUtil.convertFromTextArray;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
/**
*
*/
public class PercolatorTests extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
}
public class PercolatorIT extends ESIntegTestCase {
@Test
public void testSimple1() throws Exception {
@ -1183,7 +1173,7 @@ public class PercolatorTests extends ESIntegTestCase {
.setScore(true)
.setSize(size)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.execute().actionGet();
assertMatchCount(response, numQueries);
assertThat(response.getMatches().length, equalTo(size));
@ -1200,7 +1190,7 @@ public class PercolatorTests extends ESIntegTestCase {
.setSortByScore(true)
.setSize(size)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.execute().actionGet();
assertMatchCount(response, numQueries);
assertThat(response.getMatches().length, equalTo(size));
@ -1224,7 +1214,7 @@ public class PercolatorTests extends ESIntegTestCase {
.setSize(size)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(
QueryBuilders.functionScoreQuery(matchQuery("field1", value), scriptFunction(new Script("doc['level'].value")))
QueryBuilders.functionScoreQuery(matchQuery("field1", value), fieldValueFactorFunction("level"))
.boostMode(
CombineFunction.REPLACE))
.execute().actionGet();
@ -1258,7 +1248,7 @@ public class PercolatorTests extends ESIntegTestCase {
.setSortByScore(true)
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.execute().actionGet();
assertMatchCount(response, 2l);
assertThat(response.getMatches()[0].getId().string(), equalTo("2"));
@ -1269,7 +1259,7 @@ public class PercolatorTests extends ESIntegTestCase {
response = client().preparePercolate().setIndices("my-index").setDocumentType("my-type")
.setSortByScore(true)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.execute().actionGet();
assertThat(response.getCount(), equalTo(0l));
assertThat(response.getShardFailures().length, greaterThan(0));
@ -1298,7 +1288,7 @@ public class PercolatorTests extends ESIntegTestCase {
PercolateResponse response = client().preparePercolate().setIndices("my-index").setDocumentType("my-type")
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.addSort(SortBuilders.fieldSort("level"))
.get();
@ -1316,7 +1306,7 @@ public class PercolatorTests extends ESIntegTestCase {
.setSortByScore(true)
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.execute().actionGet();
assertMatchCount(response, 0l);
}
@ -1336,7 +1326,7 @@ public class PercolatorTests extends ESIntegTestCase {
.setSortByScore(true)
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction(new Script("doc['level'].value"))))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), fieldValueFactorFunction("level")))
.execute().actionGet();
assertMatchCount(response, 0l);
}

View File

@ -19,7 +19,9 @@
package org.elasticsearch.script;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
@ -31,6 +33,27 @@ import java.util.Map;
public class MockScriptEngine implements ScriptEngineService {
public static final String NAME = "mockscript";
public static class TestPlugin extends Plugin {
public TestPlugin() {
}
@Override
public String name() {
return NAME;
}
@Override
public String description() {
return "Mock script engine for integration tests";
}
public void onModule(ScriptModule module) {
module.addScriptEngine(MockScriptEngine.class);
}
}
@Override
public String[] types() {
return new String[]{ NAME };
@ -58,7 +81,26 @@ public class MockScriptEngine implements ScriptEngineService {
@Override
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, @Nullable Map<String, Object> vars) {
return null;
return new SearchScript() {
@Override
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
AbstractSearchScript leafSearchScript = new AbstractSearchScript() {
@Override
public Object run() {
return compiledScript.compiled();
}
};
leafSearchScript.setLookup(lookup.getLeafSearchLookup(context));
return leafSearchScript;
}
@Override
public boolean needsScores() {
return false;
}
};
}
@Override

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.messy.tests;
package org.elasticsearch.search.aggregations.metrics;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.util.ArrayUtil;
@ -28,8 +28,9 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptEngine;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.groovy.GroovyPlugin;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
@ -84,14 +85,14 @@ import static org.hamcrest.Matchers.sameInstance;
*
*/
@ESIntegTestCase.SuiteScopeTestCase()
public class TopHitsTests extends ESIntegTestCase {
public class TopHitsIT extends ESIntegTestCase {
private static final String TERMS_AGGS_FIELD = "terms";
private static final String SORT_FIELD = "sort";
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
return Collections.singleton(MockScriptEngine.TestPlugin.class);
}
public static String randomExecutionHint() {
@ -151,6 +152,7 @@ public class TopHitsTests extends ESIntegTestCase {
.startObject()
.field("group", "a")
.field("text", "term x y z n rare")
.field("value", 1)
.endObject()));
builders.add(client().prepareIndex("idx", "field-collapsing", "3").setSource(jsonBuilder()
.startObject()
@ -171,6 +173,7 @@ public class TopHitsTests extends ESIntegTestCase {
.startObject()
.field("group", "b")
.field("text", "term rare")
.field("value", 3)
.endObject()));
builders.add(client().prepareIndex("idx", "field-collapsing", "7").setSource(jsonBuilder()
.startObject()
@ -186,6 +189,7 @@ public class TopHitsTests extends ESIntegTestCase {
.startObject()
.field("group", "c")
.field("text", "rare x term")
.field("value", 2)
.endObject()));
numArticles = scaledRandomIntBetween(10, 100);
@ -493,7 +497,7 @@ public class TopHitsTests extends ESIntegTestCase {
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).field("group")
.order(Terms.Order.aggregation("max_score", false)).subAggregation(topHits("hits").setSize(1))
.subAggregation(max("max_score").script(new Script("_score.doubleValue()")))).get();
.subAggregation(max("max_score").field("value"))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
@ -539,7 +543,7 @@ public class TopHitsTests extends ESIntegTestCase {
.addHighlightedField("text")
.setExplain(true)
.addFieldDataField("field1")
.addScriptField("script", new Script("doc['field1'].value"))
.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()))
.setFetchSource("text", null)
.setVersion(true)
)
@ -860,7 +864,7 @@ public class TopHitsTests extends ESIntegTestCase {
nested("to-comments").path("comments").subAggregation(
topHits("top-comments").setSize(1).addHighlightedField(hlField).setExplain(true)
.addFieldDataField("comments.user")
.addScriptField("script", new Script("doc['comments.user'].value")).setFetchSource("message", null)
.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap())).setFetchSource("message", null)
.setVersion(true).addSort("comments.date", SortOrder.ASC))).get();
assertHitCount(searchResponse, 2);
Nested nested = searchResponse.getAggregations().get("to-comments");
@ -892,7 +896,7 @@ public class TopHitsTests extends ESIntegTestCase {
assertThat(field.getValue().toString(), equalTo("a"));
field = searchHit.field("script");
assertThat(field.getValue().toString(), equalTo("a"));
assertThat(field.getValue().toString(), equalTo("5"));
assertThat(searchHit.sourceAsMap().size(), equalTo(1));
assertThat(searchHit.sourceAsMap().get("message").toString(), equalTo("some comment"));

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.messy.tests;
package org.elasticsearch.search.child;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
@ -34,12 +34,12 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.cache.IndexCacheModule;
import org.elasticsearch.index.mapper.MergeMappingException;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.query.HasChildQueryBuilder;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.groovy.GroovyPlugin;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
import org.elasticsearch.search.aggregations.bucket.global.Global;
@ -58,7 +58,7 @@ import java.util.*;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.fieldValueFactorFunction;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.weightFactorFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
@ -67,12 +67,7 @@ import static org.hamcrest.Matchers.*;
*
*/
@ClusterScope(scope = Scope.SUITE)
public class ChildQuerySearchTests extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
}
public class ChildQuerySearchIT extends ESIntegTestCase {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
@ -656,7 +651,7 @@ public class ChildQuerySearchTests extends ESIntegTestCase {
QueryBuilders.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
scriptFunction(new Script("doc['c_field1'].value")))
fieldValueFactorFunction("c_field1"))
.boostMode(CombineFunction.REPLACE)).scoreMode(ScoreMode.Total)).get();
assertThat(response.getHits().totalHits(), equalTo(3l));
@ -673,7 +668,7 @@ public class ChildQuerySearchTests extends ESIntegTestCase {
QueryBuilders.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
scriptFunction(new Script("doc['c_field1'].value")))
fieldValueFactorFunction("c_field1"))
.boostMode(CombineFunction.REPLACE)).scoreMode(ScoreMode.Max)).get();
assertThat(response.getHits().totalHits(), equalTo(3l));
@ -690,7 +685,7 @@ public class ChildQuerySearchTests extends ESIntegTestCase {
QueryBuilders.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
scriptFunction(new Script("doc['c_field1'].value")))
fieldValueFactorFunction("c_field1"))
.boostMode(CombineFunction.REPLACE)).scoreMode(ScoreMode.Avg)).get();
assertThat(response.getHits().totalHits(), equalTo(3l));
@ -707,7 +702,7 @@ public class ChildQuerySearchTests extends ESIntegTestCase {
QueryBuilders.hasParentQuery(
"parent",
QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"),
scriptFunction(new Script("doc['p_field2'].value")))
fieldValueFactorFunction("p_field2"))
.boostMode(CombineFunction.REPLACE)).score(true))
.addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get();

View File

@ -17,7 +17,7 @@
* under the License.
*/
package org.elasticsearch.messy.tests;
package org.elasticsearch.search.innerhits;
import org.apache.lucene.util.ArrayUtil;
import org.elasticsearch.Version;
@ -30,8 +30,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptEngine;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.groovy.GroovyPlugin;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.innerhits.InnerHitsBuilder;
@ -53,11 +54,11 @@ import static org.hamcrest.Matchers.*;
/**
*/
public class InnerHitsTests extends ESIntegTestCase {
public class InnerHitsIT extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
return Collections.singleton(MockScriptEngine.TestPlugin.class);
}
@Test
@ -151,7 +152,7 @@ public class InnerHitsTests extends ESIntegTestCase {
innerHit.highlightBuilder().field("comments.message");
innerHit.setExplain(true);
innerHit.addFieldDataField("comments.message");
innerHit.addScriptField("script", new Script("doc['comments.message'].value"));
innerHit.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()));
innerHit.setSize(1);
searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
@ -161,7 +162,7 @@ public class InnerHitsTests extends ESIntegTestCase {
.addHighlightedField("comments.message")
.setExplain(true)
.addFieldDataField("comments.message")
.addScriptField("script", new Script("doc['comments.message'].value"))
.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()))
.setSize(1)).request(),
client().prepareSearch("articles")
.setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")).innerHit(new QueryInnerHits(null, innerHit))).request()
@ -176,7 +177,7 @@ public class InnerHitsTests extends ESIntegTestCase {
assertThat(innerHits.getAt(0).getHighlightFields().get("comments.message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
assertThat(innerHits.getAt(0).explanation().toString(), containsString("weight(comments.message:fox in"));
assertThat(innerHits.getAt(0).getFields().get("comments.message").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("5"));
}
}
@ -329,7 +330,7 @@ public class InnerHitsTests extends ESIntegTestCase {
innerHit.highlightBuilder().field("message");
innerHit.setExplain(true);
innerHit.addFieldDataField("message");
innerHit.addScriptField("script", new Script("doc['message'].value"));
innerHit.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()));
innerHit.setSize(1);
searchRequests = new SearchRequest[] {
client().prepareSearch("articles")
@ -339,7 +340,7 @@ public class InnerHitsTests extends ESIntegTestCase {
.addHighlightedField("message")
.setExplain(true)
.addFieldDataField("message")
.addScriptField("script", new Script("doc['message'].value"))
.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()))
.setSize(1)
).request(),
@ -356,7 +357,7 @@ public class InnerHitsTests extends ESIntegTestCase {
assertThat(innerHits.getAt(0).getHighlightFields().get("message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
assertThat(innerHits.getAt(0).explanation().toString(), containsString("weight(message:fox"));
assertThat(innerHits.getAt(0).getFields().get("message").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("5"));
}
}

View File

@ -17,7 +17,7 @@
* under the License.
*/
package org.elasticsearch.messy.tests;
package org.elasticsearch.search.nested;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.join.ScoreMode;
@ -26,7 +26,6 @@ import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
@ -34,30 +33,18 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
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.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Assert;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
public class SimpleNestedTests extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
}
public class SimpleNestedIT extends ESIntegTestCase {
@Test
public void simpleNested() throws Exception {
@ -419,99 +406,6 @@ public class SimpleNestedTests extends ESIntegTestCase {
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("4"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("2"));
searchResponse = client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(
SortBuilders.scriptSort(new Script("_fields['nested1.field1'].value + 1"), "number").setNestedPath("nested1")
.order(SortOrder.DESC)).execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("6.0"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("5.0"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("3.0"));
searchResponse = client()
.prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(
SortBuilders.scriptSort(new Script("_fields['nested1.field1'].value + 1"), "number").setNestedPath("nested1")
.sortMode("sum").order(SortOrder.DESC)).execute().actionGet();
// B/c of sum it is actually +2
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("11.0"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("9.0"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("5.0"));
searchResponse = client()
.prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(
SortBuilders.scriptSort(new Script("_fields['nested1.field1'].value"), "number")
.setNestedFilter(rangeQuery("nested1.field1").from(1).to(3)).setNestedPath("nested1").sortMode("avg")
.order(SortOrder.DESC)).execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo(Double.toString(Double.MAX_VALUE)));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("3.0"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("1.5"));
searchResponse = client()
.prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(
SortBuilders.scriptSort(new Script("_fields['nested1.field1'].value"), "string").setNestedPath("nested1")
.order(SortOrder.DESC)).execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("5"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("4"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("2"));
searchResponse = client()
.prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(
SortBuilders.scriptSort(new Script("_fields['nested1.field1'].value"), "string").setNestedPath("nested1")
.order(SortOrder.ASC)).execute().actionGet();
assertHitCount(searchResponse, 3);
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("2"));
assertThat(searchResponse.getHits().hits()[0].sortValues()[0].toString(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[1].id(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[1].sortValues()[0].toString(), equalTo("3"));
assertThat(searchResponse.getHits().hits()[2].id(), equalTo("1"));
assertThat(searchResponse.getHits().hits()[2].sortValues()[0].toString(), equalTo("4"));
try {
client().prepareSearch("test")
.setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.addSort(
SortBuilders.scriptSort(new Script("_fields['nested1.field1'].value"), "string").setNestedPath("nested1")
.sortMode("sum").order(SortOrder.ASC)).execute().actionGet();
Assert.fail("SearchPhaseExecutionException should have been thrown");
} catch (SearchPhaseExecutionException e) {
assertThat(e.toString(), containsString("type [string] doesn't support mode [SUM]"));
}
}