use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.
the class FunctionScoreTests method testExplanationAndScoreEqualsEvenIfNoFunctionMatches.
public void testExplanationAndScoreEqualsEvenIfNoFunctionMatches() throws IOException {
IndexSearcher localSearcher = newSearcher(reader);
CombineFunction combineFunction = randomFrom(new CombineFunction[] { CombineFunction.SUM, CombineFunction.AVG, CombineFunction.MIN, CombineFunction.MAX, CombineFunction.MULTIPLY, CombineFunction.REPLACE });
// check for document that has no matching function
FunctionScoreQuery query = new FunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), new FilterScoreFunction(new TermQuery(new Term("_uid", "2")), new WeightFactorFunction(10)), combineFunction, Float.NEGATIVE_INFINITY, Float.MAX_VALUE);
TopDocs searchResult = localSearcher.search(query, 1);
Explanation explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
// check for document that has a matching function
query = new FunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), new FilterScoreFunction(new TermQuery(new Term("_uid", "1")), new WeightFactorFunction(10)), combineFunction, Float.NEGATIVE_INFINITY, Float.MAX_VALUE);
searchResult = localSearcher.search(query, 1);
explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
}
use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.
the class FunctionScoreTests method testExplainFunctionScoreQueryWithName.
public void testExplainFunctionScoreQueryWithName() throws IOException {
Explanation functionExplanation = getFunctionScoreExplanation(searcher, RANDOM_SCORE_FUNCTION_NAMED);
checkFunctionScoreExplanation(functionExplanation, "random score function (seed: 0, field: test, _name: func1)");
assertThat(functionExplanation.getDetails()[0].getDetails().length, equalTo(0));
functionExplanation = getFunctionScoreExplanation(searcher, FIELD_VALUE_FACTOR_FUNCTION_NAMED);
checkFunctionScoreExplanation(functionExplanation, "field value function(_name: func1): ln(doc['test'].value?:1.0 * factor=1.0)");
assertThat(functionExplanation.getDetails()[0].getDetails().length, equalTo(0));
functionExplanation = getFunctionScoreExplanation(searcher, GAUSS_DECAY_FUNCTION_NAMED);
checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = exp(-0.5*pow(MAX[Math.max(Math.abs" + "(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)],2.0)/0.21714724095162594, _name: func1)\n"));
assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
functionExplanation = getFunctionScoreExplanation(searcher, EXP_DECAY_FUNCTION_NAMED);
checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = exp(- MAX[Math.max(Math.abs(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)] * 2.3025850929940455, _name: func1)\n"));
assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
functionExplanation = getFunctionScoreExplanation(searcher, LIN_DECAY_FUNCTION_NAMED);
checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = max(0.0, ((1.1111111111111112" + " - MAX[Math.max(Math.abs(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)])/1.1111111111111112, _name: func1)\n"));
assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
functionExplanation = getFunctionScoreExplanation(searcher, new WeightFactorFunction(4, RANDOM_SCORE_FUNCTION_NAMED));
checkFunctionScoreExplanation(functionExplanation, "product of:");
assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), endsWith("random score function (seed: 0, field: test, _name: func1)\n"));
assertThat(functionExplanation.getDetails()[0].getDetails()[1].toString(), equalTo("4.0 = weight\n"));
assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
assertThat(functionExplanation.getDetails()[0].getDetails()[1].getDetails().length, equalTo(0));
}
use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.
the class FunctionScoreQueryBuilderTests method testWeight1fStillProducesWeightFunction.
public void testWeight1fStillProducesWeightFunction() throws IOException {
String queryString = Strings.toString(jsonBuilder().startObject().startObject("function_score").startArray("functions").startObject().startObject("field_value_factor").field("field", INT_FIELD_NAME).endObject().field("weight", 1.0).endObject().endArray().endObject().endObject());
QueryBuilder query = parseQuery(queryString);
assertThat(query, instanceOf(FunctionScoreQueryBuilder.class));
FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) query;
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(FieldValueFactorFunctionBuilder.class));
FieldValueFactorFunctionBuilder fieldValueFactorFunctionBuilder = (FieldValueFactorFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
assertThat(fieldValueFactorFunctionBuilder.fieldName(), equalTo(INT_FIELD_NAME));
assertThat(fieldValueFactorFunctionBuilder.factor(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_FACTOR));
assertThat(fieldValueFactorFunctionBuilder.modifier(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_MODIFIER));
assertThat(fieldValueFactorFunctionBuilder.getWeight(), equalTo(1f));
assertThat(fieldValueFactorFunctionBuilder.missing(), nullValue());
Query luceneQuery = query.toQuery(createShardContext());
assertThat(luceneQuery, instanceOf(FunctionScoreQuery.class));
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) luceneQuery;
assertThat(functionScoreQuery.getFunctions().length, equalTo(1));
assertThat(functionScoreQuery.getFunctions()[0], instanceOf(WeightFactorFunction.class));
WeightFactorFunction weightFactorFunction = (WeightFactorFunction) functionScoreQuery.getFunctions()[0];
assertThat(weightFactorFunction.getWeight(), equalTo(1.0f));
assertThat(weightFactorFunction.getScoreFunction(), instanceOf(FieldValueFactorFunction.class));
}
use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.
the class FunctionScoreTests method testSimpleWeightedFunction.
public void testSimpleWeightedFunction() throws IOException, ExecutionException, InterruptedException {
int numFunctions = randomIntBetween(1, 3);
float[] weights = randomPositiveFloats(numFunctions);
double[] scores = randomPositiveDoubles(numFunctions);
ScoreFunctionStub[] scoreFunctionStubs = new ScoreFunctionStub[numFunctions];
for (int i = 0; i < numFunctions; i++) {
scoreFunctionStubs[i] = new ScoreFunctionStub(scores[i]);
}
WeightFactorFunction[] weightFunctionStubs = new WeightFactorFunction[numFunctions];
for (int i = 0; i < numFunctions; i++) {
weightFunctionStubs[i] = new WeightFactorFunction(weights[i], scoreFunctionStubs[i]);
}
FunctionScoreQuery functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.MULTIPLY, CombineFunction.REPLACE, weightFunctionStubs);
TopDocs topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
float scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
double score = 1;
for (int i = 0; i < weights.length; i++) {
score *= weights[i] * scores[i];
}
assertThat(scoreWithWeight / (float) score, is(1f));
float explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
assertThat(explainedScore / scoreWithWeight, is(1f));
functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.SUM, CombineFunction.REPLACE, weightFunctionStubs);
topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
double sum = 0;
for (int i = 0; i < weights.length; i++) {
sum += weights[i] * scores[i];
}
assertThat(scoreWithWeight / (float) sum, is(1f));
explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
assertThat(explainedScore / scoreWithWeight, is(1f));
functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.AVG, CombineFunction.REPLACE, weightFunctionStubs);
topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
double norm = 0;
sum = 0;
for (int i = 0; i < weights.length; i++) {
norm += weights[i];
sum += weights[i] * scores[i];
}
assertThat(scoreWithWeight / (float) (sum / norm), is(1f));
explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
assertThat(explainedScore / scoreWithWeight, is(1f));
functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.MIN, CombineFunction.REPLACE, weightFunctionStubs);
topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
double min = Double.POSITIVE_INFINITY;
for (int i = 0; i < weights.length; i++) {
min = Math.min(min, weights[i] * scores[i]);
}
assertThat(scoreWithWeight / (float) min, is(1f));
explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
assertThat(explainedScore / scoreWithWeight, is(1f));
functionScoreQueryWithWeights = getFiltersFunctionScoreQuery(FunctionScoreQuery.ScoreMode.MAX, CombineFunction.REPLACE, weightFunctionStubs);
topDocsWithWeights = searcher.search(functionScoreQueryWithWeights, 1);
scoreWithWeight = topDocsWithWeights.scoreDocs[0].score;
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < weights.length; i++) {
max = Math.max(max, weights[i] * scores[i]);
}
assertThat(scoreWithWeight / (float) max, is(1f));
explainedScore = getExplanation(searcher, functionScoreQueryWithWeights).getValue().floatValue();
assertThat(explainedScore / scoreWithWeight, is(1f));
}
use of org.opensearch.common.lucene.search.function.WeightFactorFunction in project OpenSearch by opensearch-project.
the class FunctionScoreTests method testWeightOnlyCreatesBoostFunction.
public void testWeightOnlyCreatesBoostFunction() throws IOException {
FunctionScoreQuery filtersFunctionScoreQueryWithWeights = new FunctionScoreQuery(new MatchAllDocsQuery(), new WeightFactorFunction(2), CombineFunction.MULTIPLY, 0.0f, 100);
TopDocs topDocsWithWeights = searcher.search(filtersFunctionScoreQueryWithWeights, 1);
float score = topDocsWithWeights.scoreDocs[0].score;
assertThat(score, equalTo(2.0f));
}
Aggregations