# JSON Query via REST from Java client

**URL:** <https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760>\
**Category:** Elasticsearch\
**Created:** [July 19, 2017, 12:43pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760 "2017-07-19T12:43:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 19, 2017, 12:43pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760/1 "2017-07-19T12:43:44Z")

</div>

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"  
}  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 19, 2017, 1:57pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760/2 "2017-07-19T13:57:49Z")

</div>

```
{
   ""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"
         }
      }
   ]
}
```

---

<div class="post-metadata">

**Author:** ![iam.xtreme](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iam.xtreme/32/20143_2.png) [@iam.xtreme](https://discuss.elastic.co/u/iam.xtreme)\
**Post date:** [July 19, 2017, 4:48pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760/3 "2017-07-19T16:48:12Z")

</div>

You can refer [Elastic Java Rest](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_example_requests.html) for reference.  
OR  
You can also create a simple Rest Client using Java. Refer [Rest Client using Java](https://www.javacodegeeks.com/2012/09/simple-rest-client-in-java.html) 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](http://yourhostname:9200/yourindextoquery/_search)** and the body would contain the query to execute.

Hope this helps.  
Thank you.

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 19, 2017, 7:10pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760/4 "2017-07-19T19:10:20Z")

</div>

Thank you Tushar, will give it a try.

Thanks

Velusamy K. Velu  
[vvelu@peruselab.com](mailto:vvelu@peruselab.com)  
[http://peruselab.com](http://peruselab.com)  
_614-321-9649_

[http://www.linkedin.com/in/vkvelu](http://www.linkedin.com/in/vkvelu)

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 26, 2017, 7:22pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760/5 "2017-07-26T19:22:21Z")

</div>

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 (...) {
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 23, 2017, 7:22pm UTC](https://discuss.elastic.co/t/json-query-via-rest-from-java-client/93760/6 "2017-08-23T19:22:25Z")

</div>

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