org.elasticsearch.script.ScoreScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.script;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Scorable;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.Source;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* A script used for adjusting the score on a per document basis.
*/
public abstract class ScoreScript extends DocBasedScript {
/** A helper to take in an explanation from a script and turn it into an {@link org.apache.lucene.search.Explanation} */
public static class ExplanationHolder {
private String description;
/**
* Explain the current score.
*
* @param description A textual description of how the score was calculated
*/
public void set(String description) {
this.description = description;
}
public Explanation get(double score, Explanation subQueryExplanation) {
if (description == null) {
return null;
}
if (subQueryExplanation != null) {
return Explanation.match(score, description, subQueryExplanation);
}
return Explanation.match(score, description);
}
}
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DynamicMap.class);
@SuppressWarnings("unchecked")
private static final Map> PARAMS_FUNCTIONS = Map.of("doc", value -> {
deprecationLogger.warn(
DeprecationCategory.SCRIPTING,
"score-script_doc",
"Accessing variable [doc] via [params.doc] from within an score-script " + "is deprecated in favor of directly accessing [doc]."
);
return value;
}, "_doc", value -> {
deprecationLogger.warn(
DeprecationCategory.SCRIPTING,
"score-script__doc",
"Accessing variable [doc] via [params._doc] from within an score-script "
+ "is deprecated in favor of directly accessing [doc]."
);
return value;
}, "_source", value -> ((Supplier