Specialized queries
editSpecialized queries
editThis group contains queries which do not fit into the other groups:
-
more_like_thisquery - This query finds documents which are similar to the specified text, document, or collection of documents.
-
templatequery -
The
templatequery accepts a Mustache template (either inline, indexed, or from a file), and a map of parameters, and combines the two to generate the final query to execute. -
scriptquery -
This query allows a script to act as a filter. Also see the
function_scorequery.
More Like This Query (mlt)
editSee: * More Like This Query
Template Query
editSee Search Template documentation
Define your template parameters as a Map<String,Object>:
Map<String, Object> template_params = new HashMap<>();
template_params.put("param_gender", "male");
You can use your stored search templates in config/scripts.
For example, if you have a file named config/scripts/template_gender.mustache containing:
{
"template" : {
"query" : {
"match" : {
"gender" : "{{param_gender}}"
}
}
}
}
Define your template query:
QueryBuilder qb = templateQuery(
"gender_template",
ScriptService.ScriptType.FILE,
template_params);
You can also store your template in a special index named .scripts:
client.preparePutIndexedScript("mustache", "template_gender",
"{\n" +
" \"template\" : {\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"gender\" : \"{{param_gender}}\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}").get();
To execute an indexed templates, use ScriptService.ScriptType.INDEXED:
Script Query
editSee Script Query
If you have stored on each data node a script named mygroovyscript.groovy with:
doc['num1'].value > param1
You can use it then with:
QueryBuilder qb = scriptQuery(
new Script(
"mygroovyscript",
ScriptService.ScriptType.FILE,
"groovy",
ImmutableMap.of("param1", 5))
);
|
Script name |
|
|
Script type: either |
|
|
Scripting engine |
|
|
Parameters as a |
æ