@@ -164,8 +164,164 @@ include::../mapping/types/numeric.asciidoc[tag=map-ids-as-keyword]
164164[float]
165165=== Avoid scripts
166166
167- In general, scripts should be avoided. If they are absolutely needed, you
168- should prefer the `painless` and `expressions` engines.
167+ 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.
171+
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.
169325
170326[float]
171327=== Search rounded dates
0 commit comments