Skip to content

Commit 69a8035

Browse files
committed
Fix example in documentation for Painless using _source. (#21322)
1 parent c6c7e7c commit 69a8035

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

docs/reference/search/request/script-fields.asciidoc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ Script fields can work on fields that are not stored (`my_field_name` in
3737
the above case), and allow to return custom values to be returned (the
3838
evaluated value of the script).
3939

40-
Script fields can also access the actual `_source` document indexed and
41-
extract specific elements to be returned from it (can be an "object"
42-
type). Here is an example:
40+
Script fields can also access the actual `_source` document and
41+
extract specific elements to be returned from it by using `params['_source']`.
42+
Here is an example:
4343

4444
[source,js]
4545
--------------------------------------------------
@@ -50,22 +50,24 @@ GET /_search
5050
},
5151
"script_fields" : {
5252
"test1" : {
53-
"script" : "_source.obj1.obj2"
53+
"script" : "params['_source']['message']"
5454
}
5555
}
5656
}
5757
--------------------------------------------------
5858
// CONSOLE
59+
// TEST[setup:twitter]
5960

6061
Note the `_source` keyword here to navigate the json-like model.
6162

6263
It's important to understand the difference between
63-
`doc['my_field'].value` and `_source.my_field`. The first, using the doc
64-
keyword, will cause the terms for that field to be loaded to memory
65-
(cached), which will result in faster execution, but more memory
64+
`doc['my_field'].value` and `params['_source']['my_field']`. The first,
65+
using the doc keyword, will cause the terms for that field to be loaded to
66+
memory (cached), which will result in faster execution, but more memory
6667
consumption. Also, the `doc[...]` notation only allows for simple valued
6768
fields (can't return a json object from it) and make sense only on
68-
non-analyzed or single term based fields.
69+
non-analyzed or single term based fields. However, using `doc` is
70+
still the recommended way to access values from the document, if at all
71+
possible, because `_source` must be loaded and parsed every time it's used.
72+
Using `_source` is very slow.
6973

70-
The `_source` on the other hand causes the source to be loaded, parsed,
71-
and then only the relevant part of the json is returned.

0 commit comments

Comments
 (0)