Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception if function in function score query is null #6784

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.query.functionscore;

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.BaseQueryBuilder;
Expand Down Expand Up @@ -66,19 +67,28 @@ public FunctionScoreQueryBuilder() {
}

public FunctionScoreQueryBuilder(ScoreFunctionBuilder scoreFunctionBuilder) {
if (scoreFunctionBuilder == null) {
throw new ElasticsearchIllegalArgumentException("function_score: function must not be null");
}
queryBuilder = null;
filterBuilder = null;
this.filters.add(null);
this.scoreFunctions.add(scoreFunctionBuilder);
}

public FunctionScoreQueryBuilder add(FilterBuilder filter, ScoreFunctionBuilder scoreFunctionBuilder) {
if (scoreFunctionBuilder == null) {
throw new ElasticsearchIllegalArgumentException("function_score: function must not be null");
}
this.filters.add(filter);
this.scoreFunctions.add(scoreFunctionBuilder);
return this;
}

public FunctionScoreQueryBuilder add(ScoreFunctionBuilder scoreFunctionBuilder) {
if (scoreFunctionBuilder == null) {
throw new ElasticsearchIllegalArgumentException("function_score: function must not be null");
}
this.filters.add(null);
this.scoreFunctions.add(scoreFunctionBuilder);
return this;
Expand Down
Expand Up @@ -195,6 +195,9 @@ private String parseFiltersAndFunctions(QueryParseContext parseContext, XContent
if (filter == null) {
filter = Queries.MATCH_ALL_FILTER;
}
if (scoreFunction == null) {
throw new ElasticsearchParseException("function_score: One entry in functions list is missing a function.");
}
filterFunctions.add(new FiltersFunctionScoreQuery.FilterFunction(filter, scoreFunction));

}
Expand Down
Expand Up @@ -19,16 +19,20 @@

package org.elasticsearch.search.functionscore;

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchIllegalStateException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.MatchAllFilterBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.DecayFunctionBuilder;
Expand All @@ -38,6 +42,7 @@
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -843,4 +848,94 @@ public void errorMessageForFaultyFunctionScoreBody() throws Exception {
}
}

// issue https://github.com/elasticsearch/elasticsearch/issues/6292
@Test
public void testMissingFunctionThrowsElasticsearchParseException() throws IOException {

// example from issue https://github.com/elasticsearch/elasticsearch/issues/6292
String doc = "{\n" +
" \"text\": \"baseball bats\"\n" +
"}\n";

String query = "{\n" +
" \"function_score\": {\n" +
" \"score_mode\": \"sum\",\n" +
" \"boost_mode\": \"replace\",\n" +
" \"functions\": [\n" +
" {\n" +
" \"filter\": {\n" +
" \"term\": {\n" +
" \"text\": \"baseball\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n";

client().prepareIndex("t", "test").setSource(doc).get();
refresh();
ensureYellow("t");
try {
client().search(
searchRequest().source(
searchSource().query(query))).actionGet();
fail("Should fail with SearchPhaseExecutionException");
} catch (SearchPhaseExecutionException failure) {
assertTrue(failure.getMessage().contains("SearchParseException"));
assertFalse(failure.getMessage().contains("NullPointerException"));
}

query = "{\n" +
" \"function_score\": {\n" +
" \"score_mode\": \"sum\",\n" +
" \"boost_mode\": \"replace\",\n" +
" \"functions\": [\n" +
" {\n" +
" \"filter\": {\n" +
" \"term\": {\n" +
" \"text\": \"baseball\"\n" +
" }\n" +
" },\n" +
" \"boost_factor\": 2\n" +
" },\n" +
" {\n" +
" \"filter\": {\n" +
" \"term\": {\n" +
" \"text\": \"baseball\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";

try {
client().search(
searchRequest().source(
searchSource().query(query))).actionGet();
fail("Should fail with SearchPhaseExecutionException");
} catch (SearchPhaseExecutionException failure) {
assertTrue(failure.getMessage().contains("SearchParseException"));
assertFalse(failure.getMessage().contains("NullPointerException"));
assertTrue(failure.getMessage().contains("One entry in functions list is missing a function"));
}

// next test java client
try {
client().prepareSearch("t").setQuery(QueryBuilders.functionScoreQuery(FilterBuilders.matchAllFilter(), null)).get();
} catch (ElasticsearchIllegalArgumentException failure) {
assertTrue(failure.getMessage().contains("function must not be null"));
}
try {
client().prepareSearch("t").setQuery(QueryBuilders.functionScoreQuery().add(FilterBuilders.matchAllFilter(), null)).get();
} catch (ElasticsearchIllegalArgumentException failure) {
assertTrue(failure.getMessage().contains("function must not be null"));
}
try {
client().prepareSearch("t").setQuery(QueryBuilders.functionScoreQuery().add(null)).get();
} catch (ElasticsearchIllegalArgumentException failure) {
assertTrue(failure.getMessage().contains("function must not be null"));
}
}
}