Saturday, December 13, 2014

ElasticSearch: Filtered Query using JEST

While it is possible to query ElasticSearch using httpclient or es node, it is not as effective as JEST. This post explains the basics of using JEST for filtered query against ElasticSearch. To start, create Maven project and add the following dependencies into the pom file:


Suppose the elastic search stores indexed document with the following example mapping:

{ "name" : "xxx",
  "age" : 11,
"address" : "xxxxx"}

Let's suppose the elasticsearch is running at and the indexed documents are stored under

Let's create a simple java class representing this index document:

public class SearchResultTuple {
 public String address;
 public String name;
 public int age;

We want to retrieves 20 records of the indexed documents from the elasticsearch matching using the following query:

{"from": 0, "size" : 20,
"sort" : {
   "age" : { "order" : "asc" }
"query" : {
  "filtered" : {
     "query" : {
        "match" : { "name" : "James" }

    "filter" : {
       "range" : { "age" : { "lte" : 10, "gte" : 20 } }

The java implementation to execute the above filtered query using JEST on ElasticSearch is shown below:

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import org.json.simple.JSONObject;

public class App 
    public static void main( String[] args ) 
        JestClient client = openClient();
        JSONObject json=new JSONObject();
        json.put("from", 0);
        json.put("size", 20);
        JSONObject sortJson=new JSONObject();
        json.put("sort", sortJson);
        JSONObject sortDateJson=new JSONObject();
        sortJson.put("age", sortDateJson);
        sortDateJson.put("order", "asc");
        JSONObject queryJson=new JSONObject();
        json.put("query", queryJson);
        JSONObject filteredJson=new JSONObject();
        queryJson.put("filtered", filteredJson);

        JSONObject queryMatchJson=new JSONObject();
        filteredJson.put("query", queryMatchJson);
        JSONObject matchJson=new JSONObject();
        queryMatchJson.put("match", matchJson);
        matchJson.put("name", "James");
        JSONObject filterJson=new JSONObject();
        filteredJson.put("filter", filterJson);
        JSONObject rangeJson=new JSONObject();
        filterJson.put("range", rangeJson);

        JSONObject dateJson = new JSONObject();

        rangeJson.put("age", dateJson);

        dateJson.put("gte", 20);
        dateJson.put("lte", 10);
        String jsonString = json.toJSONString();
        Search search = (Search) new Search.Builder(jsonString)

        try {
           SearchResult result = client.execute(search);
           List<Hit<ElasticSearchResultTuple, Void>> hits = result.getHits(SearchResultTuple.class);
          for(Hit<SearchResultTuple, Void> hit : hits)
            SearchResultTuple hitTuple = hit.source;
            int age = hitTuple.age;
            String name =;
            String address =hitTuple.address;
        } catch (Exception e) {
         // TODO Auto-generated catch block
    private static JestClient openClient()
     HttpClientConfig clientConfig = new HttpClientConfig.Builder("")
     JestClientFactory factory = new JestClientFactory();
     JestClient jestClient = factory.getObject();
     return jestClient;

No comments:

Post a Comment