Skip to content

Commit 8239527

Browse files
committed
Fix fleet search API with no checkpints (#79400)
Currently the fleet search API does not work properly when no checkpoints are provided. It throws an exception indicating that the number of checkpoints do not match the number of shards. This PR fixes the issue by allowing the search to progress if no checkpoints are provided or if an empty array is provided.
1 parent 0ba0b55 commit 8239527

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

x-pack/plugin/fleet/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/fleet/20_wait_for_checkpoints.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ setup:
3232
- do:
3333
indices.refresh: {}
3434

35+
---
36+
"Execute successful search with wait_for_checkpoints default":
37+
- do:
38+
fleet.search:
39+
index: "test-after-refresh"
40+
allow_partial_search_results: false
41+
42+
body: { query: { match_all: {} } }
43+
44+
- match: { _shards.successful: 1 }
45+
- match: { hits.total.value: 2 }
46+
47+
- do:
48+
fleet.search:
49+
index: "test-after-refresh"
50+
allow_partial_search_results: false
51+
wait_for_checkpoints: []
52+
body: { query: { match_all: { } } }
53+
54+
- match: { _shards.successful: 1 }
55+
- match: { hits.total.value: 2 }
56+
3557
---
3658
"Execute successful after refresh search":
3759
- do:
@@ -114,3 +136,19 @@ setup:
114136
- match: { responses.1._shards.successful: 1 }
115137
- match: { responses.1.hits.total.value: 2 }
116138
- match: { responses.2.error.caused_by.type: "illegal_argument_exception" }
139+
140+
---
141+
"Test msearch wait_for_checkpoints default":
142+
- do:
143+
fleet.msearch:
144+
index: "test-after-refresh"
145+
body:
146+
- { "allow_partial_search_results": false }
147+
- { query: { match_all: { } } }
148+
- { "allow_partial_search_results": false, wait_for_checkpoints: [] }
149+
- { query: { match_all: { } } }
150+
151+
- match: { responses.0._shards.successful: 1 }
152+
- match: { responses.0.hits.total.value: 2 }
153+
- match: { responses.1._shards.successful: 1 }
154+
- match: { responses.1.hits.total.value: 2 }

x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/rest/RestFleetMultiSearchAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
6868
for (int i = 0; i < stringWaitForCheckpoints.length; ++i) {
6969
waitForCheckpoints[i] = Long.parseLong(stringWaitForCheckpoints[i]);
7070
}
71-
searchRequest.setWaitForCheckpoints(Collections.singletonMap("*", waitForCheckpoints));
71+
if (waitForCheckpoints.length != 0) {
72+
searchRequest.setWaitForCheckpoints(Collections.singletonMap("*", waitForCheckpoints));
73+
}
7274
return true;
7375
} else if ("wait_for_checkpoints_timeout".equals(key)) {
7476
final TimeValue waitForCheckpointsTimeout = nodeTimeValue(value, TimeValue.timeValueSeconds(30));
@@ -96,7 +98,9 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
9698
}
9799
}
98100
long[] checkpoints = searchRequest.getWaitForCheckpoints().get("*");
99-
searchRequest.setWaitForCheckpoints(Collections.singletonMap(indices[0], checkpoints));
101+
if (checkpoints != null) {
102+
searchRequest.setWaitForCheckpoints(Collections.singletonMap(indices[0], checkpoints));
103+
}
100104
}
101105

102106
return channel -> {

x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/rest/RestFleetSearchAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
7676
"Fleet search API only supports searching a single index. Found: [" + Arrays.toString(indices1) + "]."
7777
);
7878
}
79-
sr.setWaitForCheckpoints(Collections.singletonMap(indices1[0], waitForCheckpoints));
79+
if (waitForCheckpoints.length != 0) {
80+
sr.setWaitForCheckpoints(Collections.singletonMap(indices1[0], waitForCheckpoints));
81+
}
8082
final TimeValue waitForCheckpointsTimeout = request.paramAsTime(
8183
"wait_for_checkpoints_timeout",
8284
TimeValue.timeValueSeconds(30)

0 commit comments

Comments
 (0)