Skip to content

Commit 1923634

Browse files
authored
Configure returnLocalAll on FieldCapabilitiesRequest (#134284)
1 parent e7b5842 commit 1923634

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fieldcaps/CCSFieldCapabilitiesIT.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
import static org.hamcrest.Matchers.arrayContaining;
2828
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
2929
import static org.hamcrest.Matchers.containsInAnyOrder;
30+
import static org.hamcrest.Matchers.emptyArray;
3031
import static org.hamcrest.Matchers.equalTo;
32+
import static org.hamcrest.Matchers.hasItems;
3133
import static org.hamcrest.Matchers.hasKey;
3234
import static org.hamcrest.Matchers.hasSize;
35+
import static org.hamcrest.Matchers.not;
3336

3437
public class CCSFieldCapabilitiesIT extends AbstractMultiClustersTestCase {
3538

@@ -162,7 +165,6 @@ public void testIncludeIndices() {
162165
// mapping conflict, therefore indices is always present for `field3`
163166
assertThat(response.getField("field3").get("long").indices(), arrayContaining(remoteIndex));
164167
assertThat(response.getField("field3").get("keyword").indices(), arrayContaining(localIndex));
165-
166168
}
167169

168170
public void testRandomIncludeIndices() {
@@ -241,4 +243,27 @@ public void testIncludeIndicesSwapped() {
241243
assertThat(response.getField("field3").get("long").indices(), arrayContaining(localIndex));
242244
assertThat(response.getField("field3").get("keyword").indices(), arrayContaining(remoteIndex));
243245
}
246+
247+
public void testReturnAllLocal() {
248+
assertAcked(
249+
client().admin()
250+
.indices()
251+
.prepareCreate("index")
252+
.setMapping("@timestamp", "type=date", "field1", "type=keyword", "field2", "type=long")
253+
);
254+
for (var pattern : List.of("fake-remote*:index", "fake-remote*:*")) {
255+
{
256+
// returnLocalAll = true by default
257+
var response = client().prepareFieldCaps(pattern).setFields("*").get();
258+
assertThat(response.getIndices(), arrayContaining("index"));
259+
assertThat(response.get().keySet(), hasItems("@timestamp", "field1", "field2"));
260+
}
261+
{
262+
// user can opt out by explicitly setting returnLocalAll=false
263+
var response = client().prepareFieldCaps(pattern).setFields("*").setReturnLocalAll(false).get();
264+
assertThat(response.getIndices(), emptyArray());
265+
assertThat(response.get().keySet(), not(hasItems("@timestamp", "field1", "field2")));
266+
}
267+
}
268+
}
244269
}

server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public final class FieldCapabilitiesRequest extends LegacyActionRequest implemen
5454
* in the response if required.
5555
*/
5656
private transient boolean includeIndices = false;
57+
58+
/**
59+
* Controls whether all local indices should be returned if no remotes matched
60+
* See {@link org.elasticsearch.transport.RemoteClusterService#groupIndices} returnLocalAll argument.
61+
* This flag is only used locally on the coordinating node for index grouping and does not need to be serialized.
62+
*/
63+
private transient boolean returnLocalAll = true;
64+
5765
// pkg private API mainly for cross cluster search to signal that we do multiple reductions ie. the results should not be merged
5866
private boolean mergeResults = true;
5967
private QueryBuilder indexFilter;
@@ -214,6 +222,11 @@ public FieldCapabilitiesRequest includeIndices(boolean includeIndices) {
214222
return this;
215223
}
216224

225+
public FieldCapabilitiesRequest returnLocalAll(boolean returnLocalAll) {
226+
this.returnLocalAll = returnLocalAll;
227+
return this;
228+
}
229+
217230
@Override
218231
public String[] indices() {
219232
return indices;
@@ -242,6 +255,10 @@ public boolean includeIndices() {
242255
return includeIndices;
243256
}
244257

258+
public boolean returnLocalAll() {
259+
return returnLocalAll;
260+
}
261+
245262
public boolean includeEmptyFields() {
246263
return includeEmptyFields;
247264
}

server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequestBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public FieldCapabilitiesRequestBuilder setIncludeIndices(boolean includeIndices)
5858
request().includeIndices(includeIndices);
5959
return this;
6060
}
61+
62+
public FieldCapabilitiesRequestBuilder setReturnLocalAll(boolean returnLocalAll) {
63+
request().returnLocalAll(returnLocalAll);
64+
return this;
65+
}
6166
}

server/src/main/java/org/elasticsearch/action/fieldcaps/TransportFieldCapabilitiesAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void doExecuteForked(
169169
long nowInMillis = request.nowInMillis() == null ? System.currentTimeMillis() : request.nowInMillis();
170170
final ProjectState projectState = projectResolver.getProjectState(clusterService.state());
171171
final Map<String, OriginalIndices> remoteClusterIndices = transportService.getRemoteClusterService()
172-
.groupIndices(request.indicesOptions(), request.indices());
172+
.groupIndices(request.indicesOptions(), request.indices(), request.returnLocalAll());
173173
final OriginalIndices localIndices = remoteClusterIndices.remove(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY);
174174
final String[] concreteIndices;
175175
if (localIndices == null) {

0 commit comments

Comments
 (0)