Specialized queries

edit

Specialized queries

edit

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)

edit

See: * More Like This Query

QueryBuilder qb = moreLikeThisQuery("name.first", "name.last") 
    .like("text like this one")                                
    .minTermFreq(1)                                            
    .maxQueryTerms(12);                                        

fields

text

ignore threshold

max num of Terms in generated queries

Template Query

edit

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(
    "gender_template",                  
    ScriptService.ScriptType.FILE,      
    template_params);                   

template name

template stored on disk in gender_template.mustache

parameters

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:

QueryBuilder qb = templateQuery(
    "gender_template",                  
    ScriptService.ScriptType.INDEXED,   
    template_params);                   

template name

template stored in an index

parameters

Script Query

edit

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(
        "mygroovyscript",                    
        ScriptService.ScriptType.FILE,       
        "groovy",                            
        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>

æ