Fix transient testScore failure by making DF consistent for query.

This commit is contained in:
Ryan Ernst 2014-07-15 11:17:30 -07:00
parent 86dfad24fe
commit e764c5f08a
1 changed files with 16 additions and 17 deletions

View File

@ -21,10 +21,7 @@ package org.elasticsearch.script.expression;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.action.search.*;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
@ -49,7 +46,7 @@ import static org.hamcrest.Matchers.greaterThan;
public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
private SearchResponse runScript(String script, Object... params) {
private SearchRequestBuilder buildRequest(String script, Object... params) {
ensureGreen("test");
Map<String, Object> paramsMap = new HashMap<>();
@ -62,14 +59,14 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
req.setQuery(QueryBuilders.matchAllQuery())
.addSort(SortBuilders.fieldSort("_uid")
.order(SortOrder.ASC)).addScriptField("foo", "expression", script, paramsMap);
return req.get();
return req;
}
public void testBasic() throws Exception {
createIndex("test");
ensureGreen("test");
client().prepareIndex("test", "doc", "1").setSource("foo", 4).setRefresh(true).get();
SearchResponse rsp = runScript("doc['foo'].value + 1");
SearchResponse rsp = buildRequest("doc['foo'].value + 1").get();
assertEquals(1, rsp.getHits().getTotalHits());
assertEquals(5.0, rsp.getHits().getAt(0).field("foo").getValue());
}
@ -81,9 +78,10 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"),
client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"),
client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye"));
ScoreFunctionBuilder score = ScoreFunctionBuilders.scriptFunction("1 / _score", "expression", Collections.EMPTY_MAP);
ScoreFunctionBuilder score = ScoreFunctionBuilders.scriptFunction("1 / _score", "expression");
SearchRequestBuilder req = new SearchRequestBuilder(client()).setIndices("test");
req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode("replace"));
req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // make sure DF is consistent
SearchResponse rsp = req.get();
SearchHits hits = rsp.getHits();
assertEquals(3, hits.getTotalHits());
@ -98,7 +96,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
indexRandom(true,
client().prepareIndex("test", "doc", "1").setSource("x", 4),
client().prepareIndex("test", "doc", "2").setSource("y", 2));
SearchResponse rsp = runScript("doc['x'].value + 1");
SearchResponse rsp = buildRequest("doc['x'].value + 1").get();
ElasticsearchAssertions.assertSearchResponse(rsp);
SearchHits hits = rsp.getHits();
assertEquals(2, rsp.getHits().getTotalHits());
@ -111,7 +109,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
ensureGreen("test");
client().prepareIndex("test", "doc", "1").setSource("x", 4).setRefresh(true).get();
try {
runScript("doc['bogus'].value");
buildRequest("doc['bogus'].value").get();
fail("Expected missing field to cause failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",
@ -129,7 +127,8 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
client().prepareIndex("test", "doc", "2").setSource("x", 3),
client().prepareIndex("test", "doc", "3").setSource("x", 5));
// a = int, b = double, c = long
SearchResponse rsp = runScript("doc['x'].value * a + b + ((c + doc['x'].value) > 5000000009 ? 1 : 0)", "a", 2, "b", 3.5, "c", 5000000000L);
String script = "doc['x'].value * a + b + ((c + doc['x'].value) > 5000000009 ? 1 : 0)";
SearchResponse rsp = buildRequest(script, "a", 2, "b", 3.5, "c", 5000000000L).get();
SearchHits hits = rsp.getHits();
assertEquals(3, hits.getTotalHits());
assertEquals(24.5, hits.getAt(0).field("foo").getValue());
@ -140,7 +139,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
public void testCompileFailure() {
client().prepareIndex("test", "doc", "1").setSource("x", 1).setRefresh(true).get();
try {
runScript("garbage%@#%@");
buildRequest("garbage%@#%@").get();
fail("Expected expression compilation failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",
@ -153,7 +152,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
public void testNonNumericParam() {
client().prepareIndex("test", "doc", "1").setSource("x", 1).setRefresh(true).get();
try {
runScript("a", "a", "astring");
buildRequest("a", "a", "astring").get();
fail("Expected string parameter to cause failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",
@ -166,7 +165,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
public void testNonNumericField() {
client().prepareIndex("test", "doc", "1").setSource("text", "this is not a number").setRefresh(true).get();
try {
runScript("doc['text'].value");
buildRequest("doc['text'].value").get();
fail("Expected text field to cause execution failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",
@ -179,7 +178,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
public void testInvalidGlobalVariable() {
client().prepareIndex("test", "doc", "1").setSource("foo", 5).setRefresh(true).get();
try {
runScript("bogus");
buildRequest("bogus").get();
fail("Expected bogus variable to cause execution failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",
@ -192,7 +191,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
public void testDocWithoutField() {
client().prepareIndex("test", "doc", "1").setSource("foo", 5).setRefresh(true).get();
try {
runScript("doc");
buildRequest("doc").get();
fail("Expected doc variable without field to cause execution failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",
@ -205,7 +204,7 @@ public class ExpressionScriptTests extends ElasticsearchIntegrationTest {
public void testInvalidFieldMember() {
client().prepareIndex("test", "doc", "1").setSource("foo", 5).setRefresh(true).get();
try {
runScript("doc['foo'].bogus");
buildRequest("doc['foo'].bogus").get();
fail("Expected bogus field member to cause execution failure");
} catch (SearchPhaseExecutionException e) {
assertThat(ExceptionsHelper.detailedMessage(e) + "should have contained ExpressionScriptCompilationException",