Skip to content

Commit

Permalink
Adjust random_score default field to _seq_no field
Browse files Browse the repository at this point in the history
  • Loading branch information
benwtrent committed Dec 13, 2024
1 parent 9837e78 commit 56c2b8e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.lucene.search.function.RandomScoreFunction;
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
Expand All @@ -30,7 +28,6 @@
* A function that computes a random score for the matched documents
*/
public class RandomScoreFunctionBuilder extends ScoreFunctionBuilder<RandomScoreFunctionBuilder> {
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RandomScoreFunctionBuilder.class);

public static final String NAME = "random_score";
private String field;
Expand Down Expand Up @@ -140,17 +137,7 @@ protected ScoreFunction doToFunction(SearchExecutionContext context) {
// DocID-based random score generation
return new RandomScoreFunction(hash(context.nowInMillis()), salt, null);
} else {
String fieldName;
if (field == null) {
deprecationLogger.warn(
DeprecationCategory.QUERIES,
"seed_requires_field",
"As of version 7.0 Elasticsearch will require that a [field] parameter is provided when a [seed] is set"
);
fieldName = IdFieldMapper.NAME;
} else {
fieldName = field;
}
final String fieldName = Objects.requireNonNullElse(field, SeqNoFieldMapper.NAME);
if (context.isFieldMapped(fieldName) == false) {
if (context.hasMappings() == false) {
// no mappings: the index is empty anyway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public void testRandomScoreFunctionWithSeedNoField() throws Exception {
Mockito.when(context.getFieldType(IdFieldMapper.NAME)).thenReturn(new KeywordFieldMapper.KeywordFieldType(IdFieldMapper.NAME));
Mockito.when(context.isFieldMapped(IdFieldMapper.NAME)).thenReturn(true);
builder.toFunction(context);
assertWarnings("As of version 7.0 Elasticsearch will require that a [field] parameter is provided when a [seed] is set");
}

public void testRandomScoreFunctionWithSeed() throws Exception {
Expand Down

0 comments on commit 56c2b8e

Please sign in to comment.