# How to search with full Json query using Java API

**URL:** <https://discuss.elastic.co/t/how-to-search-with-full-json-query-using-java-api/100628>\
**Category:** Elasticsearch\
**Created:** [September 15, 2017, 3:44am UTC](https://discuss.elastic.co/t/how-to-search-with-full-json-query-using-java-api/100628 "2017-09-15T03:44:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AntonQUT](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@AntonQUT](https://discuss.elastic.co/u/AntonQUT)\
**Post date:** [September 15, 2017, 3:44am UTC](https://discuss.elastic.co/t/how-to-search-with-full-json-query-using-java-api/100628/1 "2017-09-15T03:44:30Z")

</div>

I need to be able to perform searches with the full JSON queries that you would normally use with CURL. How can I do this using the Java API. My current approach is not working:

1. A sample query (which works directly using CURL)

2. The API method I am trying to use now where queryDef is the JSON query above:

3. The error results are: all shards failed

How can I run a query using the API where I have specified the full query?

---

<div class="post-metadata">

**Author:** ![AntonQUT](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@AntonQUT](https://discuss.elastic.co/u/AntonQUT)\
**Post date:** [September 15, 2017, 4:16am UTC](https://discuss.elastic.co/t/how-to-search-with-full-json-query-using-java-api/100628/2 "2017-09-15T04:16:53Z")

</div>

I seem to have got it working with:

```
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	QueryBuilder qb = QueryBuilders.simpleQueryStringQuery(queryDef);
	searchSourceBuilder.query(qb);

   SearchResponse response;		
	try {
		 response = _myClient.prepareSearch(indexToSearch)
		        .setTypes(docType)
		        .setSource(searchSourceBuilder)
		        .setFrom(0).setSize(maxNumberToGet).setExplain(true)
		        .execute()
		        .actionGet();
	} catch (Exception e) {
		System.out.println("Error performing search on index[" + indexToSearch + "] and docType[" + docType + "] using query[" + queryDef + "]. Error is: " + e.getLocalizedMessage());
		return null;
	}

```

Will have to test across different queries.

---

<div class="post-metadata">

**Author:** ![AntonQUT](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@AntonQUT](https://discuss.elastic.co/u/AntonQUT)\
**Post date:** [September 20, 2017, 12:16am UTC](https://discuss.elastic.co/t/how-to-search-with-full-json-query-using-java-api/100628/3 "2017-09-20T00:16:56Z")

</div>

This is actually incorrect and doesn't work. The best solution is to use the low level java REST api client.

---

<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:** [October 18, 2017, 12:17am UTC](https://discuss.elastic.co/t/how-to-search-with-full-json-query-using-java-api/100628/4 "2017-10-18T00:17:00Z")

</div>

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