Skip to content

Commit 5f87203

Browse files
authored
Merge branch 'main' into windows-nulls
2 parents 5bf824f + 876c456 commit 5f87203

File tree

17 files changed

+281
-108
lines changed

17 files changed

+281
-108
lines changed

docs/changelog/128854.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pr: 128854
2+
summary: Mark token pruning for sparse vector as GA
3+
area: Machine Learning
4+
type: feature
5+
issues: []
6+
highlight:
7+
title: Mark Token Pruning for Sparse Vector as GA
8+
body: |-
9+
Token pruning for sparse_vector queries has been live since 8.13 as tech preview.
10+
As of 8.19.0 and 9.1.0, this is now generally available.
11+
notable: true

docs/reference/query-languages/query-dsl/query-dsl-sparse-vector-query.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,36 @@ GET _search
6262
`query_vector`
6363
: (Optional, dictionary) A dictionary of token-weight pairs representing the precomputed query vector to search. Searching using this query vector will bypass additional inference. Only one of `inference_id` and `query_vector` is allowed.
6464

65-
`prune`
66-
: (Optional, boolean) [preview] Whether to perform pruning, omitting the non-significant tokens from the query to improve query performance. If `prune` is true but the `pruning_config` is not specified, pruning will occur but default values will be used. Default: false.
65+
`prune` {applies_to}`stack: preview 9.0, ga 9.1`
66+
: (Optional, boolean) Whether to perform pruning, omitting the non-significant tokens from the query to improve query performance. If `prune` is true but the `pruning_config` is not specified, pruning will occur but default values will be used. Default: false.
6767

68-
`pruning_config`
69-
: (Optional, object) [preview] Optional pruning configuration. If enabled, this will omit non-significant tokens from the query in order to improve query performance. This is only used if `prune` is set to `true`. If `prune` is set to `true` but `pruning_config` is not specified, default values will be used.
68+
`pruning_config` {applies_to}`stack: preview 9.0, ga 9.1`
69+
: (Optional, object) Optional pruning configuration. If enabled, this will omit non-significant tokens from the query in order to improve query performance. This is only used if `prune` is set to `true`. If `prune` is set to `true` but `pruning_config` is not specified, default values will be used.
7070

7171
Parameters for `pruning_config` are:
7272

7373
`tokens_freq_ratio_threshold`
74-
: (Optional, integer) [preview] Tokens whose frequency is more than `tokens_freq_ratio_threshold` times the average frequency of all tokens in the specified field are considered outliers and pruned. This value must between 1 and 100. Default: `5`.
74+
: (Optional, integer) Tokens whose frequency is more than `tokens_freq_ratio_threshold` times the average frequency of all tokens in the specified field are considered outliers and pruned. This value must between 1 and 100. Default: `5`.
7575

7676
`tokens_weight_threshold`
77-
: (Optional, float) [preview] Tokens whose weight is less than `tokens_weight_threshold` are considered insignificant and pruned. This value must be between 0 and 1. Default: `0.4`.
77+
: (Optional, float) Tokens whose weight is less than `tokens_weight_threshold` are considered insignificant and pruned. This value must be between 0 and 1. Default: `0.4`.
7878

7979
`only_score_pruned_tokens`
80-
: (Optional, boolean) [preview] If `true` we only input pruned tokens into scoring, and discard non-pruned tokens. It is strongly recommended to set this to `false` for the main query, but this can be set to `true` for a rescore query to get more relevant results. Default: `false`.
80+
: (Optional, boolean) If `true` we only input pruned tokens into scoring, and discard non-pruned tokens. It is strongly recommended to set this to `false` for the main query, but this can be set to `true` for a rescore query to get more relevant results. Default: `false`.
8181

8282
::::{note}
8383
The default values for `tokens_freq_ratio_threshold` and `tokens_weight_threshold` were chosen based on tests using ELSERv2 that provided the most optimal results.
8484
::::
8585

86+
When token pruning is applied, non-significant tokens will be pruned from the query.
87+
Non-significant tokens can be defined as tokens that meet both of the following criteria:
88+
* The token appears much more frequently than most tokens, indicating that it is a very common word and may not benefit the overall search results much.
89+
* The weight/score is so low that the token is likely not very relevant to the original term
8690

91+
Both the token frequency threshold and weight threshold must show the token is non-significant in order for the token to be pruned.
92+
This ensures that:
93+
* The tokens that are kept are frequent enough and have significant scoring.
94+
* Very infrequent tokens that may not have as high of a score are removed.
8795

8896
## Example ELSER query [sparse-vector-query-example]
8997

@@ -198,7 +206,7 @@ GET my-index/_search
198206

199207
## Example ELSER query with pruning configuration and rescore [sparse-vector-query-with-pruning-config-and-rescore-example]
200208

