Search in sources :

Example 1 with RandomScoreFunctionBuilder

use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.

the class SearchModule method registerScoreFunctions.

private void registerScoreFunctions(List<SearchPlugin> plugins) {
    // ScriptScoreFunctionBuilder has it own named writable because of a new script_score query
    namedWriteables.add(new NamedWriteableRegistry.Entry(ScriptScoreFunctionBuilder.class, ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new));
    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.opensearch.common.io.stream.NamedWriteableRegistry) SearchPlugin(org.opensearch.plugins.SearchPlugin) ScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScoreFunctionBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) Entry(org.opensearch.common.io.stream.NamedWriteableRegistry.Entry) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder)

Example 2 with RandomScoreFunctionBuilder

use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.

the class HighlighterSearchIT method testFiltersFunctionScoreQueryHighlight.

public void testFiltersFunctionScoreQueryHighlight() throws Exception {
    client().prepareIndex("test").setId("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());
    SearchResponse searchResponse = client().prepareSearch().setQuery(new FunctionScoreQueryBuilder(QueryBuilders.prefixQuery("text", "bro"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { filterBuilder })).highlighter(new HighlightBuilder().field(new Field("text"))).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.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with RandomScoreFunctionBuilder

use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.

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);
    PercolatorFieldMapper.verifyQuery(rangeQuery2);
    HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new DisMaxQueryBuilder().add(hasChildQuery)));
    PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(hasChildQuery)));
    PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(hasChildQuery, new MatchAllQueryBuilder())));
    PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(hasChildQuery, new RandomScoreFunctionBuilder())));
    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.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder)

Example 4 with RandomScoreFunctionBuilder

use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.

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())).get();
        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.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

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