Specialized queries

This group contains queries which do not fit into the other groups:

more_like_this query
This query finds documents which are similar to the specified text, document, or collection of documents.
template query
The template query 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.
script query
This query allows a script to act as a filter. Also see the function_score query.

More Like This Query (mlt)

See: * More Like This Query

QueryBuilder qb = moreLikeThisQuery("name.first", "name.last") 
    .like("text like this one")                                



ignore threshold

max num of Terms in generated queries

Template Query

See 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(

template name

template stored on disk in gender_template.mustache


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" +

To execute an indexed templates, use ScriptService.ScriptType.INDEXED:

QueryBuilder qb = templateQuery(

template name

template stored in an index


Script Query

See Script Query

QueryBuilder qb = scriptQuery(
    new Script("doc['num1'].value > 1") 

inlined script

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(
        ImmutableMap.of("param1", 5))        

Script name

Script type: either ScriptType.FILE, ScriptType.INLINE or ScriptType.INDEXED

Scripting engine

Parameters as a Map of <String, Object>