Skip to content

Commit dca8fa0

Browse files
authored
[DOCS] Add performance warning for scripts (#59890) (#59915)
1 parent 2c64a9a commit dca8fa0

File tree

6 files changed

+175
-156
lines changed

6 files changed

+175
-156
lines changed

docs/painless/painless-lang-spec/painless-regexes.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ are always constants and compiled efficiently a single time.
1010
Pattern p = /[aeiou]/
1111
---------------------------------------------------------
1212

13+
WARNING: A poorly written regular expression can significantly slow performance.
14+
If possible, avoid using regular expressions, particularly in frequently run
15+
scripts.
16+
1317
[[pattern-flags]]
1418
==== Pattern flags
1519

docs/reference/aggregations/metrics/scripted-metric-aggregation.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
A metric aggregation that executes using scripts to provide a metric output.
55

6+
WARNING: Using scripts can result in slower search speeds. See
7+
<<scripts-and-search-speed>>.
8+
69
Example:
710

811
[source,console]

docs/reference/aggregations/metrics/string-stats-aggregation.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
A `multi-value` metrics aggregation that computes statistics over string values extracted from the aggregated documents.
77
These values can be retrieved either from specific `keyword` fields in the documents or can be generated by a provided script.
88

9+
WARNING: Using scripts can result in slower search speeds. See
10+
<<scripts-and-search-speed>>.
11+
912
The string stats aggregation returns the following results:
1013

1114
* `count` - The number of non-empty fields counted.

docs/reference/how-to/search-speed.asciidoc

Lines changed: 2 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -165,163 +165,9 @@ include::../mapping/types/numeric.asciidoc[tag=map-ids-as-keyword]
165165
=== Avoid scripts
166166

167167
If possible, avoid using <<modules-scripting,scripts>> or
168-
<<request-body-search-script-fields,scripted fields>> in searches. Because
169-
scripts can't make use of index structures, using scripts in search queries can
170-
result in slower search speeds.
168+
<<request-body-search-script-fields,scripted fields>> in searches. See
169+
<<scripts-and-search-speed>>.
171170

172-
If you often use scripts to transform indexed data, you can speed up search by
173-
making these changes during ingest instead. However, that often means slower
174-
index speeds.
175-
176-
.*Example*
177-
[%collapsible]
178-
====
179-
An index, `my_test_scores`, contains two `long` fields:
180-
181-
* `math_score`
182-
* `verbal_score`
183-
184-
When running searches, users often use a script to sort results by the sum of
185-
these two field's values.
186-
187-
[source,console]
188-
----
189-
GET /my_test_scores/_search
190-
{
191-
"query": {
192-
"term": {
193-
"grad_year": "2020"
194-
}
195-
},
196-
"sort": [
197-
{
198-
"_script": {
199-
"type": "number",
200-
"script": {
201-
"source": "doc['math_score'].value + doc['verbal_score'].value"
202-
},
203-
"order": "desc"
204-
}
205-
}
206-
]
207-
}
208-
----
209-
// TEST[s/^/PUT my_test_scores\n/]
210-
211-
To speed up search, you can perform this calculation during ingest and index the
212-
sum to a field instead.
213-
214-
First, <<indices-put-mapping,add a new field>>, `total_score`, to the index. The
215-
`total_score` field will contain sum of the `math_score` and `verbal_score`
216-
field values.
217-
218-
[source,console]
219-
----
220-
PUT /my_test_scores/_mapping
221-
{
222-
"properties": {
223-
"total_score": {
224-
"type": "long"
225-
}
226-
}
227-
}
228-
----
229-
// TEST[continued]
230-
231-
Next, use an <<ingest,ingest pipeline>> containing the
232-
<<script-processor,`script`>> processor to calculate the sum of `math_score` and
233-
`verbal_score` and index it in the `total_score` field.
234-
235-
[source,console]
236-
----
237-
PUT _ingest/pipeline/my_test_scores_pipeline
238-
{
239-
"description": "Calculates the total test score",
240-
"processors": [
241-
{
242-
"script": {
243-
"source": "ctx.total_score = (ctx.math_score + ctx.verbal_score)"
244-
}
245-
}
246-
]
247-
}
248-
----
249-
// TEST[continued]
250-
251-
To update existing data, use this pipeline to <<docs-reindex,reindex>> any
252-
documents from `my_test_scores` to a new index, `my_test_scores_2`.
253-
254-
[source,console]
255-
----
256-
POST /_reindex
257-
{
258-
"source": {
259-
"index": "my_test_scores"
260-
},
261-
"dest": {
262-
"index": "my_test_scores_2",
263-
"pipeline": "my_test_scores_pipeline"
264-
}
265-
}
266-
----
267-
// TEST[continued]
268-
269-
Continue using the pipeline to index any new documents to `my_test_scores_2`.
270-
271-
[source,console]
272-
----
273-
POST /my_test_scores_2/_doc/?pipeline=my_test_scores_pipeline
274-
{
275-
"student": "kimchy",
276-
"grad_year": "2020",
277-
"math_score": 800,
278-
"verbal_score": 800
279-
}
280-
----
281-
// TEST[continued]
282-
283-
These changes may slow indexing but allow for faster searches. Users can now
284-
sort searches made on `my_test_scores_2` using the `total_score` field instead
285-
of using a script.
286-
287-
[source,console]
288-
----
289-
GET /my_test_scores_2/_search
290-
{
291-
"query": {
292-
"term": {
293-
"grad_year": "2020"
294-
}
295-
},
296-
"sort": [
297-
{
298-
"total_score": {
299-
"order": "desc"
300-
}
301-
}
302-
]
303-
}
304-
----
305-
// TEST[continued]
306-
307-
////
308-
[source,console]
309-
----
310-
DELETE /_ingest/pipeline/my_test_scores_pipeline
311-
----
312-
// TEST[continued]
313-
314-
[source,console-result]
315-
----
316-
{
317-
"acknowledged": true
318-
}
319-
----
320-
////
321-
====
322-
323-
We recommend testing and benchmarking any indexing changes before deploying them
324-
in production.
325171

326172
[float]
327173
=== Search rounded dates

docs/reference/query-dsl/script-query.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
Filters documents based on a provided <<modules-scripting-using,script>>. The
88
`script` query is typically used in a <<query-filter-context,filter context>>.
99

10+
WARNING: Using scripts can result in slower search speeds. See
11+
<<scripts-and-search-speed>>.
12+
1013

1114
[[script-query-ex-request]]
1215
==== Example request

docs/reference/scripting/using.asciidoc

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,166 @@ changed by setting `script.max_size_in_bytes` setting to increase that soft
250250
limit, but if scripts are really large then a
251251
<<modules-scripting-engine,native script engine>> should be considered.
252252

253+
[[scripts-and-search-speed]]
254+
=== Scripts and search speed
255+
256+
Scripts can't make use of {es}'s index structures or related optimizations. This
257+
can sometimes result in slower search speeds.
258+
259+
If you often use scripts to transform indexed data, you can speed up search by
260+
making these changes during ingest instead. However, that often means slower
261+
index speeds.
262+
263+
.*Example*
264+
[%collapsible]
265+
=====
266+
An index, `my_test_scores`, contains two `long` fields:
267+
268+
* `math_score`
269+
* `verbal_score`
270+
271+
When running searches, users often use a script to sort results by the sum of
272+
these two field's values.
273+
274+
[source,console]
275+
----
276+
GET /my_test_scores/_search
277+
{
278+
"query": {
279+
"term": {
280+
"grad_year": "2099"
281+
}
282+
},
283+
"sort": [
284+
{
285+
"_script": {
286+
"type": "number",
287+
"script": {
288+
"source": "doc['math_score'].value + doc['verbal_score'].value"
289+
},
290+
"order": "desc"
291+
}
292+
}
293+
]
294+
}
295+
----
296+
// TEST[s/^/PUT my_test_scores\n/]
297+
298+
To speed up search, you can perform this calculation during ingest and index the
299+
sum to a field instead.
300+
301+
First, <<indices-put-mapping,add a new field>>, `total_score`, to the index. The
302+
`total_score` field will contain sum of the `math_score` and `verbal_score`
303+
field values.
304+
305+
[source,console]
306+
----
307+
PUT /my_test_scores/_mapping
308+
{
309+
"properties": {
310+
"total_score": {
311+
"type": "long"
312+
}
313+
}
314+
}
315+
----
316+
// TEST[continued]
317+
318+
Next, use an <<ingest,ingest pipeline>> containing the
319+
<<script-processor,`script`>> processor to calculate the sum of `math_score` and
320+
`verbal_score` and index it in the `total_score` field.
321+
322+
[source,console]
323+
----
324+
PUT _ingest/pipeline/my_test_scores_pipeline
325+
{
326+
"description": "Calculates the total test score",
327+
"processors": [
328+
{
329+
"script": {
330+
"source": "ctx.total_score = (ctx.math_score + ctx.verbal_score)"
331+
}
332+
}
333+
]
334+
}
335+
----
336+
// TEST[continued]
337+
338+
To update existing data, use this pipeline to <<docs-reindex,reindex>> any
339+
documents from `my_test_scores` to a new index, `my_test_scores_2`.
340+
341+
[source,console]
342+
----
343+
POST /_reindex
344+
{
345+
"source": {
346+
"index": "my_test_scores"
347+
},
348+
"dest": {
349+
"index": "my_test_scores_2",
350+
"pipeline": "my_test_scores_pipeline"
351+
}
352+
}
353+
----
354+
// TEST[continued]
355+
356+
Continue using the pipeline to index any new documents to `my_test_scores_2`.
357+
358+
[source,console]
359+
----
360+
POST /my_test_scores_2/_doc/?pipeline=my_test_scores_pipeline
361+
{
362+
"student": "kimchy",
363+
"grad_year": "2099",
364+
"math_score": 800,
365+
"verbal_score": 800
366+
}
367+
----
368+
// TEST[continued]
369+
370+
These changes may slow indexing but allow for faster searches. Users can now
371+
sort searches made on `my_test_scores_2` using the `total_score` field instead
372+
of using a script.
373+
374+
[source,console]
375+
----
376+
GET /my_test_scores_2/_search
377+
{
378+
"query": {
379+
"term": {
380+
"grad_year": "2099"
381+
}
382+
},
383+
"sort": [
384+
{
385+
"total_score": {
386+
"order": "desc"
387+
}
388+
}
389+
]
390+
}
391+
----
392+
// TEST[continued]
393+
394+
////
395+
[source,console]
396+
----
397+
DELETE /_ingest/pipeline/my_test_scores_pipeline
398+
----
399+
// TEST[continued]
400+
401+
[source,console-result]
402+
----
403+
{
404+
"acknowledged": true
405+
}
406+
----
407+
////
408+
=====
409+
410+
We recommend testing and benchmarking any indexing changes before deploying them
411+
in production.
412+
253413
[float]
254414
[[modules-scripting-errors]]
255415
=== Script errors

0 commit comments

Comments
 (0)