Search in sources :

Example 1 with RandomScoreFunctionBuilder

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

the class HighlighterSearchIT method testFiltersFunctionScoreQueryHighlight.

public void testFiltersFunctionScoreQueryHighlight() throws Exception {
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("text", "brown").field("enable", "yes").endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    FunctionScoreQueryBuilder.FilterFunctionBuilder filterBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("enable", "yes"), new RandomScoreFunctionBuilder());
    for (String type : UNIFIED_AND_NULL) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(new FunctionScoreQueryBuilder(QueryBuilders.prefixQuery("text", "bro"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { filterBuilder })).highlighter(new HighlightBuilder().field(new Field("text")).highlighterType(type)).get();
        assertHitCount(searchResponse, 1);
        HighlightField field = searchResponse.getHits().getAt(0).getHighlightFields().get("text");
        assertThat(field.getFragments().length, equalTo(1));
        assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with RandomScoreFunctionBuilder

use of org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder 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)

Example 3 with RandomScoreFunctionBuilder

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

the class PercolatorFieldMapperTests method testUnsupportedQueries.

public void testUnsupportedQueries() {
    RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D");
    RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now");
    PercolatorFieldMapper.verifyQuery(rangeQuery1);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(rangeQuery2));
    PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery1));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery2)));
    PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(rangeQuery2)));
    PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery2, new MatchAllQueryBuilder())));
    PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery2, new RandomScoreFunctionBuilder())));
    HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
    HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery)));
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder)

Example 4 with RandomScoreFunctionBuilder

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

the class RandomScoreFunctionIT method checkDistribution.

public void checkDistribution() throws Exception {
    int count = 10000;
    assertAcked(prepareCreate("test"));
    ensureGreen();
    for (int i = 0; i < count; i++) {
        index("test", "type", "" + i, jsonBuilder().startObject().endObject());
    }
    flush();
    refresh();
    int[] matrix = new int[count];
    for (int i = 0; i < count; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(functionScoreQuery(matchAllQuery(), new RandomScoreFunctionBuilder())).execute().actionGet();
        matrix[Integer.valueOf(searchResponse.getHits().getAt(0).getId())]++;
    }
    int filled = 0;
    int maxRepeat = 0;
    int sumRepeat = 0;
    for (int i = 0; i < matrix.length; i++) {
        int value = matrix[i];
        sumRepeat += value;
        maxRepeat = Math.max(maxRepeat, value);
        if (value > 0) {
            filled++;
        }
    }
    logger.info("max repeat: {}", maxRepeat);
    logger.info("avg repeat: {}", sumRepeat / (double) filled);
    logger.info("distribution: {}", filled / (double) count);
    int percentile50 = filled / 2;
    int percentile25 = (filled / 4);
    int percentile75 = percentile50 + percentile25;
    int sum = 0;
    for (int i = 0; i < matrix.length; i++) {
        if (matrix[i] == 0) {
            continue;
        }
        sum += i * matrix[i];
        if (percentile50 == 0) {
            logger.info("median: {}", i);
        } else if (percentile25 == 0) {
            logger.info("percentile_25: {}", i);
        } else if (percentile75 == 0) {
            logger.info("percentile_75: {}", i);
        }
        percentile50--;
        percentile25--;
        percentile75--;
    }
    logger.info("mean: {}", sum / (double) count);
}
Also used : RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

RandomScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)2 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 Entry (org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 BoostingQueryBuilder (org.elasticsearch.index.query.BoostingQueryBuilder)1 ConstantScoreQueryBuilder (org.elasticsearch.index.query.ConstantScoreQueryBuilder)1 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)1 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1 ScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder)1 ScriptScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder)1 SearchPlugin (org.elasticsearch.plugins.SearchPlugin)1 Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)1 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1