How to do a "join"/"sub-query" using ElasticSearch? How to do a "join"/"sub-query" using ElasticSearch? elasticsearch elasticsearch

How to do a "join"/"sub-query" using ElasticSearch?


I faced with the similar problem. You need a join here. Elasticsearch Team advises to use application side. ES emulates a relational database by implementing joins in our application: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/application-joins.htmlSo you can use any of the ES client and write something similar

    SearchResponse response = client.prepareSearch(scriptVersionFirst)                   .setTypes("yourtype")                   .setQuery(QueryBuilders.termQuery("multi", "test"))                   .setFrom(0).setSize(60).setExplain(true)                   .execute()                   .actionGet();    if (response != null) {        SearchHits hitList = response.getHits();        if (hitList != null) {            SearchHit[] hits = hitList.hits();                      for (SearchHit hit : hits)                 MethodPojo source = gson.fromJson(hit.getSourceAsString(), MethodPojo.class);                System.out.println("Found: " + getMethodResultEntity(scriptVersionSecond, hit.getType(), source.getMethodName(), source.getMethodDesc(), source.getRequest()));            }        }

Here function getMethodResultEntity return result of other get query.

P.S. Im using here ES java client. It's a little bit heavy. If you need java client it's better to use jest client - much light version for simple situation.