Skip to content

Commit a5c57ba

Browse files
authored
Adjust random_score default field to _seq_no field (#118671)
In an effort to improve performance and continue to provide unique seeded scores for documents in the same index, we are switching from _id to _seq_no. Requiring a field that is "unique" for a field and to help with random scores is burdensome for the user. So, we should default to a unique field (per index) when the user provides a seed. Using `_seq_no` should be better as: - We don't have to grab stored fields values - Bytes used are generally smaller Additionally this removes the deprecation warning. Marking as "breaking" as it does change the scores & behavior, but the API provide is the same.
1 parent f3a1664 commit a5c57ba

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

build-tools-internal/src/main/resources/changelog-schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
"Painless",
296296
"REST API",
297297
"Rollup",
298+
"Search",
298299
"System requirement",
299300
"Transform"
300301
]

docs/changelog/118671.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pr: 118671
2+
summary: Adjust `random_score` default field to `_seq_no` field
3+
area: Search
4+
type: breaking
5+
issues: []
6+
breaking:
7+
title: Adjust `random_score` default field to `_seq_no` field
8+
area: Search
9+
details: When providing a 'seed' parameter to a 'random_score' function in the 'function_score' query but NOT providing a 'field', the default 'field' is switched from '_id' to '_seq_no'.
10+
impact: The random scoring and ordering may change when providing a 'seed' and not providing a 'field' to a 'random_score' function.
11+
notable: false

server/src/main/java/org/elasticsearch/index/query/functionscore/RandomScoreFunctionBuilder.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
import org.elasticsearch.common.ParsingException;
1414
import org.elasticsearch.common.io.stream.StreamInput;
1515
import org.elasticsearch.common.io.stream.StreamOutput;
16-
import org.elasticsearch.common.logging.DeprecationCategory;
17-
import org.elasticsearch.common.logging.DeprecationLogger;
1816
import org.elasticsearch.common.lucene.search.function.RandomScoreFunction;
1917
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
20-
import org.elasticsearch.index.mapper.IdFieldMapper;
2118
import org.elasticsearch.index.mapper.MappedFieldType;
19+
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
2220
import org.elasticsearch.index.query.SearchExecutionContext;
2321
import org.elasticsearch.xcontent.XContentBuilder;
2422
import org.elasticsearch.xcontent.XContentParser;
@@ -30,7 +28,6 @@
3028
* A function that computes a random score for the matched documents
3129
*/
3230
public class RandomScoreFunctionBuilder extends ScoreFunctionBuilder<RandomScoreFunctionBuilder> {
33-
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RandomScoreFunctionBuilder.class);
3431

3532
public static final String NAME = "random_score";
3633
private String field;
@@ -140,17 +137,7 @@ protected ScoreFunction doToFunction(SearchExecutionContext context) {
140137
// DocID-based random score generation
141138
return new RandomScoreFunction(hash(context.nowInMillis()), salt, null);
142139
} else {
143-
String fieldName;
144-
if (field == null) {
145-
deprecationLogger.warn(
146-
DeprecationCategory.QUERIES,
147-
"seed_requires_field",
148-
"As of version 7.0 Elasticsearch will require that a [field] parameter is provided when a [seed] is set"
149-
);
150-
fieldName = IdFieldMapper.NAME;
151-
} else {
152-
fieldName = field;
153-
}
140+
final String fieldName = Objects.requireNonNullElse(field, SeqNoFieldMapper.NAME);
154141
if (context.isFieldMapped(fieldName) == false) {
155142
if (context.hasMappings() == false) {
156143
// no mappings: the index is empty anyway

server/src/test/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilderTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public void testRandomScoreFunctionWithSeedNoField() throws Exception {
6666
Mockito.when(context.getFieldType(IdFieldMapper.NAME)).thenReturn(new KeywordFieldMapper.KeywordFieldType(IdFieldMapper.NAME));
6767
Mockito.when(context.isFieldMapped(IdFieldMapper.NAME)).thenReturn(true);
6868
builder.toFunction(context);
69-
assertWarnings("As of version 7.0 Elasticsearch will require that a [field] parameter is provided when a [seed] is set");
7069
}
7170

7271
public void testRandomScoreFunctionWithSeed() throws Exception {

0 commit comments

Comments
 (0)