Search in sources :

Example 1 with RandomScoreFunction

use of org.opensearch.common.lucene.search.function.RandomScoreFunction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testFunctionScoreQuery.

public void testFunctionScoreQuery() {
    TermQuery termQuery = new TermQuery(new Term("_field", "_value"));
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(termQuery, new RandomScoreFunction(0, 0, null));
    Result result = analyze(functionScoreQuery, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("_field", "_value"));
    functionScoreQuery = new FunctionScoreQuery(termQuery, new RandomScoreFunction(0, 0, null), CombineFunction.MULTIPLY, 1f, 10f);
    result = analyze(functionScoreQuery, Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("_field", "_value"));
}
Also used : BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) Term(org.apache.lucene.index.Term) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 2 with RandomScoreFunction

use of org.opensearch.common.lucene.search.function.RandomScoreFunction in project OpenSearch by opensearch-project.

the class RandomScoreFunctionBuilder method doToFunction.

@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
    final int salt = (context.index().getName().hashCode() << 10) | context.getShardId();
    if (seed == null) {
        // DocID-based random score generation
        return new RandomScoreFunction(hash(context.nowInMillis()), salt, null, getFunctionName());
    } else {
        final MappedFieldType fieldType;
        if (field != null) {
            fieldType = context.getMapperService().fieldType(field);
        } else {
            deprecationLogger.deprecate("seed_requires_field", "OpenSearch requires that a [field] parameter is provided when a [seed] is set");
            fieldType = context.getMapperService().fieldType(IdFieldMapper.NAME);
        }
        if (fieldType == null) {
            if (context.getMapperService().documentMapper() == null) {
                // no mappings: the index is empty anyway
                return new RandomScoreFunction(hash(context.nowInMillis()), salt, null, getFunctionName());
            }
            throw new IllegalArgumentException("Field [" + field + "] is not mapped on [" + context.index() + "] and cannot be used as a source of random numbers.");
        }
        int seed;
        if (this.seed != null) {
            seed = this.seed;
        } else {
            seed = hash(context.nowInMillis());
        }
        return new RandomScoreFunction(seed, salt, context.getForField(fieldType), getFunctionName());
    }
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction)

Example 3 with RandomScoreFunction

use of org.opensearch.common.lucene.search.function.RandomScoreFunction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testFunctionScoreQuery_withMatchAll.

public void testFunctionScoreQuery_withMatchAll() {
    MatchAllDocsQuery innerQuery = new MatchAllDocsQuery();
    FunctionScoreQuery functionScoreQuery1 = new FunctionScoreQuery(innerQuery, new RandomScoreFunction(0, 0, null));
    Result result = analyze(functionScoreQuery1, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(0));
    assertThat(result.matchAllDocs, is(true));
    assertThat(result.extractions.isEmpty(), is(true));
    FunctionScoreQuery functionScoreQuery2 = new FunctionScoreQuery(innerQuery, new RandomScoreFunction(0, 0, null), CombineFunction.MULTIPLY, 1f, 10f);
    result = analyze(functionScoreQuery2, Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.minimumShouldMatch, equalTo(0));
    assertThat(result.matchAllDocs, is(true));
    assertThat(result.extractions.isEmpty(), is(true));
}
Also used : FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 4 with RandomScoreFunction

use of org.opensearch.common.lucene.search.function.RandomScoreFunction in project OpenSearch by opensearch-project.

the class PercolatorHighlightSubFetchPhaseTests method testLocatePercolatorQuery.

public void testLocatePercolatorQuery() {
    PercolateQuery percolateQuery = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(new MatchAllDocsQuery()).size(), equalTo(0));
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(0));
    bq.add(percolateQuery, BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).get(0), sameInstance(percolateQuery));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(new MatchAllDocsQuery());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery).size(), equalTo(0));
    constantScoreQuery = new ConstantScoreQuery(percolateQuery);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery).get(0), sameInstance(percolateQuery));
    BoostQuery boostQuery = new BoostQuery(new MatchAllDocsQuery(), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery).size(), equalTo(0));
    boostQuery = new BoostQuery(percolateQuery, 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery).get(0), sameInstance(percolateQuery));
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(new MatchAllDocsQuery(), new RandomScoreFunction(0, 0, null));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery).size(), equalTo(0));
    functionScoreQuery = new FunctionScoreQuery(percolateQuery, new RandomScoreFunction(0, 0, null));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery).get(0), sameInstance(percolateQuery));
    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(Collections.singleton(new MatchAllDocsQuery()), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).size(), equalTo(0));
    disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(percolateQuery, new MatchAllDocsQuery()), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).get(0), sameInstance(percolateQuery));
    PercolateQuery percolateQuery2 = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery());
    bq = new BooleanQuery.Builder();
    bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(0));
    bq.add(percolateQuery, BooleanClause.Occur.FILTER);
    bq.add(percolateQuery2, BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(2));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()), containsInAnyOrder(sameInstance(percolateQuery), sameInstance(percolateQuery2)));
    assertNotNull(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null).size(), equalTo(0));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) BytesArray(org.opensearch.common.bytes.BytesArray) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction)

Aggregations

RandomScoreFunction (org.opensearch.common.lucene.search.function.RandomScoreFunction)4 FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 Result (org.opensearch.percolator.QueryAnalyzer.Result)2 QueryAnalyzer.selectBestResult (org.opensearch.percolator.QueryAnalyzer.selectBestResult)2 Term (org.apache.lucene.index.Term)1 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TermQuery (org.apache.lucene.search.TermQuery)1 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)1 BytesArray (org.opensearch.common.bytes.BytesArray)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1