JSON Query via REST from Java client

Friends:

I want to know the way to send a query in JSON format to ES from Java via a REST API. For example one of my query is given below the signature line. I want to construct an end point in my Java client like this:
http://:9200//indexType> -q {"from":0,"size":1000,"query":{"bool":{"must":[..]...and so on..}
I have no problem with my Java. I just don't know the valid way of sending a JSON query to ES.

I want to avoid using org.elasticsearch.client.Client or org.elasticsearch.client.transport.TransportClient.

Please help me.

Thank you
Velu

{
""from": 0,
"size": 1000,
"query": {
"bool": {
"must": [
{
"range": {
"edgeCount": {
"from": 1,
"to": 101,
"include_lower": true,
"include_upper": true
}
}
},
{
"range": {
"degreesCountMap.ONE": {
"from": 2,
"to": 102,
"include_lower": true,
"include_upper": true
}
}
},
{
"range": {
"nodeCount": {
"from": 2,
"to": 102,
"include_lower": true,
"include_upper": true
}
}
}
]
}
},
"fields": [
"id",
"primaryLId",
"primaryMId",
"nodeCount"
],
"sort": [
{
"nodeCount": {
"order": "asc",
"missing": "_last"
}
}
]
}

{
   ""from": 0,
   "size": 1000,
   "query": {
      "bool": {
         "must": [
            {
               "range": {
                  "edgeCount": {
                     "from": 1,
                     "to": 101,
                     "include_lower": true,
                     "include_upper": true
                  }
               }
            },
            {
               "range": {
                  "degreesCountMap.ONE": {
                     "from": 2,
                     "to": 102,
                     "include_lower": true,
                     "include_upper": true
                  }
               }
            },
            {
               "range": {
                  "nodeCount": {
                     "from": 2,
                     "to": 102,
                     "include_lower": true,
                     "include_upper": true
                  }
               }
            }
         ]
      }
   },
   "fields": [
      "id",
      "primaryLId",
      "primaryMId",
      "nodeCount"
   ],
   "sort": [
      {
         "nodeCount": {
            "order": "asc",
            "missing": "_last"
         }
      }
   ]
}

You can refer Elastic Java Rest for reference.
OR
You can also create a simple Rest Client using Java. Refer Rest Client using Java for code. To fetch data from elasticsearch you would need to make a POST Request to your respective elasctic index. Inclued your JSON query in the request body. e.g. your URL for HTTP Post request would be: http://yourhostname:9200/yourindextoquery/_search and the body would contain the query to execute.

Hope this helps.
Thank you.

Thank you Tushar, will give it a try.

Thanks

Velusamy K. Velu
vvelu@peruselab.com
http://peruselab.com
614-321-9649

http://www.linkedin.com/in/vkvelu

Tushar:

RestClient kept having some issues, probably it was still my problems but I became impatient with it. However, Jersey REST client worked well. The key was in having the keyword _search in the end point and wrapping the query in the Entity class. Here's the code snippet:

   String endPoint = server + indexName + "/" + indexType + "/_search";

    WebTarget target = buildTarget(endPoint);
    try {
        json = target.request().accept(JSON).post(Entity.json(json), String.class);
    } catch (...) {

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.