Painless Context Doc: Add filter context example (#35305)

This commit is contained in:
Mayya Sharipova 2018-11-15 14:33:34 -05:00 committed by GitHub
parent f7ada9b29b
commit 501c03e529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 1 deletions

View File

@ -24,3 +24,42 @@ query to include and exclude documents.
*API*
The standard <<painless-api-reference, Painless API>> is available.
*Example*
To run this example, first follow the steps in
<<painless-context-examples, context examples>>.
This script finds all unsold documents that cost less than $18.
[source,Painless]
----
doc['sold'].value == false && doc['cost'].value < 18
----
Defining cost as a script parameter enables the cost to be configured
in the script query request. For example, the following request finds
all available theatre seats for evening performances that are under $18.
[source,js]
----
GET evening/_search
{
"query": {
"bool" : {
"filter" : {
"script" : {
"script" : {
"source" : "doc['sold'].value == false && doc['cost'].value < params.cost",
"params" : {
"cost" : 18
}
}
}
}
}
}
}
----
// CONSOLE
// TEST[skip: requires setup from other pages]

View File

@ -1,3 +1,4 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
@ -359,5 +360,41 @@ public class ContextExampleTests extends ScriptTestCase {
singletonMap("_source", source), true)
);
}
// Use script query request to filter documents
/*
GET localhost:9200/evening/_search
{
"query": {
"bool" : {
"filter" : {
"script" : {
"script" : {
"source" : "doc['sold'].value == false && doc['cost'].value < params.cost",
"params" : {
"cost" : 18
}
}
}
}
}
}
}
*/
public void testFilterScript() {
Map<String, Object> source = new HashMap<>();
source.put("sold", false);
source.put("cost", 15);
Map<String, Object> params = new HashMap<>();
params.put("_source", source);
params.put("cost", 18);
boolean result = (boolean) exec(
" params['_source']['sold'] == false && params['_source']['cost'] < params.cost;",
params, true);
assertTrue(result);
}
}