Painless Context Doc: Add filter context example (#35305)
This commit is contained in:
parent
f7ada9b29b
commit
501c03e529
|
@ -23,4 +23,43 @@ query to include and exclude documents.
|
|||
|
||||
*API*
|
||||
|
||||
The standard <<painless-api-reference, Painless API>> is available.
|
||||
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]
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue