Compound queries

edit

Compound queries

edit

Compound queries wrap other compound or leaf queries, either to combine their results and scores, to change their behaviour, or to switch from query to filter context.

The queries in this group are:

constant_score query
A query which wraps another query, but executes it in filter context. All matching documents are given the same “constant” _score.
bool query
The default query for combining multiple leaf or compound query clauses, as must, should, must_not, or filter clauses. The must and should clauses have their scores combined — the more matching clauses, the better — while the must_not and filter clauses are executed in filter context.
dis_max query
A query which accepts multiple queries, and returns any documents which match any of the query clauses. While the bool query combines the scores from all matching queries, the dis_max query uses the score of the single best- matching query clause.
function_score query
Modify the scores returned by the main query with functions to take into account factors like popularity, recency, distance, or custom algorithms implemented with scripting.
boosting query
Return documents which match a positive query, but reduce the score of documents which also match a negative query.
indices query
Execute one query for the specified indices, and another for other indices.
and, or, not
Synonyms for the bool query.
filtered query
Combine a query clause in query context with another in filter context. [2.0.0] Deprecated in 2.0.0. Use the bool query instead
limit query
Limits the number of documents examined per shard.

Constant Score Query

edit

See Constant Score Query

QueryBuilder qb = constantScoreQuery(
        termQuery("name","kimchy")      
    )
    .boost(2.0f);                       

your query

query score

Bool Query

edit

See Bool Query

QueryBuilder qb = boolQuery()
    .must(termQuery("content", "test1"))    
    .must(termQuery("content", "test4"))    
    .mustNot(termQuery("content", "test2")) 
    .should(termQuery("content", "test3")); 

must query

must not query

should query

Dis Max Query

edit

See Dis Max Query

QueryBuilder qb = disMaxQuery()
    .add(termQuery("name", "kimchy"))        
    .add(termQuery("name", "elasticsearch")) 
    .boost(1.2f)                             
    .tieBreaker(0.7f);                       

add your queries

add your queries

boost factor

tie breaker

Function Score Query

edit

See Function Score Query.

To use ScoreFunctionBuilders just import them in your class:

import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.*;
QueryBuilder qb = functionScoreQuery()
    .add(
        matchQuery("name", "kimchy"),             
        randomFunction("ABCDEF")                  
    )
    .add(
        exponentialDecayFunction("age", 0L, 1L)   
    );

Add a first function based on a query

And randomize the score based on a given seed

Add another function based on the age field

Boosting Query

edit

See Boosting Query

QueryBuilder qb = boostingQuery()
    .positive(termQuery("name","kimchy"))   
    .negative(termQuery("name","dadoonet")) 
    .negativeBoost(0.2f);                   

query that will promote documents

query that will demote documents

negative boost

Indices Query

edit

See Indices Query

// Using another query when no match for the main one
QueryBuilder qb = indicesQuery(
        termQuery("tag", "wow"),             
        "index1", "index2"                   
    ).noMatchQuery(termQuery("tag", "kow")); 

query to be executed on selected indices

selected indices

query to be executed on non matching indices

// Using all (match all) or none (match no documents)
QueryBuilder qb = indicesQuery(
        termQuery("tag", "wow"),            
        "index1", "index2"                  
    ).noMatchQuery("all");                  

query to be executed on selected indices

selected indices

none (to match no documents), and all (to match all documents). Defaults to all.

And Query

edit

Deprecated in 2.0.0.

Use the bool query instead

See And Query

QueryBuilder query = andQuery(
    rangeQuery("postDate").from("2010-03-01").to("2010-04-01"),    
    prefixQuery("name.second", "ba"));                             

queries

Not Query

edit

See Not Query

QueryBuilder qb = notQuery(
    rangeQuery("price").from("1").to("2")  
);

query

Or Query

edit

Deprecated in 2.0.0.

Use the bool query instead

See Or Query

QueryBuilder qb = orQuery(
    rangeQuery("price").from(1).to(2),  
    matchQuery("name", "joe")           
);

queries

Filtered Query

edit

Deprecated in 2.0.0.

Use the bool query instead with a must clause for the query and a filter clause for the filter

See Filtered Query.

QueryBuilder qb = filteredQuery(
        matchQuery("name", "kimchy"),                       
        rangeQuery("dateOfBirth").from("1900").to("2100")   
);

query which will be used for scoring

query which will only be used for filtering the result set

Limit Query

edit

See Limit Query

QueryBuilder qb = limitQuery(100);   

number of documents per shard