use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.
the class SearchModule method registerScoreFunctions.
private void registerScoreFunctions(List<SearchPlugin> plugins) {
// ScriptScoreFunctionBuilder has it own named writable because of a new script_score query
namedWriteables.add(new NamedWriteableRegistry.Entry(ScriptScoreFunctionBuilder.class, ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new));
registerScoreFunction(new ScoreFunctionSpec<>(ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new, ScriptScoreFunctionBuilder::fromXContent));
registerScoreFunction(new ScoreFunctionSpec<>(GaussDecayFunctionBuilder.NAME, GaussDecayFunctionBuilder::new, GaussDecayFunctionBuilder.PARSER));
registerScoreFunction(new ScoreFunctionSpec<>(LinearDecayFunctionBuilder.NAME, LinearDecayFunctionBuilder::new, LinearDecayFunctionBuilder.PARSER));
registerScoreFunction(new ScoreFunctionSpec<>(ExponentialDecayFunctionBuilder.NAME, ExponentialDecayFunctionBuilder::new, ExponentialDecayFunctionBuilder.PARSER));
registerScoreFunction(new ScoreFunctionSpec<>(RandomScoreFunctionBuilder.NAME, RandomScoreFunctionBuilder::new, RandomScoreFunctionBuilder::fromXContent));
registerScoreFunction(new ScoreFunctionSpec<>(FieldValueFactorFunctionBuilder.NAME, FieldValueFactorFunctionBuilder::new, FieldValueFactorFunctionBuilder::fromXContent));
// weight doesn't have its own parser, so every function supports it out of the box.
// Can be a single function too when not associated to any other function, which is why it needs to be registered manually here.
namedWriteables.add(new NamedWriteableRegistry.Entry(ScoreFunctionBuilder.class, WeightBuilder.NAME, WeightBuilder::new));
registerFromPlugin(plugins, SearchPlugin::getScoreFunctions, this::registerScoreFunction);
}
use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.
the class HighlighterSearchIT method testFiltersFunctionScoreQueryHighlight.
public void testFiltersFunctionScoreQueryHighlight() throws Exception {
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("text", "brown").field("enable", "yes").endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
FunctionScoreQueryBuilder.FilterFunctionBuilder filterBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("enable", "yes"), new RandomScoreFunctionBuilder());
SearchResponse searchResponse = client().prepareSearch().setQuery(new FunctionScoreQueryBuilder(QueryBuilders.prefixQuery("text", "bro"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { filterBuilder })).highlighter(new HighlightBuilder().field(new Field("text"))).get();
assertHitCount(searchResponse, 1);
HighlightField field = searchResponse.getHits().getAt(0).getHighlightFields().get("text");
assertThat(field.getFragments().length, equalTo(1));
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
}
use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.
the class PercolatorFieldMapperTests method testUnsupportedQueries.
public void testUnsupportedQueries() {
RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D");
RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now");
PercolatorFieldMapper.verifyQuery(rangeQuery1);
PercolatorFieldMapper.verifyQuery(rangeQuery2);
HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new DisMaxQueryBuilder().add(hasChildQuery)));
PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(hasChildQuery)));
PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(hasChildQuery, new MatchAllQueryBuilder())));
PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(hasChildQuery, new RandomScoreFunctionBuilder())));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false);
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery)));
}
use of org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder in project OpenSearch by opensearch-project.
the class RandomScoreFunctionIT method checkDistribution.
public void checkDistribution() throws Exception {
int count = 10000;
assertAcked(prepareCreate("test"));
ensureGreen();
for (int i = 0; i < count; i++) {
index("test", "type", "" + i, jsonBuilder().startObject().endObject());
}
flush();
refresh();
int[] matrix = new int[count];
for (int i = 0; i < count; i++) {
SearchResponse searchResponse = client().prepareSearch().setQuery(functionScoreQuery(matchAllQuery(), new RandomScoreFunctionBuilder())).get();
matrix[Integer.valueOf(searchResponse.getHits().getAt(0).getId())]++;
}
int filled = 0;
int maxRepeat = 0;
int sumRepeat = 0;
for (int i = 0; i < matrix.length; i++) {
int value = matrix[i];
sumRepeat += value;
maxRepeat = Math.max(maxRepeat, value);
if (value > 0) {
filled++;
}
}
logger.info("max repeat: {}", maxRepeat);
logger.info("avg repeat: {}", sumRepeat / (double) filled);
logger.info("distribution: {}", filled / (double) count);
int percentile50 = filled / 2;
int percentile25 = (filled / 4);
int percentile75 = percentile50 + percentile25;
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
if (matrix[i] == 0) {
continue;
}
sum += i * matrix[i];
if (percentile50 == 0) {
logger.info("median: {}", i);
} else if (percentile25 == 0) {
logger.info("percentile_25: {}", i);
} else if (percentile75 == 0) {
logger.info("percentile_75: {}", i);
}
percentile50--;
percentile25--;
percentile75--;
}
logger.info("mean: {}", sum / (double) count);
}
Aggregations