Specialized queriesedit

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.
script query
This query allows a script to act as a filter. Also see the function_score query.
percolate query
This query finds percolator queries based on documents.
wrapper query
A query that accepts other queries as json or yaml string.

More Like This Queryedit

See More Like This Query

String[] fields = {"name.first", "name.last"};               
String[] texts = {"text like this one"};                     

moreLikeThisQuery(fields, texts, null)



ignore threshold

max num of Terms in generated queries

Script Queryedit

See Script Query

        new Script("doc['num1'].value > 1")                  

inlined script

If you have stored on each data node a script named myscript.painless with:

doc['num1'].value > params.param1

You can use it then with:

Map<String, Object> parameters = new HashMap<>();
parameters.put("param1", 5);
scriptQuery(new Script(
        singletonMap("param1", 5)));                         

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

Scripting engine

Script name

Parameters as a Map<String, Object>

Percolate Queryedit

See: * Percolate Query

Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(new InetSocketAddress(InetAddresses.forString(""), 9300)));

Before the percolate query can be used an percolator mapping should be added and a document containing a percolator query should be indexed:

// create an index with a percolator field with the name 'query':
                        .addMapping("_doc", "query", "type=percolator", "content", "type=text")

//This is the query we're registering in the percolator
QueryBuilder qb = termQuery("content", "amazing");

//Index the query = register it in the percolator
client.prepareIndex("myIndexName", "_doc", "myDesignatedQueryName")
            .field("query", qb) // Register the query
    .setRefreshPolicy(RefreshPolicy.IMMEDIATE) // Needed when the query shall be available immediately

This indexes the above term query under the name myDesignatedQueryName.

In order to check a document against the registered queries, use this code:

//Build a document to check against the percolator
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
docBuilder.field("content", "This is amazing!");
docBuilder.endObject(); //End of the JSON root object

PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder("query", "_doc", BytesReference.bytes(docBuilder));

// Percolate, by executing the percolator query in the query dsl:
SearchResponse response = client().prepareSearch("myIndexName")
//Iterate over the results
for(SearchHit hit : response.getHits()) {
    // Percolator queries as hit

Wrapper Queryedit

See Wrapper Query

String query = "{\"term\": {\"user\": \"kimchy\"}}"; 

query defined as query builder