Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization of empty field capabilities response #33263

Merged
merged 4 commits into from
Aug 30, 2018
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 @@ -166,14 +166,14 @@ public String getName() {
}

/**
* Whether this field is indexed for search on all indices.
* Whether this field can be aggregated on all indices.
*/
public boolean isAggregatable() {
return isAggregatable;
}

/**
* Whether this field can be aggregated on all indices.
* Whether this field is indexed for search on all indices.
*/
public boolean isSearchable() {
return isSearchable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public String[] fields() {
}

/**
*
* The list of indices to lookup
*/
public FieldCapabilitiesRequest indices(String... indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -56,15 +57,15 @@ public class FieldCapabilitiesResponse extends ActionResponse implements ToXCont

private FieldCapabilitiesResponse(Map<String, Map<String, FieldCapabilities>> responseMap,
List<FieldCapabilitiesIndexResponse> indexResponses) {
this.responseMap = responseMap;
this.indexResponses = indexResponses;
this.responseMap = Objects.requireNonNull(responseMap);
this.indexResponses = Objects.requireNonNull(indexResponses);
}

/**
* Used for serialization
*/
FieldCapabilitiesResponse() {
this.responseMap = Collections.emptyMap();
this(Collections.emptyMap(), Collections.emptyList());
}

/**
Expand All @@ -81,6 +82,7 @@ public Map<String, Map<String, FieldCapabilities>> get() {
List<FieldCapabilitiesIndexResponse> getIndexResponses() {
return indexResponses;
}

/**
*
* Get the field capabilities per type for the provided {@code field}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void doExecute(Task task, FieldCapabilitiesRequest request, final Acti
}
};
if (totalNumRequest == 0) {
listener.onResponse(new FieldCapabilitiesResponse());
listener.onResponse(new FieldCapabilitiesResponse(Collections.emptyMap()));
} else {
ActionListener<FieldCapabilitiesIndexResponse> innerListener = new ActionListener<FieldCapabilitiesIndexResponse>() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/org/elasticsearch/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public interface Client extends ElasticsearchClient, Releasable {
/**
* Builder for the field capabilities request.
*/
FieldCapabilitiesRequestBuilder prepareFieldCaps();
FieldCapabilitiesRequestBuilder prepareFieldCaps(String... indices);

/**
* An action that returns the field capabilities from the provided request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ public ActionFuture<FieldCapabilitiesResponse> fieldCaps(FieldCapabilitiesReques
}

@Override
public FieldCapabilitiesRequestBuilder prepareFieldCaps() {
return new FieldCapabilitiesRequestBuilder(this, FieldCapabilitiesAction.INSTANCE);
public FieldCapabilitiesRequestBuilder prepareFieldCaps(String... indices) {
return new FieldCapabilitiesRequestBuilder(this, FieldCapabilitiesAction.INSTANCE, indices);
}

static class Admin implements AdminClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
import org.elasticsearch.test.AbstractStreamableXContentTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiLettersOfLength;


public class FieldCapabilitiesResponseTests extends AbstractStreamableXContentTestCase<FieldCapabilitiesResponse> {

Expand All @@ -48,22 +52,46 @@ protected FieldCapabilitiesResponse createBlankInstance() {

@Override
protected FieldCapabilitiesResponse createTestInstance() {
Map<String, Map<String, FieldCapabilities>> responses = new HashMap<>();
if (randomBoolean()) {
// merged responses
Map<String, Map<String, FieldCapabilities>> responses = new HashMap<>();

String[] fields = generateRandomStringArray(5, 10, false, true);
assertNotNull(fields);

for (String field : fields) {
Map<String, FieldCapabilities> typesToCapabilities = new HashMap<>();
String[] types = generateRandomStringArray(5, 10, false, false);
assertNotNull(types);

for (String type : types) {
typesToCapabilities.put(type, FieldCapabilitiesTests.randomFieldCaps(field));
}
responses.put(field, typesToCapabilities);
}
return new FieldCapabilitiesResponse(responses);
} else {
// non-merged responses
List<FieldCapabilitiesIndexResponse> responses = new ArrayList<>();
int numResponse = randomIntBetween(0, 10);
for (int i = 0; i < numResponse; i++) {
responses.add(createRandomIndexResponse());
}
return new FieldCapabilitiesResponse(responses);
}
}


private FieldCapabilitiesIndexResponse createRandomIndexResponse() {
Map<String, FieldCapabilities> responses = new HashMap<>();

String[] fields = generateRandomStringArray(5, 10, false, true);
assertNotNull(fields);

for (String field : fields) {
Map<String, FieldCapabilities> typesToCapabilities = new HashMap<>();
String[] types = generateRandomStringArray(5, 10, false, false);
assertNotNull(types);

for (String type : types) {
typesToCapabilities.put(type, FieldCapabilitiesTests.randomFieldCaps(field));
}
responses.put(field, typesToCapabilities);
responses.put(field, FieldCapabilitiesTests.randomFieldCaps(field));
}
return new FieldCapabilitiesResponse(responses);
return new FieldCapabilitiesIndexResponse(randomAsciiLettersOfLength(10), responses);
}

@Override
Expand Down Expand Up @@ -138,6 +166,11 @@ public void testToXContent() throws IOException {
"}").replaceAll("\\s+", ""), generatedResponse);
}

public void testEmptyResponse() throws IOException {
FieldCapabilitiesResponse testInstance = new FieldCapabilitiesResponse();
assertSerialization(testInstance);
}

private static FieldCapabilitiesResponse createSimpleResponse() {
Map<String, FieldCapabilities> titleCapabilities = new HashMap<>();
titleCapabilities.put("text", new FieldCapabilities("title", "text", true, false));
Expand Down