Skip to content

Commit

Permalink
inline builders and add error message to fail()
Browse files Browse the repository at this point in the history
  • Loading branch information
brwe committed Aug 29, 2014
1 parent 467a682 commit f006bdd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@ public void testProperErrorMessageWhenTwoFunctionsDefinedInQueryBody() throws IO
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/function-score-query-causing-NPE.json");
try {
queryParser.parse(query).query();
fail();
fail("FunctionScoreQueryParser should throw an exception here because two functions in body are not allowed.");
} catch (QueryParsingException e) {
assertThat(e.getDetailedMessage(), containsString("Use functions[{...},...] if you want to define several functions."));
}
Expand Down Expand Up @@ -2363,30 +2363,44 @@ public void testWeight1fStillProducesWeighFuction() throws IOException {
@Test
public void testProperErrorMessagesForMisplacedWeightsAndFunctions() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/function-score-query-with-weight-for-boost_factor.json");
String query = jsonBuilder().startObject().startObject("function_score")
.startArray("functions")
.startObject().field("weight", 2).field("boost_factor",2).endObject()
.endArray()
.endObject().endObject().string();
try {
queryParser.parse(query).query();
fail();
fail("Expect exception here because boost_factor must not have a weight");
} catch (QueryParsingException e) {
assertThat(e.getDetailedMessage(), containsString(BoostScoreFunction.BOOST_WEIGHT_ERROR_MESSAGE));
}
try {
functionScoreQuery().add(factorFunction(2.0f).setWeight(2.0f));
fail();
fail("Expect exception here because boost_factor must not have a weight");
} catch (ElasticsearchIllegalArgumentException e) {
assertThat(e.getDetailedMessage(), containsString(BoostScoreFunction.BOOST_WEIGHT_ERROR_MESSAGE));
}
query = copyToStringFromClasspath("/org/elasticsearch/index/query/function-score-query-with-functions-and-weight-in-body.json");
query = jsonBuilder().startObject().startObject("function_score")
.startArray("functions")
.startObject().field("boost_factor",2).endObject()
.endArray()
.field("weight", 2)
.endObject().endObject().string();
try {
queryParser.parse(query).query();
fail();
fail("Expect exception here because array of functions and one weight in body is not allowed.");
} catch (QueryParsingException e) {
assertThat(e.getDetailedMessage(), containsString("You can either define \"functions\":[...] or a single function, not both. Found \"functions\": [...] already, now encountering \"weight\"."));
}
query = copyToStringFromClasspath("/org/elasticsearch/index/query/function-score-query-with-weight-and-functions-in-body.json");
query = jsonBuilder().startObject().startObject("function_score")
.field("weight", 2)
.startArray("functions")
.startObject().field("boost_factor",2).endObject()
.endArray()
.endObject().endObject().string();
try {
queryParser.parse(query).query();
fail();
fail("Expect exception here because array of functions and one weight in body is not allowed.");
} catch (QueryParsingException e) {
assertThat(e.getDetailedMessage(), containsString("You can either define \"functions\":[...] or a single function, not both. Found \"weight\" already, now encountering \"functions\": [...]."));
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,30 @@ public void checkWeightOnlyCreatesBoostFunction() throws IOException {

index(INDEX, TYPE, "1", SIMPLE_DOC);
refresh();
String query = copyToStringFromClasspath("/org/elasticsearch/search/functionscore/function-score-query-with-weight-only.json");
String query =jsonBuilder().startObject()
.startObject("query")
.startObject("function_score")
.startArray("functions")
.startObject()
.field("weight",2)
.endObject()
.endArray()
.endObject()
.endObject()
.endObject().string();
SearchResponse response = client().search(
searchRequest().source(query)
).actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getAt(0).score(), equalTo(2.0f));
query = copyToStringFromClasspath("/org/elasticsearch/search/functionscore/function-score-query-with-weight-only-in-body.json");

query =jsonBuilder().startObject()
.startObject("query")
.startObject("function_score")
.field("weight",2)
.endObject()
.endObject()
.endObject().string();
response = client().search(
searchRequest().source(query)
).actionGet();
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit f006bdd

Please sign in to comment.