private void registerScoreFunctions(List<SearchPlugin> plugins) { registerScoreFunction(new ScoreFunctionSpec<>(ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new, ScriptScoreFunctionBuilder::fromXContent)); registerScoreFunction( new ScoreFunctionSpec<>(GaussDecayFunctionBuilder.NAME, GaussDecayFunctionBuilder::new, GaussDecayFunctionBuilder.PARSER)); registerScoreFunction(new ScoreFunctionSpec<>(LinearDecayFunctionBuilder.NAME, LinearDecayFunctionBuilder::new, LinearDecayFunctionBuilder.PARSER)); registerScoreFunction(new ScoreFunctionSpec<>(ExponentialDecayFunctionBuilder.NAME, ExponentialDecayFunctionBuilder::new, ExponentialDecayFunctionBuilder.PARSER)); registerScoreFunction(new ScoreFunctionSpec<>(RandomScoreFunctionBuilder.NAME, RandomScoreFunctionBuilder::new, RandomScoreFunctionBuilder::fromXContent)); registerScoreFunction(new ScoreFunctionSpec<>(FieldValueFactorFunctionBuilder.NAME, FieldValueFactorFunctionBuilder::new, FieldValueFactorFunctionBuilder::fromXContent)); //weight doesn't have its own parser, so every function supports it out of the box. //Can be a single function too when not associated to any other function, which is why it needs to be registered manually here. namedWriteables.add(new NamedWriteableRegistry.Entry(ScoreFunctionBuilder.class, WeightBuilder.NAME, WeightBuilder::new)); registerFromPlugin(plugins, SearchPlugin::getScoreFunctions, this::registerScoreFunction); }
public void testFailedMultiSearchWithWrongQueryWithFunctionScore() throws Exception { prepareData(); logger.info("Start Testing failed multi search with a wrong query"); MultiSearchResponse response = client().prepareMultiSearch() // Add custom score query with bogus script .add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1), new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, "bar", "foo", Collections.emptyMap()))))) .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))) .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())) .execute().actionGet(); assertThat(response.getResponses().length, equalTo(3)); assertThat(response.getResponses()[0].getFailureMessage(), notNullValue()); assertThat(response.getResponses()[1].getFailureMessage(), nullValue()); assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1)); assertThat(response.getResponses()[2].getFailureMessage(), nullValue()); assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10)); logger.info("Done Testing failed search"); }
/** * Processes the scripts and score function * * @param queryBuilder the query builder * * @return the function score query builder */ private FunctionScoreQueryBuilder getFunctionScoreQueryBuilder(QueryBuilder queryBuilder, String bdefActiveIndex) { // Script for tag search score multiplier. If bdef set to tag search score multiplier else set to a default value. String inlineScript = "_score * (doc['_index'].value == '" + bdefActiveIndex + "' ? doc['" + BDEF_TAGS_SEARCH_SCORE_MULTIPLIER + "']: 1)"; // Set the lang to groovy Script script = new Script(ScriptType.INLINE, "groovy", inlineScript, Collections.emptyMap()); // Set the script ScriptScoreFunctionBuilder scoreFunction = ScoreFunctionBuilders.scriptFunction(script); // Create function score query builder return new FunctionScoreQueryBuilder(queryBuilder, scoreFunction); }