mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-04 01:49:15 +00:00
Moves the following API sections under the REST APIs navigations: - API Conventions - Document APIs - Search APIs - Index APIs (previously named Indices APIs) - cat APIs - Cluster APIs Other supporting changes: - Removes the previous index APIs page under REST APIs. Adds a redirect for the removed page. - Removes several [partintro] macros so the docs build correctly. - Changes anchors for pages that become sections of a parent page. - Adds several redirects for existing pages that become sections of a parent page. This commit re-applies changes from #44238. Changes from that PR were reverted due to broken links in several repos. This commit adds redirects for those broken links.
74 lines
2.2 KiB
Plaintext
74 lines
2.2 KiB
Plaintext
[[request-body-search-script-fields]]
|
|
=== Script Fields
|
|
|
|
Allows to return a <<modules-scripting,script
|
|
evaluation>> (based on different fields) for each hit, for example:
|
|
|
|
[source,js]
|
|
--------------------------------------------------
|
|
GET /_search
|
|
{
|
|
"query" : {
|
|
"match_all": {}
|
|
},
|
|
"script_fields" : {
|
|
"test1" : {
|
|
"script" : {
|
|
"lang": "painless",
|
|
"source": "doc['price'].value * 2"
|
|
}
|
|
},
|
|
"test2" : {
|
|
"script" : {
|
|
"lang": "painless",
|
|
"source": "doc['price'].value * params.factor",
|
|
"params" : {
|
|
"factor" : 2.0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
--------------------------------------------------
|
|
// CONSOLE
|
|
// TEST[setup:sales]
|
|
|
|
Script fields can work on fields that are not stored (`price` in
|
|
the above case), and allow to return custom values to be returned (the
|
|
evaluated value of the script).
|
|
|
|
Script fields can also access the actual `_source` document and
|
|
extract specific elements to be returned from it by using `params['_source']`.
|
|
Here is an example:
|
|
|
|
[source,js]
|
|
--------------------------------------------------
|
|
GET /_search
|
|
{
|
|
"query" : {
|
|
"match_all": {}
|
|
},
|
|
"script_fields" : {
|
|
"test1" : {
|
|
"script" : "params['_source']['message']"
|
|
}
|
|
}
|
|
}
|
|
--------------------------------------------------
|
|
// CONSOLE
|
|
// TEST[setup:twitter]
|
|
|
|
Note the `_source` keyword here to navigate the json-like model.
|
|
|
|
It's important to understand the difference between
|
|
`doc['my_field'].value` and `params['_source']['my_field']`. The first,
|
|
using the doc keyword, will cause the terms for that field to be loaded to
|
|
memory (cached), which will result in faster execution, but more memory
|
|
consumption. Also, the `doc[...]` notation only allows for simple valued
|
|
fields (you can't return a json object from it) and makes sense only for
|
|
non-analyzed or single term based fields. However, using `doc` is
|
|
still the recommended way to access values from the document, if at all
|
|
possible, because `_source` must be loaded and parsed every time it's used.
|
|
Using `_source` is very slow.
|
|
|