2015-08-15 12:00:55 -04:00
|
|
|
[[lang-javascript]]
|
|
|
|
=== JavaScript Language Plugin
|
|
|
|
|
2016-08-30 12:06:18 -04:00
|
|
|
deprecated[5.0.0,Javascript will be replaced by the new scripting language <<modules-scripting-painless, `Painless`>>]
|
|
|
|
|
2015-08-15 12:00:55 -04:00
|
|
|
The JavaScript language plugin enables the use of JavaScript in Elasticsearch
|
|
|
|
scripts, via Mozilla's
|
|
|
|
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino[Rhino JavaScript] engine.
|
|
|
|
|
|
|
|
[[lang-javascript-install]]
|
|
|
|
[float]
|
|
|
|
==== Installation
|
|
|
|
|
|
|
|
This plugin can be installed using the plugin manager:
|
|
|
|
|
|
|
|
[source,sh]
|
|
|
|
----------------------------------------------------------------
|
2016-02-04 10:00:55 -05:00
|
|
|
sudo bin/elasticsearch-plugin install lang-javascript
|
2015-08-15 12:00:55 -04:00
|
|
|
----------------------------------------------------------------
|
2016-08-10 18:00:58 -04:00
|
|
|
// NOTCONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
|
|
|
The plugin must be installed on every node in the cluster, and each node must
|
|
|
|
be restarted after installation.
|
|
|
|
|
|
|
|
[[lang-javascript-remove]]
|
|
|
|
[float]
|
|
|
|
==== Removal
|
|
|
|
|
|
|
|
The plugin can be removed with the following command:
|
|
|
|
|
|
|
|
[source,sh]
|
|
|
|
----------------------------------------------------------------
|
2016-02-04 10:00:55 -05:00
|
|
|
sudo bin/elasticsearch-plugin remove lang-javascript
|
2015-08-15 12:00:55 -04:00
|
|
|
----------------------------------------------------------------
|
2016-08-10 18:00:58 -04:00
|
|
|
// NOTCONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
|
|
|
The node must be stopped before removing the plugin.
|
|
|
|
|
|
|
|
[[lang-javascript-usage]]
|
|
|
|
==== Using JavaScript in Elasticsearch
|
|
|
|
|
|
|
|
Once the plugin has been installed, JavaScript can be used at a scripting
|
2016-05-11 10:06:07 -04:00
|
|
|
language by setting the `lang` parameter to `javascript`.
|
2015-08-15 12:00:55 -04:00
|
|
|
|
|
|
|
Scripting is available in many APIs, but we will use an example with the
|
|
|
|
`function_score` for demonstration purposes:
|
|
|
|
|
|
|
|
[[lang-javascript-inline]]
|
|
|
|
[float]
|
|
|
|
=== Inline scripts
|
|
|
|
|
|
|
|
WARNING: Enabling inline scripting on an unprotected Elasticsearch cluster is dangerous.
|
|
|
|
See <<lang-javascript-file>> for a safer option.
|
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[inline scripts],
|
2015-08-15 12:00:55 -04:00
|
|
|
you can use JavaScript as follows:
|
|
|
|
|
2016-05-12 12:43:01 -04:00
|
|
|
[source,js]
|
2015-08-15 12:00:55 -04:00
|
|
|
----
|
|
|
|
PUT test/doc/1
|
|
|
|
{
|
|
|
|
"num": 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
PUT test/doc/2
|
|
|
|
{
|
|
|
|
"num": 2.0
|
|
|
|
}
|
|
|
|
|
|
|
|
GET test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"function_score": {
|
|
|
|
"script_score": {
|
|
|
|
"script": {
|
|
|
|
"inline": "doc[\"num\"].value * factor",
|
|
|
|
"lang": "javascript",
|
|
|
|
"params": {
|
|
|
|
"factor": 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
[[lang-javascript-stored]]
|
2015-08-15 12:00:55 -04:00
|
|
|
[float]
|
2016-05-04 16:38:57 -04:00
|
|
|
=== Stored scripts
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
WARNING: Enabling stored scripts on an unprotected Elasticsearch cluster is dangerous.
|
2015-08-15 12:00:55 -04:00
|
|
|
See <<lang-javascript-file>> for a safer option.
|
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[stored scripts],
|
2015-08-15 12:00:55 -04:00
|
|
|
you can use JavaScript as follows:
|
|
|
|
|
2016-05-12 12:43:01 -04:00
|
|
|
[source,js]
|
2015-08-15 12:00:55 -04:00
|
|
|
----
|
|
|
|
PUT test/doc/1
|
|
|
|
{
|
|
|
|
"num": 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
PUT test/doc/2
|
|
|
|
{
|
|
|
|
"num": 2.0
|
|
|
|
}
|
|
|
|
|
|
|
|
POST _scripts/javascript/my_script <1>
|
|
|
|
{
|
|
|
|
"script": "doc[\"num\"].value * factor"
|
|
|
|
}
|
|
|
|
|
|
|
|
GET test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"function_score": {
|
|
|
|
"script_score": {
|
|
|
|
"script": {
|
|
|
|
"id": "my_script", <2>
|
|
|
|
"lang": "javascript",
|
|
|
|
"params": {
|
|
|
|
"factor": 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
<1> We store the script under the id `my_script`.
|
2015-08-15 12:00:55 -04:00
|
|
|
<2> The function score query retrieves the script with id `my_script`.
|
|
|
|
|
|
|
|
|
|
|
|
[[lang-javascript-file]]
|
|
|
|
[float]
|
|
|
|
=== File scripts
|
|
|
|
|
|
|
|
You can save your scripts to a file in the `config/scripts/` directory on
|
|
|
|
every node. The `.javascript` file suffix identifies the script as containing
|
|
|
|
JavaScript:
|
|
|
|
|
2016-05-13 16:15:51 -04:00
|
|
|
First, save this file as `config/scripts/my_script.js` on every node
|
2015-08-15 12:00:55 -04:00
|
|
|
in the cluster:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
doc["num"].value * factor
|
|
|
|
----
|
2016-08-26 16:18:58 -04:00
|
|
|
// NOTCONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
|
|
|
then use the script as follows:
|
|
|
|
|
2016-05-12 12:43:01 -04:00
|
|
|
[source,js]
|
2015-08-15 12:00:55 -04:00
|
|
|
----
|
|
|
|
PUT test/doc/1
|
|
|
|
{
|
|
|
|
"num": 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
PUT test/doc/2
|
|
|
|
{
|
|
|
|
"num": 2.0
|
|
|
|
}
|
|
|
|
|
|
|
|
GET test/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"function_score": {
|
|
|
|
"script_score": {
|
|
|
|
"script": {
|
|
|
|
"file": "my_script", <1>
|
|
|
|
"lang": "javascript",
|
|
|
|
"params": {
|
|
|
|
"factor": 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
|
|
|
<1> The function score query retrieves the script with filename `my_script.javascript`.
|