use of org.elasticsearch.common.lucene.search.function.ScoreFunction in project elasticsearch by elastic.
the class FunctionScoreTests method testFunctionScoreHashCodeAndEquals.
public void testFunctionScoreHashCodeAndEquals() {
Float minScore = randomBoolean() ? null : 1.0f;
CombineFunction combineFunction = randomFrom(CombineFunction.values());
float maxBoost = randomBoolean() ? Float.POSITIVE_INFINITY : randomFloat();
ScoreFunction function = randomBoolean() ? null : new DummyScoreFunction(combineFunction);
FunctionScoreQuery q = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, minScore, combineFunction, maxBoost);
FunctionScoreQuery q1 = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, minScore, combineFunction, maxBoost);
assertEquals(q, q);
assertEquals(q.hashCode(), q.hashCode());
assertEquals(q, q1);
assertEquals(q.hashCode(), q1.hashCode());
FunctionScoreQuery diffQuery = new FunctionScoreQuery(new TermQuery(new Term("foo", "baz")), function, minScore, combineFunction, maxBoost);
FunctionScoreQuery diffMinScore = new FunctionScoreQuery(q.getSubQuery(), function, minScore == null ? 1.0f : null, combineFunction, maxBoost);
ScoreFunction otherFunciton = function == null ? new DummyScoreFunction(combineFunction) : null;
FunctionScoreQuery diffFunction = new FunctionScoreQuery(q.getSubQuery(), otherFunciton, minScore, combineFunction, maxBoost);
FunctionScoreQuery diffMaxBoost = new FunctionScoreQuery(new TermQuery(new Term("foo", "bar")), function, minScore, combineFunction, maxBoost == 1.0f ? 0.9f : 1.0f);
FunctionScoreQuery[] queries = new FunctionScoreQuery[] { diffFunction, diffMinScore, diffQuery, q, diffMaxBoost };
final int numIters = randomIntBetween(20, 100);
for (int i = 0; i < numIters; i++) {
FunctionScoreQuery left = randomFrom(queries);
FunctionScoreQuery right = randomFrom(queries);
if (left == right) {
assertEquals(left, right);
assertEquals(left.hashCode(), right.hashCode());
} else {
assertNotEquals(left + " == " + right, left, right);
}
}
}
use of org.elasticsearch.common.lucene.search.function.ScoreFunction in project elasticsearch by elastic.
the class FunctionScoreQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
FilterFunction[] filterFunctions = new FilterFunction[filterFunctionBuilders.length];
int i = 0;
for (FilterFunctionBuilder filterFunctionBuilder : filterFunctionBuilders) {
Query filter = filterFunctionBuilder.getFilter().toQuery(context);
ScoreFunction scoreFunction = filterFunctionBuilder.getScoreFunction().toFunction(context);
filterFunctions[i++] = new FilterFunction(filter, scoreFunction);
}
Query query = this.query.toQuery(context);
if (query == null) {
query = new MatchAllDocsQuery();
}
// handle cases where only one score function and no filter was provided. In this case we create a FunctionScoreQuery.
if (filterFunctions.length == 0 || filterFunctions.length == 1 && (this.filterFunctionBuilders[0].getFilter().getName().equals(MatchAllQueryBuilder.NAME))) {
ScoreFunction function = filterFunctions.length == 0 ? null : filterFunctions[0].function;
CombineFunction combineFunction = this.boostMode;
if (combineFunction == null) {
if (function != null) {
combineFunction = function.getDefaultScoreCombiner();
} else {
combineFunction = DEFAULT_BOOST_MODE;
}
}
return new FunctionScoreQuery(query, function, minScore, combineFunction, maxBoost);
}
// in all other cases we create a FiltersFunctionScoreQuery
CombineFunction boostMode = this.boostMode == null ? DEFAULT_BOOST_MODE : this.boostMode;
return new FiltersFunctionScoreQuery(query, scoreMode, filterFunctions, maxBoost, minScore, boostMode);
}
use of org.elasticsearch.common.lucene.search.function.ScoreFunction in project elasticsearch by elastic.
the class FunctionScoreTests method testFilterFunctionScoreHashCodeAndEquals.
public void testFilterFunctionScoreHashCodeAndEquals() {
ScoreMode mode = randomFrom(ScoreMode.values());
CombineFunction combineFunction = randomFrom(CombineFunction.values());
ScoreFunction scoreFunction = new DummyScoreFunction(combineFunction);
Float minScore = randomBoolean() ? null : 1.0f;
Float maxBoost = randomBoolean() ? Float.POSITIVE_INFINITY : randomFloat();
FilterFunction function = new FilterFunction(new TermQuery(new Term("filter", "query")), scoreFunction);
FiltersFunctionScoreQuery q = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode, new FilterFunction[] { function }, maxBoost, minScore, combineFunction);
FiltersFunctionScoreQuery q1 = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode, new FilterFunction[] { function }, maxBoost, minScore, combineFunction);
assertEquals(q, q);
assertEquals(q.hashCode(), q.hashCode());
assertEquals(q, q1);
assertEquals(q.hashCode(), q1.hashCode());
FiltersFunctionScoreQuery diffCombineFunc = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode, new FilterFunction[] { function }, maxBoost, minScore, combineFunction == CombineFunction.AVG ? CombineFunction.MAX : CombineFunction.AVG);
FiltersFunctionScoreQuery diffQuery = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "baz")), mode, new FilterFunction[] { function }, maxBoost, minScore, combineFunction);
FiltersFunctionScoreQuery diffMode = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode == ScoreMode.AVG ? ScoreMode.FIRST : ScoreMode.AVG, new FilterFunction[] { function }, maxBoost, minScore, combineFunction);
FiltersFunctionScoreQuery diffMaxBoost = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode, new FilterFunction[] { function }, maxBoost == 1.0f ? 0.9f : 1.0f, minScore, combineFunction);
FiltersFunctionScoreQuery diffMinScore = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode, new FilterFunction[] { function }, maxBoost, minScore == null ? 0.9f : null, combineFunction);
FilterFunction otherFunc = new FilterFunction(new TermQuery(new Term("filter", "other_query")), scoreFunction);
FiltersFunctionScoreQuery diffFunc = new FiltersFunctionScoreQuery(new TermQuery(new Term("foo", "bar")), mode, randomBoolean() ? new FilterFunction[] { function, otherFunc } : new FilterFunction[] { otherFunc }, maxBoost, minScore, combineFunction);
FiltersFunctionScoreQuery[] queries = new FiltersFunctionScoreQuery[] { diffQuery, diffMaxBoost, diffMinScore, diffMode, diffFunc, q, diffCombineFunc };
final int numIters = randomIntBetween(20, 100);
for (int i = 0; i < numIters; i++) {
FiltersFunctionScoreQuery left = randomFrom(queries);
FiltersFunctionScoreQuery right = randomFrom(queries);
if (left == right) {
assertEquals(left, right);
assertEquals(left.hashCode(), right.hashCode());
} else {
assertNotEquals(left + " == " + right, left, right);
}
}
}
use of org.elasticsearch.common.lucene.search.function.ScoreFunction in project elasticsearch by elastic.
the class FunctionScoreTests method testWeightFactorNeedsScore.
public void testWeightFactorNeedsScore() {
for (boolean needsScore : new boolean[] { true, false }) {
WeightFactorFunction function = new WeightFactorFunction(10.0f, new ScoreFunction(CombineFunction.REPLACE) {
@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
return null;
}
@Override
public boolean needsScores() {
return needsScore;
}
@Override
protected boolean doEquals(ScoreFunction other) {
return false;
}
@Override
protected int doHashCode() {
return 0;
}
});
assertEquals(needsScore, function.needsScores());
}
}
Aggregations