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

Bulk merge field-caps responses using mapping hash #86323

Merged
merged 16 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions docs/changelog/86323.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 86323
summary: Speed up merging field-caps responses from single mapping
area: Search
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;

import static java.util.Collections.singletonList;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
Expand All @@ -68,6 +69,8 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

public class FieldCapabilitiesIT extends ESIntegTestCase {

Expand Down Expand Up @@ -566,6 +569,53 @@ public void testRelocation() throws Exception {
}
}

public void testManyIndicesWithSameMapping() {
final String mapping = """
{
"properties": {
"message_field": { "type": "text" },
"value_field": { "type": "long" },
"multi_field" : { "type" : "ip", "fields" : { "keyword" : { "type" : "keyword" } } },
"timestamp": {"type": "date"}
}
}
""";
String[] indices = IntStream.range(1, between(1, 9)).mapToObj(n -> "test_many_index_" + n).toArray(String[]::new);
for (String index : indices) {
assertAcked(client().admin().indices().prepareCreate(index).setMapping(mapping).get());
}
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest();
request.indices("test_many_index_*");
request.fields("*");
if (randomBoolean()) {
request.includeUnmapped(randomBoolean());
}
boolean excludeMultiField = randomBoolean();
if (excludeMultiField) {
request.filters("-multifield");
}
FieldCapabilitiesResponse response = client().execute(FieldCapabilitiesAction.INSTANCE, request).actionGet();
assertThat(response.getIndices(), equalTo(indices));
assertThat(response.get().get("message_field"), hasKey("text"));
assertThat(response.get().get("message_field").get("text").indices(), nullValue());
assertTrue(response.get().get("message_field").get("text").isSearchable());
assertFalse(response.get().get("message_field").get("text").isAggregatable());

assertThat(response.get().get("value_field"), hasKey("long"));
assertThat(response.get().get("value_field").get("long").indices(), nullValue());
assertTrue(response.get().get("value_field").get("long").isSearchable());
assertTrue(response.get().get("value_field").get("long").isAggregatable());

assertThat(response.get().get("timestamp"), hasKey("date"));

assertThat(response.get().get("multi_field"), hasKey("ip"));
if (excludeMultiField) {
assertThat(response.get().get("multi_field.keyword"), not(hasKey("keyword")));
} else {
assertThat(response.get().get("multi_field.keyword"), hasKey("keyword"));
}
}

private void assertIndices(FieldCapabilitiesResponse response, String... indices) {
assertNotNull(response.getIndices());
Arrays.sort(indices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,41 @@ private FieldCapabilitiesResponse merge(
.sorted(Comparator.comparing(FieldCapabilitiesIndexResponse::getIndexName))
.toList();
final String[] indices = indexResponses.stream().map(FieldCapabilitiesIndexResponse::getIndexName).toArray(String[]::new);

if (isFromSingleMapping(indexResponses)) {
FieldCapabilitiesIndexResponse firstResponse = indexResponses.get(0);
final Map<String, IndexFieldCapabilities> fields = ResponseRewriter.rewriteOldResponses(
firstResponse.getOriginVersion(),
firstResponse.get(),
request.filters(),
request.types(),
metadataFieldPred
);
final Map<String, Map<String, FieldCapabilities>> responseMap = fields.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> {
final IndexFieldCapabilities cap = e.getValue();
return Map.of(
cap.getType(),
new FieldCapabilities(
cap.getName(),
cap.getType(),
cap.isMetadatafield(),
cap.isSearchable(),
cap.isAggregatable(),
cap.isDimension(),
cap.getMetricType(),
null,
null,
null,
null,
null,
cap.meta().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> Set.of(v.getValue())))
)
);
}));
return new FieldCapabilitiesResponse(indices, responseMap, failures);
}
final Map<String, Map<String, FieldCapabilities.Builder>> responseMapBuilder = new HashMap<>();
for (FieldCapabilitiesIndexResponse response : indexResponses) {
innerMerge(responseMapBuilder, request, response);
Expand Down Expand Up @@ -272,6 +307,21 @@ private static void addUnmappedFields(String[] indices, String field, Map<String
}
}

private boolean isFromSingleMapping(List<FieldCapabilitiesIndexResponse> indexResponses) {
String sharedMappingHash = null;
for (FieldCapabilitiesIndexResponse resp : indexResponses) {
if (resp.getIndexMappingHash() == null) {
return false;
}
if (sharedMappingHash == null) {
sharedMappingHash = resp.getIndexMappingHash();
} else if (sharedMappingHash.equals(resp.getIndexMappingHash()) == false) {
return false;
}
}
return sharedMappingHash != null;
}

private void innerMerge(
Map<String, Map<String, FieldCapabilities.Builder>> responseMapBuilder,
FieldCapabilitiesRequest request,
Expand Down