Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected ScoreFunction doToFunction(SearchExecutionContext context) {
// DocID-based random score generation
return new RandomScoreFunction(hash(context.nowInMillis()), salt, null);
} else {
final String fieldName = Objects.requireNonNullElse(field, SeqNoFieldMapper.NAME);
final String fieldName = field(context);
if (context.isFieldMapped(fieldName) == false) {
if (context.hasMappings() == false) {
// no mappings: the index is empty anyway
Expand All @@ -155,6 +155,20 @@ protected ScoreFunction doToFunction(SearchExecutionContext context) {
}
}

private String field(SearchExecutionContext context) {
if (field != null) {
return field;
}
if (context.getIndexSettings().sequenceNumbersDisabled()) {
throw new IllegalArgumentException(
"random_score requires a [field] parameter when [index.disable_sequence_numbers] is [true] on index ["
+ context.index()
+ "]"
);
}
return SeqNoFieldMapper.NAME;
}

private static int hash(long value) {
return Long.hashCode(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.index.query.functionscore;

import org.elasticsearch.common.lucene.search.function.RandomScoreFunction;
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.test.AbstractBuilderTestCase;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

/**
* Tests for {@link RandomScoreFunctionBuilder} on an index with sequence numbers disabled.
* The default _seq_no behavior is covered by {@link FunctionScoreQueryBuilderTests}.
*/
public class RandomScoreFunctionBuilderTests extends AbstractBuilderTestCase {

@Override
protected Settings createTestIndexSettings() {
assumeTrue("Test requires disable_sequence_numbers feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
return Settings.builder()
.put("index.version.created", IndexVersion.current())
.put(IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(), true)
.put(IndexSettings.SEQ_NO_INDEX_OPTIONS_SETTING.getKey(), SeqNoFieldMapper.SeqNoIndexOptions.DOC_VALUES_ONLY)
.build();
}

public void testRandomScoreWithoutFieldRequiresFieldWhenSeqNoDisabled() throws Exception {
assumeTrue("Test requires disable_sequence_numbers feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
RandomScoreFunctionBuilder builder = new RandomScoreFunctionBuilder();
builder.seed(42);
SearchExecutionContext context = createSearchExecutionContext();
assertTrue(context.getIndexSettings().sequenceNumbersDisabled());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.toFunction(context));
assertThat(e.getMessage(), containsString("random_score requires a [field] parameter"));
assertThat(e.getMessage(), containsString("index.disable_sequence_numbers"));
}

public void testRandomScoreWithExplicitFieldWhenSeqNoDisabled() throws Exception {
assumeTrue("Test requires disable_sequence_numbers feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
RandomScoreFunctionBuilder builder = new RandomScoreFunctionBuilder();
builder.seed(42);
builder.setField(KEYWORD_FIELD_NAME);
SearchExecutionContext context = createSearchExecutionContext();
assertTrue(context.getIndexSettings().sequenceNumbersDisabled());
ScoreFunction function = builder.toFunction(context);
assertNotNull(function);
assertThat(function, instanceOf(RandomScoreFunction.class));
}

public void testRandomScoreWithoutSeedFallsBackToDocIdWhenSeqNoDisabled() throws Exception {
assumeTrue("Test requires disable_sequence_numbers feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
RandomScoreFunctionBuilder builder = new RandomScoreFunctionBuilder();
SearchExecutionContext context = createSearchExecutionContext();
assertTrue(context.getIndexSettings().sequenceNumbersDisabled());
ScoreFunction function = builder.toFunction(context);
assertNotNull(function);
assertThat(function, instanceOf(RandomScoreFunction.class));
}
}
Loading