Skip to content

Commit 50186a4

Browse files
authored
🌊 Streams: Speed up field simulation (#241313)
This PR should speed up field simulation, which is blocking the submit button when defining mappings. It does the following things: * Set a timeout * Reduce sample size * Set terminate_after and disable track_total_hits to get Elasticsearch to stop work as soon as possible * Don't sort the results, otherwise Elasticsearch is forced to evaluate all the docs because otherwise it can't tell which ones would end up on top in the order
1 parent 7225267 commit 50186a4

File tree

1 file changed

+9
-20
lines changed
  • x-pack/platform/plugins/shared/streams/server/routes/internal/streams/schema

1 file changed

+9
-20
lines changed

‎x-pack/platform/plugins/shared/streams/server/routes/internal/streams/schema/route.ts‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { checkAccess } from '../../../../lib/streams/stream_crud';
2020
import { createServerRoute } from '../../../create_server_route';
2121

2222
const UNMAPPED_SAMPLE_SIZE = 500;
23+
const FIELD_SIMULATION_TIMEOUT = '1s';
2324

2425
export const unmappedFieldsRoute = createServerRoute({
2526
endpoint: 'GET /internal/streams/{name}/schema/unmapped_fields',
@@ -90,7 +91,7 @@ export const unmappedFieldsRoute = createServerRoute({
9091
},
9192
});
9293

93-
const FIELD_SIMILATION_SAMPLE_SIZE = 200;
94+
const FIELD_SIMILATION_SAMPLE_SIZE = 20;
9495

9596
export const schemaFieldsSimulationRoute = createServerRoute({
9697
endpoint: 'POST /internal/streams/{name}/schema/fields_simulation',
@@ -149,26 +150,18 @@ export const schemaFieldsSimulationRoute = createServerRoute({
149150
})),
150151
},
151152
},
152-
sort: [
153-
{
154-
'@timestamp': {
155-
order: 'desc' as const,
156-
},
157-
},
158-
],
159153
size: FIELD_SIMILATION_SAMPLE_SIZE,
154+
track_total_hits: false,
155+
terminate_after: FIELD_SIMILATION_SAMPLE_SIZE,
156+
timeout: FIELD_SIMULATION_TIMEOUT,
160157
};
161158

162159
const sampleResults = await scopedClusterClient.asCurrentUser.search({
163160
index: params.path.name,
164161
...documentSamplesSearchBody,
165162
});
166163

167-
if (
168-
(typeof sampleResults.hits.total === 'object' && sampleResults.hits.total?.value === 0) ||
169-
sampleResults.hits.total === 0 ||
170-
!sampleResults.hits.total
171-
) {
164+
if (sampleResults.hits.hits.length === 0) {
172165
return {
173166
status: 'unknown',
174167
simulationError: null,
@@ -232,16 +225,12 @@ export const schemaFieldsSimulationRoute = createServerRoute({
232225

233226
const runtimeFieldsSearchBody = {
234227
runtime_mappings: propertiesCompatibleWithRuntimeMappings,
235-
sort: [
236-
{
237-
'@timestamp': {
238-
order: 'desc' as const,
239-
},
240-
},
241-
],
242228
size: FIELD_SIMILATION_SAMPLE_SIZE,
243229
fields: params.body.field_definitions.map((field) => field.name),
244230
_source: false,
231+
track_total_hits: false,
232+
terminate_after: FIELD_SIMILATION_SAMPLE_SIZE,
233+
timeout: FIELD_SIMULATION_TIMEOUT,
245234
};
246235

247236
// This gives us a "fields" representation rather than _source from the simulation

0 commit comments

Comments
 (0)