Joining queries

edit

Joining queries

edit

Performing full SQL-style joins in a distributed system like Elasticsearch is prohibitively expensive. Instead, Elasticsearch offers two forms of join which are designed to scale horizontally.

nested query
Documents may contains fields of type nested. These fields are used to index arrays of objects, where each object can be queried (with the nested query) as an independent document.
has_child and has_parent queries
A parent-child relationship can exist between two document types within a single index. The has_child query returns parent documents whose child documents match the specified query, while the has_parent query returns child documents whose parent document matches the specified query.

Nested Query

edit

See Nested Query

QueryBuilder qb = nestedQuery(
        "obj1",                       
        boolQuery()                   
                .must(matchQuery("obj1.name", "blue"))
                .must(rangeQuery("obj1.count").gt(5)),
        ScoreMode.Avg                 
    );

path to nested document

your query. Any fields referenced inside the query must use the complete path (fully qualified).

score mode could be ScoreMode.Max, ScoreMode.Min, ScoreMode.Total, ScoreMode.Avg or ScoreMode.None

Has Child Query

edit

See Has Child Query

When using the has_child query it is important to use the PreBuiltTransportClient instead of the regular client:

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

Otherwise the parent-join module doesn’t get loaded and the has_child query can’t be used from the transport client.

QueryBuilder qb = JoinQueryBuilders.hasChildQuery(
    "blog_tag",                     
    termQuery("tag","something"),   
    ScoreMode.Avg                   
);

child type to query against

query

score mode can be ScoreMode.Avg, ScoreMode.Max, ScoreMode.Min, ScoreMode.None or ScoreMode.Total

Has Parent Query

edit

See Has Parent

When using the has_parent query it is important to use the PreBuiltTransportClient instead of the regular client:

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

Otherwise the parent-join module doesn’t get loaded and the has_parent query can’t be used from the transport client.

QueryBuilder qb = JoinQueryBuilders.hasParentQuery(
    "blog",                         
    termQuery("tag","something"),   
    false                           
);

parent type to query against

query

whether the score from the parent hit should propagate to the child hit