Skip to content

Commit c4015ad

Browse files
authored
Merge branch 'main' into esql_copy_sign_docs_and_fix
2 parents 82282ba + e69a05b commit c4015ad

File tree

22 files changed

+840
-122
lines changed

22 files changed

+840
-122
lines changed

docs/changelog/132138.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 132138
2+
summary: Fix lookup index resolution when field-caps returns empty mapping
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 132105

docs/changelog/132362.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 132362
2+
summary: Inference API disable partial search results
3+
area: Machine Learning
4+
type: bug
5+
issues: []

muted-tests.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,6 @@ tests:
506506
- class: org.elasticsearch.xpack.ml.integration.AutodetectMemoryLimitIT
507507
method: testTooManyByAndOverFields
508508
issue: https://github.com/elastic/elasticsearch/issues/132310
509-
- class: org.elasticsearch.xpack.logsdb.qa.LogsDbVersusLogsDbReindexedIntoStandardModeChallengeRestIT
510-
method: testTermsQuery
511-
issue: https://github.com/elastic/elasticsearch/issues/132335
512-
- class: org.elasticsearch.xpack.logsdb.qa.LogsDbVersusReindexedIntoStoredSourceChallengeRestIT
513-
method: testTermsQuery
514-
issue: https://github.com/elastic/elasticsearch/issues/132336
515-
- class: org.elasticsearch.xpack.logsdb.qa.LogsDbVersusReindexedLogsDbChallengeRestIT
516-
method: testTermsQuery
517-
issue: https://github.com/elastic/elasticsearch/issues/132337
518509
- class: org.elasticsearch.xpack.esql.action.CrossClusterAsyncQueryIT
519510
method: testBadAsyncId
520511
issue: https://github.com/elastic/elasticsearch/issues/132353

