Search in sources :

Example 1 with FieldValueFactorFunction

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

the class FunctionScoreTests method testExceptionOnNegativeScores.

public void testExceptionOnNegativeScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
    // test that field_value_factor function throws an exception on negative scores
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.NONE;
    final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, -10, modifier, 1.0, new IndexNumericFieldDataStub());
    FunctionScoreQuery fsQuery1 = new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
    assertThat(exc.getMessage(), containsString("field value function must not produce negative scores"));
    assertThat(exc.getMessage(), not(containsString("consider using ln1p or ln2p instead of ln to avoid negative scores")));
    assertThat(exc.getMessage(), not(containsString("consider using log1p or log2p instead of log to avoid negative scores")));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 2 with FieldValueFactorFunction

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

the class FunctionScoreTests method testExceptionOnLogNegativeScores.

public void testExceptionOnLogNegativeScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
    // test that field_value_factor function using modifier log throws an exception on negative scores
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.LOG;
    final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, 0.5f, modifier, 1.0, new IndexNumericFieldDataStub());
    FunctionScoreQuery fsQuery1 = new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
    assertThat(exc.getMessage(), containsString("consider using log1p or log2p instead of log to avoid negative scores"));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 3 with FieldValueFactorFunction

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

the class FieldValueFactorFunctionBuilder method doToFunction.

@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
    MappedFieldType fieldType = context.getMapperService().fieldType(field);
    IndexNumericFieldData fieldData = null;
    if (fieldType == null) {
        if (missing == null) {
            throw new OpenSearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined.");
        }
    } else {
        fieldData = context.getForField(fieldType);
    }
    return new FieldValueFactorFunction(field, factor, modifier, missing, fieldData, getFunctionName());
}
Also used : FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) OpenSearchException(org.opensearch.OpenSearchException)

Example 4 with FieldValueFactorFunction

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

the class FunctionScoreTests method testExceptionOnLnNegativeScores.

public void testExceptionOnLnNegativeScores() {
    IndexSearcher localSearcher = new IndexSearcher(reader);
    TermQuery termQuery = new TermQuery(new Term(FIELD, "out"));
    // test that field_value_factor function using modifier ln throws an exception on negative scores
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.LN;
    final ScoreFunction fvfFunction = new FieldValueFactorFunction(FIELD, 0.5f, modifier, 1.0, new IndexNumericFieldDataStub());
    FunctionScoreQuery fsQuery1 = new FunctionScoreQuery(termQuery, fvfFunction, CombineFunction.REPLACE, null, Float.POSITIVE_INFINITY);
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> localSearcher.search(fsQuery1, 1));
    assertThat(exc.getMessage(), containsString("consider using ln1p or ln2p instead of ln to avoid negative scores"));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) Term(org.apache.lucene.index.Term) LeafScoreFunction(org.opensearch.common.lucene.search.function.LeafScoreFunction) FilterScoreFunction(org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) ScoreFunction(org.opensearch.common.lucene.search.function.ScoreFunction)

Example 5 with FieldValueFactorFunction

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

the class DiversifiedSamplerTests method testCase.

private void testCase(IndexSearcher indexSearcher, MappedFieldType genreFieldType, String executionHint, Consumer<InternalSampler> verify, int shardSize, int maxDocsPerValue) throws IOException {
    MappedFieldType idFieldType = new KeywordFieldMapper.KeywordFieldType("id");
    SortedNumericIndexFieldData fieldData = new SortedNumericIndexFieldData("price", IndexNumericFieldData.NumericType.DOUBLE);
    FunctionScoreQuery query = new FunctionScoreQuery(new MatchAllDocsQuery(), new FieldValueFactorFunction("price", 1, FieldValueFactorFunction.Modifier.RECIPROCAL, null, fieldData));
    DiversifiedAggregationBuilder builder = new DiversifiedAggregationBuilder("_name").field(genreFieldType.name()).executionHint(executionHint).maxDocsPerValue(maxDocsPerValue).shardSize(shardSize).subAggregation(new TermsAggregationBuilder("terms").field("id"));
    InternalSampler result = searchAndReduce(indexSearcher, query, builder, genreFieldType, idFieldType);
    verify.accept(result);
}
Also used : SortedNumericIndexFieldData(org.opensearch.index.fielddata.plain.SortedNumericIndexFieldData) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.opensearch.common.lucene.search.function.FieldValueFactorFunction) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

FieldValueFactorFunction (org.opensearch.common.lucene.search.function.FieldValueFactorFunction)5 FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)4 Term (org.apache.lucene.index.Term)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 TermQuery (org.apache.lucene.search.TermQuery)3 FilterScoreFunction (org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction)3 LeafScoreFunction (org.opensearch.common.lucene.search.function.LeafScoreFunction)3 RandomScoreFunction (org.opensearch.common.lucene.search.function.RandomScoreFunction)3 ScoreFunction (org.opensearch.common.lucene.search.function.ScoreFunction)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 OpenSearchException (org.opensearch.OpenSearchException)1 IndexNumericFieldData (org.opensearch.index.fielddata.IndexNumericFieldData)1 SortedNumericIndexFieldData (org.opensearch.index.fielddata.plain.SortedNumericIndexFieldData)1 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1