201-
The following is an extension to the above example that adds a [preview] pruning configuration to the `sparse_vector` query. The pruning configuration identifies non-significant tokens to prune from the query in order to improve query performance.
209+
The following is an extension to the above example that adds a pruning configuration to the `sparse_vector` query. The pruning configuration identifies non-significant tokens to prune from the query in order to improve query performance.
202210

203211
Token pruning happens at the shard level. While this should result in the same tokens being labeled as insignificant across shards, this is not guaranteed based on the composition of each shard. Therefore, if you are running `sparse_vector` with a `pruning_config` on a multi-shard index, we strongly recommend adding a [Rescore filtered search results](/reference/elasticsearch/rest-apis/filter-search-results.md#rescore) function with the tokens that were originally pruned from the query. This will help mitigate any shard-level inconsistency with pruned tokens and provide better relevance overall.
204212

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,6 @@ tests:
565565
- class: org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapperTests
566566
method: testExistsQueryMinimalMapping
567567
issue: https://github.com/elastic/elasticsearch/issues/129911
568-
- class: org.elasticsearch.index.mapper.DynamicMappingIT
569-
method: testDenseVectorDynamicMapping
570-
issue: https://github.com/elastic/elasticsearch/issues/129928
571568

572569
# Examples:
573570
#

server/src/internalClusterTest/java/org/elasticsearch/index/mapper/DynamicMappingIT.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -920,19 +920,27 @@ public void testDenseVectorDynamicMapping() throws Exception {
920920

921921
client().index(
922922
new IndexRequest("test").source("vector_int8", Randomness.get().doubles(BBQ_DIMS_DEFAULT_THRESHOLD - 1, 0.0, 5.0).toArray())
923+
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
923924
).get();
924925
client().index(
925926
new IndexRequest("test").source("vector_bbq", Randomness.get().doubles(BBQ_DIMS_DEFAULT_THRESHOLD, 0.0, 5.0).toArray())
927+
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
926928
).get();
927-
Map<String, Object> mappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "test")
928-
.get()
929-
.mappings()
930-
.get("test")
931-
.sourceAsMap();
932-
assertTrue(new WriteField("properties.vector_int8", () -> mappings).exists());
933-
assertTrue(new WriteField("properties.vector_int8.index_options.type", () -> mappings).get(null).toString().equals("int8_hnsw"));
934-
assertTrue(new WriteField("properties.vector_bbq", () -> mappings).exists());
935-
assertTrue(new WriteField("properties.vector_bbq.index_options.type", () -> mappings).get(null).toString().equals("bbq_hnsw"));
929+
930+
assertBusy(() -> {
931+
Map<String, Object> mappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "test")
932+
.get()
933+
.mappings()
934+
.get("test")
935+
.sourceAsMap();
936+
937+
assertTrue(new WriteField("properties.vector_int8", () -> mappings).exists());
938+
assertTrue(
939+
new WriteField("properties.vector_int8.index_options.type", () -> mappings).get(null).toString().equals("int8_hnsw")
940+
);
941+
assertTrue(new WriteField("properties.vector_bbq", () -> mappings).exists());
942+
assertTrue(new WriteField("properties.vector_bbq.index_options.type", () -> mappings).get(null).toString().equals("bbq_hnsw"));
943+
});
936944
}
937945

