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