Search in sources :

Example 1 with ScriptScoreFunctionBuilder

use of org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder in project elasticsearch by elastic.

the class TransportTwoNodesSearchIT method testFailedMultiSearchWithWrongQueryWithFunctionScore.

public void testFailedMultiSearchWithWrongQueryWithFunctionScore() throws Exception {
    prepareData();
    logger.info("Start Testing failed multi search with a wrong query");
    MultiSearchResponse response = client().prepareMultiSearch().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");
}
Also used : MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) Script(org.elasticsearch.script.Script) ScriptScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder)

Example 2 with ScriptScoreFunctionBuilder

use of org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder in project herd by FINRAOS.

the class IndexSearchDaoImpl method getFunctionScoreQueryBuilder.

/**
 * 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);
}
Also used : Script(org.elasticsearch.script.Script) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) ScriptScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder)

Example 3 with ScriptScoreFunctionBuilder

use of org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder in project elasticsearch by elastic.

the class SearchModule method registerScoreFunctions.

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);
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) SearchPlugin(org.elasticsearch.plugins.SearchPlugin) ScriptScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) ScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder) Entry(org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry)

Aggregations

ScriptScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder)3 Script (org.elasticsearch.script.Script)2 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)1 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 Entry (org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry)1 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)1 RandomScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder)1 ScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder)1 SearchPlugin (org.elasticsearch.plugins.SearchPlugin)1