938946
public void testBBQDynamicMappingWhenFirstIngestingDoc() throws Exception {
@@ -954,14 +962,19 @@ public void testBBQDynamicMappingWhenFirstIngestingDoc() throws Exception {
954962
assertTrue(new WriteField("properties.vector", () -> mappings).exists());
955963
assertFalse(new WriteField("properties.vector.index_options.type", () -> mappings).exists());
956964

957-
client().index(new IndexRequest("test").source("vector", Randomness.get().doubles(BBQ_DIMS_DEFAULT_THRESHOLD, 0.0, 5.0).toArray()))
958-
.get();
959-
Map<String, Object> updatedMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "test")
960-
.get()
961-
.mappings()
962-
.get("test")
963-
.sourceAsMap();
964-
assertTrue(new WriteField("properties.vector", () -> updatedMappings).exists());
965-
assertTrue(new WriteField("properties.vector.index_options.type", () -> updatedMappings).get(null).toString().equals("bbq_hnsw"));
965+
client().index(
966+
new IndexRequest("test").source("vector", Randomness.get().doubles(BBQ_DIMS_DEFAULT_THRESHOLD, 0.0, 5.0).toArray())
967+
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
968+
).get();
969+
970+
assertBusy(() -> {
971+
Map<String, Object> updatedMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "test")
972+
.get()
973+
.mappings()
974+
.get("test")
975+
.sourceAsMap();
976+
assertTrue(new WriteField("properties.vector", () -> updatedMappings).exists());
977+
assertTrue(new WriteField("properties.vector.index_options.type", () -> updatedMappings).get("").toString().equals("bbq_hnsw"));
978+
});
966979
}
967980
}

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ static TransportVersion def(int id) {
314314
public static final TransportVersion ML_INFERENCE_CUSTOM_SERVICE_INPUT_TYPE = def(9_105_0_00);
315315
public static final TransportVersion ML_INFERENCE_SAGEMAKER_ELASTIC = def(9_106_0_00);
316316
public static final TransportVersion SPARSE_VECTOR_FIELD_PRUNING_OPTIONS = def(9_107_0_00);
317+
public static final TransportVersion CLUSTER_STATE_PROJECTS_SETTINGS = def(9_108_0_00);
317318

318319
/*
319320
* STOP! READ THIS FIRST! No, really,

server/src/main/java/org/elasticsearch/cluster/ClusterModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
2929
import org.elasticsearch.cluster.metadata.StreamsMetadata;
3030
import org.elasticsearch.cluster.project.ProjectResolver;
31+
import org.elasticsearch.cluster.project.ProjectStateRegistry;
3132
import org.elasticsearch.cluster.routing.DelayedAllocationService;
3233
import org.elasticsearch.cluster.routing.ShardRouting;
3334
import org.elasticsearch.cluster.routing.ShardRoutingRoleStrategy;
@@ -292,6 +293,7 @@ public static List<Entry> getNamedWriteables() {
292293
RegisteredPolicySnapshots::new,
293294
RegisteredPolicySnapshots.RegisteredSnapshotsDiff::new
294295
);
296+
registerClusterCustom(entries, ProjectStateRegistry.TYPE, ProjectStateRegistry::new, ProjectStateRegistry::readDiffFrom);
295297
// Secrets
296298
registerClusterCustom(entries, ClusterSecrets.TYPE, ClusterSecrets::new, ClusterSecrets::readDiffFrom);
297299
registerProjectCustom(entries, ProjectSecrets.TYPE, ProjectSecrets::new, ProjectSecrets::readDiffFrom);

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ public Builder(ClusterState state) {
10471047
public Builder(ClusterName clusterName) {
10481048
this.compatibilityVersions = new HashMap<>();
10491049
this.nodeFeatures = new HashMap<>();
1050-
customs = ImmutableOpenMap.builder();
1050+
this.customs = ImmutableOpenMap.builder();
10511051
this.clusterName = clusterName;
10521052
}
10531053

@@ -1330,7 +1330,6 @@ public void writeTo(StreamOutput out) throws IOException {
13301330
}
13311331

13321332
private static class ClusterStateDiff implements Diff<ClusterState> {
1333-
13341333
private final long toVersion;
13351334

13361335
private final String fromUuid;

server/src/main/java/org/elasticsearch/cluster/ProjectState.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import org.elasticsearch.cluster.block.ClusterBlocks;
1313
import org.elasticsearch.cluster.metadata.ProjectId;
1414
import org.elasticsearch.cluster.metadata.ProjectMetadata;
15+
import org.elasticsearch.cluster.project.ProjectStateRegistry;
1516
import org.elasticsearch.cluster.routing.RoutingTable;
1617
import org.elasticsearch.common.Strings;
18+
import org.elasticsearch.common.settings.Settings;
1719

1820
import java.util.Objects;
1921
import java.util.function.Consumer;
@@ -26,6 +28,7 @@ public final class ProjectState {
2628
private final ClusterState cluster;
2729
private final ProjectId project;
2830
private final ProjectMetadata projectMetadata;
31+
private final Settings projectSettings;
2932
private final RoutingTable routingTable;
3033

3134
ProjectState(ClusterState clusterState, ProjectId projectId) {
@@ -34,6 +37,7 @@ public final class ProjectState {
3437
this.cluster = clusterState;
3538
this.project = projectId;
3639
this.projectMetadata = clusterState.metadata().getProject(projectId);
40+
this.projectSettings = ProjectStateRegistry.getProjectSettings(projectId, clusterState);
3741
this.routingTable = clusterState.routingTable(projectId);
3842
}
3943

@@ -57,6 +61,10 @@ public ClusterBlocks blocks() {
5761
return cluster().blocks();
5862
}
5963

64+
public Settings settings() {
65+
return projectSettings;
66+
}
67+
6068
@Override
6169
public boolean equals(Object obj) {
6270
if (obj == this) return true;

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,7 @@ private MetadataDiff(StreamInput in) throws IOException {
985985
RESERVED_DIFF_VALUE_READER
986986
);
987987

988-
singleProject = new ProjectMetadata.ProjectMetadataDiff(
989-
indices,
990-
templates,
991-
projectCustoms,
992-
DiffableUtils.emptyDiff(),
993-
Settings.EMPTY_DIFF
994-
);
988+
singleProject = new ProjectMetadata.ProjectMetadataDiff(indices, templates, projectCustoms, DiffableUtils.emptyDiff());
995989
multiProject = null;
996990
} else {
997991
fromNodeBeforeMultiProjectsSupport = false;

0 commit comments

Comments
 (0)