server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void handleIncomingPublishRequest(
130130
ActionListener<PublishWithJoinResponse> publishResponseListener
131131
) throws IOException {
132132
assert ThreadPool.assertCurrentThreadPool(GENERIC);
133-
final Compressor compressor = CompressorFactory.compressor(request.bytes());
133+
final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes());
134134
StreamInput in = request.bytes().streamInput();
135135
try {
136136
if (compressor != null) {

server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public CompressedXContent(ToXContent xcontent, ToXContent.Params params) throws
135135
* that may already be compressed.
136136
*/
137137
public CompressedXContent(BytesReference data) throws IOException {
138-
Compressor compressor = CompressorFactory.compressor(data);
138+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(data);
139139
if (compressor != null) {
140140
// already compressed...
141141
this.bytes = BytesReference.toBytes(data);
@@ -148,7 +148,7 @@ public CompressedXContent(BytesReference data) throws IOException {
148148
}
149149

150150
private void assertConsistent() {
151-
assert CompressorFactory.compressor(new BytesArray(bytes)) != null;
151+
assert CompressorFactory.compressorForUnknownXContentType(new BytesArray(bytes)) != null;
152152
assert this.sha256.equals(sha256(uncompressed()));
153153
assert this.sha256.equals(sha256FromCompressed(bytes));
154154
}

server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CompressorFactory {
2222
public static final Compressor COMPRESSOR = new DeflateCompressor();
2323

2424
public static boolean isCompressed(BytesReference bytes) {
25-
return compressor(bytes) != null;
25+
return compressorForUnknownXContentType(bytes) != null;
2626
}
2727

2828
@Nullable
@@ -34,6 +34,15 @@ public static Compressor compressor(BytesReference bytes) {
3434
assert XContentHelper.xContentType(bytes) == null;
3535
return COMPRESSOR;
3636
}
37+
return null;
38+
}
39+
40+
@Nullable
41+
public static Compressor compressorForUnknownXContentType(BytesReference bytes) {
42+
Compressor compressor = compressor(bytes);
43+
if (compressor != null) {
44+
return compressor;
45+
}
3746

3847
XContentType contentType = XContentHelper.xContentType(bytes);
3948
if (contentType == null) {
@@ -56,13 +65,13 @@ private static boolean isAncient(BytesReference bytes) {
5665
* @throws NullPointerException a NullPointerException will be thrown when bytes is null
5766
*/
5867
public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
59-
Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
68+
Compressor compressor = compressorForUnknownXContentType(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
6069
return compressor == null ? bytes : compressor.uncompress(bytes);
6170
}
6271

6372
/** Decompress the provided {@link BytesReference}. */
6473
public static BytesReference uncompress(BytesReference bytes) throws IOException {
65-
Compressor compressor = compressor(bytes);
74+
Compressor compressor = compressorForUnknownXContentType(bytes);
6675
if (compressor == null) {
6776
throw new NotCompressedException();
6877
}

server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static XContentParser createParser(NamedXContentRegistry registry, Deprec
6767
*/
6868
@Deprecated
6969
public static XContentParser createParser(XContentParserConfiguration config, BytesReference bytes) throws IOException {
70-
Compressor compressor = CompressorFactory.compressor(bytes);
70+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
7171
if (compressor != null) {
7272
InputStream compressedInput = compressor.threadLocalInputStream(bytes.streamInput());
7373
if (compressedInput.markSupported() == false) {
@@ -568,7 +568,7 @@ public interface CustomMerge {
568568
@Deprecated
569569
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params)
570570
throws IOException {
571-
Compressor compressor = CompressorFactory.compressor(source);
571+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
572572
if (compressor != null) {
573573
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
574574
builder.rawField(field, compressedStreamInput);
@@ -592,7 +592,7 @@ public static void writeRawField(
592592
ToXContent.Params params
593593
) throws IOException {
594594
Objects.requireNonNull(xContentType);
595-
Compressor compressor = CompressorFactory.compressor(source);
595+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
596596
if (compressor != null) {
597597
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
598598
builder.rawField(field, compressedStreamInput, xContentType);
@@ -677,7 +677,7 @@ public static BytesReference toXContent(ChunkedToXContent toXContent, XContentTy
677677
*/
678678
@Deprecated
679679
public static XContentType xContentTypeMayCompressed(BytesReference bytes) {
680-
Compressor compressor = CompressorFactory.compressor(bytes);
680+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
681681
if (compressor != null) {
682682
try {
683683
InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput());

server/src/main/java/org/elasticsearch/index/codec/vectors/cluster/HierarchicalKMeans.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515
import java.util.Arrays;
16+
import java.util.Objects;
1617

1718
/**
1819
* An implementation of the hierarchical k-means algorithm that better partitions data than naive k-means
@@ -148,30 +149,32 @@ static FloatVectorValues createClusterSlice(int clusterSize, int cluster, FloatV
148149
}
149150

150151
void updateAssignmentsWithRecursiveSplit(KMeansIntermediate current, int cluster, KMeansIntermediate subPartitions) {
152+
if (subPartitions.centroids().length == 0) {
153+
return; // nothing to do, sub-partitions is empty
154+
}
151155
int orgCentroidsSize = current.centroids().length;
152156
int newCentroidsSize = current.centroids().length + subPartitions.centroids().length - 1;
153157

154158
// update based on the outcomes from the split clusters recursion
155-
if (subPartitions.centroids().length > 1) {
156-
float[][] newCentroids = new float[newCentroidsSize][dimension];
157-
System.arraycopy(current.centroids(), 0, newCentroids, 0, current.centroids().length);
159+
float[][] newCentroids = new float[newCentroidsSize][];
160+
System.arraycopy(current.centroids(), 0, newCentroids, 0, current.centroids().length);
158161

159-
// replace the original cluster
160-
int origCentroidOrd = 0;
161-
newCentroids[cluster] = subPartitions.centroids()[0];
162+
// replace the original cluster
163+
int origCentroidOrd = 0;
164+
newCentroids[cluster] = subPartitions.centroids()[0];
162165

163-
// append the remainder
164-
System.arraycopy(subPartitions.centroids(), 1, newCentroids, current.centroids().length, subPartitions.centroids().length - 1);
166+
// append the remainder
167+
System.arraycopy(subPartitions.centroids(), 1, newCentroids, current.centroids().length, subPartitions.centroids().length - 1);
168+
assert Arrays.stream(newCentroids).allMatch(Objects::nonNull);
165169

166-
current.setCentroids(newCentroids);
170+
current.setCentroids(newCentroids);
167171

168-
for (int i = 0; i < subPartitions.assignments().length; i++) {
169-
// this is a new centroid that was added, and so we'll need to remap it
170-
if (subPartitions.assignments()[i] != origCentroidOrd) {
171-
int parentOrd = subPartitions.ordToDoc(i);
172-
assert current.assignments()[parentOrd] == cluster;
173-
current.assignments()[parentOrd] = subPartitions.assignments()[i] + orgCentroidsSize - 1;
174-
}
172+
for (int i = 0; i < subPartitions.assignments().length; i++) {
173+
// this is a new centroid that was added, and so we'll need to remap it
174+
if (subPartitions.assignments()[i] != origCentroidOrd) {
175+
int parentOrd = subPartitions.ordToDoc(i);
176+
assert current.assignments()[parentOrd] == cluster;
177+
current.assignments()[parentOrd] = subPartitions.assignments()[i] + orgCentroidsSize - 1;
175178
}
176179
}
177180
}

server/src/main/java/org/elasticsearch/index/codec/vectors/cluster/KMeansLocal.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.index.codec.vectors.cluster;
1111

1212
import org.apache.lucene.index.FloatVectorValues;
13+
import org.apache.lucene.util.FixedBitSet;
1314
import org.apache.lucene.util.VectorUtil;
1415
import org.apache.lucene.util.hnsw.IntToIntFunction;
1516
import org.elasticsearch.index.codec.vectors.SampleReader;
@@ -70,17 +71,14 @@ private static boolean stepLloyd(
7071
FloatVectorValues vectors,
7172
IntToIntFunction translateOrd,
7273
float[][] centroids,
73-
float[][] nextCentroids,
74+
FixedBitSet centroidChanged,
75+
int[] centroidCounts,
7476
int[] assignments,
7577
NeighborHood[] neighborhoods
7678
) throws IOException {
7779
boolean changed = false;
7880
int dim = vectors.dimension();
79-
int[] centroidCounts = new int[centroids.length];
80-
81-
for (float[] nextCentroid : nextCentroids) {
82-
Arrays.fill(nextCentroid, 0.0f);
83-
}
81+
centroidChanged.clear();
8482
final float[] distances = new float[4];
8583
for (int idx = 0; idx < vectors.size(); idx++) {
8684
float[] vector = vectors.vectorValue(idx);
@@ -93,20 +91,39 @@ private static boolean stepLloyd(
9391
bestCentroidOffset = getBestCentroid(centroids, vector, distances);
9492
}
9593
if (assignment != bestCentroidOffset) {
94+
if (assignment != -1) {
95+
centroidChanged.set(assignment);
96+
}
97+
centroidChanged.set(bestCentroidOffset);
9698
assignments[vectorOrd] = bestCentroidOffset;
9799
changed = true;
98100
}
99-
centroidCounts[bestCentroidOffset]++;
100-
for (int d = 0; d < dim; d++) {
101-
nextCentroids[bestCentroidOffset][d] += vector[d];
102-
}
103101
}
102+
if (changed) {
103+
Arrays.fill(centroidCounts, 0);
104+
for (int idx = 0; idx < vectors.size(); idx++) {
105+
final int assignment = assignments[translateOrd.apply(idx)];
106+
if (centroidChanged.get(assignment)) {
107+
float[] centroid = centroids[assignment];
108+
if (centroidCounts[assignment]++ == 0) {
109+
Arrays.fill(centroid, 0.0f);
110+
}
111+
float[] vector = vectors.vectorValue(idx);
112+
for (int d = 0; d < dim; d++) {
113+
centroid[d] += vector[d];
114+
}
115+
}
116+
}
104117

105-
for (int clusterIdx = 0; clusterIdx < centroids.length; clusterIdx++) {
106-
if (centroidCounts[clusterIdx] > 0) {
107-
float countF = (float) centroidCounts[clusterIdx];
108-
for (int d = 0; d < dim; d++) {
109-
centroids[clusterIdx][d] = nextCentroids[clusterIdx][d] / countF;
118+
for (int clusterIdx = 0; clusterIdx < centroids.length; clusterIdx++) {
119+
if (centroidChanged.get(clusterIdx)) {
120+
float count = (float) centroidCounts[clusterIdx];
121+
if (count > 0) {
122+
float[] centroid = centroids[clusterIdx];
123+
for (int d = 0; d < dim; d++) {
124+
centroid[d] /= count;
125+
}
126+
}
110127
}
111128
}
112129
}
@@ -420,17 +437,18 @@ private void cluster(FloatVectorValues vectors, KMeansIntermediate kMeansInterme
420437
}
421438

422439
assert assignments.length == n;
423-
float[][] nextCentroids = new float[centroids.length][vectors.dimension()];
440+
FixedBitSet centroidChanged = new FixedBitSet(centroids.length);
441+
int[] centroidCounts = new int[centroids.length];
424442
for (int i = 0; i < maxIterations; i++) {
425443
// This is potentially sampled, so we need to translate ordinals
426-
if (stepLloyd(sampledVectors, translateOrd, centroids, nextCentroids, assignments, neighborhoods) == false) {
444+
if (stepLloyd(sampledVectors, translateOrd, centroids, centroidChanged, centroidCounts, assignments, neighborhoods) == false) {
427445
break;
428446
}
429447
}
430448
// If we were sampled, do a once over the full set of vectors to finalize the centroids
431449
if (sampleSize < n || maxIterations == 0) {
432450
// No ordinal translation needed here, we are using the full set of vectors
433-
stepLloyd(vectors, i -> i, centroids, nextCentroids, assignments, neighborhoods);
451+
stepLloyd(vectors, i -> i, centroids, centroidChanged, centroidCounts, assignments, neighborhoods);
434452
}
435453
}
436454

server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static boolean isDiff(BytesTransportRequest request, TransportVersion ve
145145
StreamInput in = null;
146146
try {
147147
in = request.bytes().streamInput();
148-
final Compressor compressor = CompressorFactory.compressor(request.bytes());
148+
final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes());
149149
if (compressor != null) {
150150
in = compressor.threadLocalStreamInput(in);
151151
}

0 commit comments

Comments
 (0)