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"));
}
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());
}
}
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));
}
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));
}
Aggregations