# Pass raw json string query to Elastic Search using java

**URL:** <https://discuss.elastic.co/t/pass-raw-json-string-query-to-elastic-search-using-java/219154>\
**Category:** Elasticsearch\
**Created:** [February 13, 2020, 8:13am UTC](https://discuss.elastic.co/t/pass-raw-json-string-query-to-elastic-search-using-java/219154 "2020-02-13T08:13:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smack\_Alpha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smack_alpha/32/43660_2.png) [@Smack\_Alpha](https://discuss.elastic.co/u/Smack_Alpha)\
**Post date:** [February 13, 2020, 8:13am UTC](https://discuss.elastic.co/t/pass-raw-json-string-query-to-elastic-search-using-java/219154/1 "2020-02-13T08:13:08Z")

</div>

I have got the data from `Elasticsearch` index by using the below code:

```
SearchRequest request = new SearchRequest("football_sum_csv").scroll(new TimeValue(60000));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(matchQuery("multi", "test"));
searchSourceBuilder.size(10000);
searchSourceBuilder.sort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
request.source(searchSourceBuilder);
SearchResponse scrollResp = client.search(request, RequestOptions.DEFAULT);
System.out.println(scrollResp);

```

This will return all the data from my index of `ElasticSearch`.  
  
In here, I want to `filter` some data based on my `query`. In my case, I want to manually enter query and get results from `ElasticSearch`.

**For example:**   
  
This below query will return all the data as like above

```
query = {'query': {'bool': {'must': [{'match_all': {}}], 'must_not': [], 'should': []}} }

```

This below query will return all the data in the `Country` named `Spain`

```
query = {"query": {"bool": {"must": [{"match_phrase": {"countryName": "Spain"}}], "must_not": [], "should": []}}}

```

This below query will return all the data in the `os` named `Windows` and `sessionTime` between `1000` and `5000`

```
{"query": {"bool": {"must": [{"range": {"sessionTime": {"gte": 1000, "lt": 5000}}}, {"match_phrase": {"os": "Windows"}}], "must_not": [], "should": []}}}

```

In the above three queries, they perform different operation in `ElasticSearch`. I want to use this like queries to get my data.

If I put query inside here in above `java` code,

```
searchSourceBuilder.query({"query": {"bool": {"must": [{"match_phrase": {"countryName": "Spain"}}], "must_not": [], "should": []}}});

```

It gives me error.  
I know `Querybuilders` has lots of query options such as range, match and so on. I don't know how to make it. Please help me with some solutions.

---

<div class="post-metadata">

**Author:** ![Smack\_Alpha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smack_alpha/32/43660_2.png) [@Smack\_Alpha](https://discuss.elastic.co/u/Smack_Alpha)\
**Post date:** [February 14, 2020, 6:41am UTC](https://discuss.elastic.co/t/pass-raw-json-string-query-to-elastic-search-using-java/219154/2 "2020-02-14T06:41:47Z")

</div>

**Found the Answer:**  
  
`wrapperQuery` needs to be used for passing query as `json`

```
String query = "{"bool": {"must": [{"match_phrase": {"countryName": "Spain"}}], "must_not": [], "should": []}}";
QueryBuilder qb = QueryBuilders.wrapperQuery(query);
SearchSourceBuilder searchSourceBuilder1 = new SearchSourceBuilder();
searchSourceBuilder1.query(qb);
SearchRequest searchRequest = new SearchRequest("index_name");
searchRequest.source(searchSourceBuilder1);
SearchResponse scrollResp1 = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(scrollResp1);
```

---

<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:** [March 13, 2020, 6:41am UTC](https://discuss.elastic.co/t/pass-raw-json-string-query-to-elastic-search-using-java/219154/3 "2020-03-13T06:41:58Z")

</div>

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