# How to set more query to elasticsearch via java API

**URL:** <https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841>\
**Category:** Elasticsearch\
**Created:** [June 4, 2016, 1:42am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841 "2016-06-04T01:42:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 4, 2016, 1:42am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/1 "2016-06-04T01:42:41Z")

</div>

Hi I am trying to do query on elastic search by following the sql query and I want to implement same logic using Java API

```
select
* from log
where
name is "jay"
and [all_field_value] LIKE "%keyword%"
and @datetime between '2016-05-20T00:00:00.000Z' AND '2016-05-30T00:00:00.000Z'

```

my java code,

```
RangeQueryBuilder range = QueryBuilders.rangeQuery("@timestamp")
                                   .gte("2016-05-20T00:00:00.000Z")
                                   .lte("2016-05-27T00:00:00.000Z"); 

TermQueryBuilder nameqry = QueryBuilders.termQuery("name", "jay");  

QueryStringQueryBuilder qs = QueryBuilders.queryStringQuery("keyword")
                                      .field("message"); 

 BoolQueryBuilder keyboolqry = QueryBuilders.boolQuery();                                           
SearchResponse mibresponse = client.prepareSearch("logstash-record-*")
                        .setTypes("log")
                        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                        .setQuery(keyboolqry)  
                        .setQuery(nameqry ) 
                       .setQuery(qs)                                        
                      .setQuery(QueryBuilders.boolQuery().filter(range))
                       .setFetchSource(new String[] { "*" },new String[] {"message"})
                     .setFrom(0).setSize(60).setExplain(true)
                     .execute()
                    .actionGet();

```

when i run the code ,the results's timestamp is not between the RangeQueryBuilder range and each document is not all match the nameqry (that's can see in my code)

Please guide I am new to Elastic search. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 4, 2016, 6:23am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/2 "2016-06-04T06:23:41Z")

</div>

You must provide only one query. Each time you call setQuery it overwrites the previous one.

Set only the boolQuery but use must clauses to wrap your other queries .

See [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html#java-query-dsl-bool-query](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html#java-query-dsl-bool-query)

---

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 4, 2016, 6:52am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/3 "2016-06-04T06:52:56Z")

</div>

hi dadoonet,  
i have try to use "must" as you said looks like ,

```
BoolQueryBuilder keyboolqry = QueryBuilders.boolQuery().must(nameqry ).must(qs).must(range);
SearchResponse mibresponse = client.prepareSearch("logstash-record-*")
                    .setTypes("log")
                    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .setQuery(keyboolqry)  
                   .setFetchSource(new String[] { "*" },new String[] {"message"})
                 .setExplain(true)
                 .execute()
                .actionGet();

```

return result is all match nameqry and qs but didn't get all match results from  
the range that i set ,  
for example , it's have miss logstash-log-2016.05.26  
please can you tell me what's wrong with me ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 4, 2016, 7:20am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/4 "2016-06-04T07:20:01Z")

</div>

What results do you get back?

---

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 4, 2016, 7:31am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/6 "2016-06-04T07:31:47Z")

</div>

for my code ,i want to get from "2016-05-20T00:00:00.000Z") to ("2016-05-27T00:00:00.000Z"),  
it's only get 2016-05-23 to 2016-05-24 , but i check the document it's should have 2016-05-26 document too

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 4, 2016, 7:49am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/7 "2016-06-04T07:49:51Z")

</div>

Yes but can you show a full complete JSON doc ?

Also what is the mapping?

BTW it would be easier to write a full script which reproduces your issue in pure Rest/JSON.

Like:

```
DELETE index
PUT index/doc/1
{
"foo":"bar"
}
GET index/_search
{
  "query": { ... }
}
```

---

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 4, 2016, 8:01am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/10 "2016-06-04T08:01:57Z")

</div>

hi dadoonet,  
i found the problem is have to set size for searchresponse,  
but how to set searchresponse to get all record ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 4, 2016, 2:30pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/11 "2016-06-04T14:30:34Z")

</div>

It's a bit more clear.

So yes there is pagination.

If you want to extract all the results, you scroll API.

---

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 4, 2016, 3:39pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/13 "2016-06-04T15:39:48Z")

</div>

> [@stefansaye](#):
>
> can i sort the results by timestamp with desc ?

can i sort the results by timestamp with desc ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 4, 2016, 7:17pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/14 "2016-06-04T19:17:23Z")

</div>

Yes you can!

---

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 4, 2016, 11:40pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/15 "2016-06-04T23:40:36Z")

</div>

can you guild me how to do ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 5, 2016, 7:02am UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/16 "2016-06-05T07:02:47Z")

</div>

A bit old but you get the idea: [Sorting response hits using java api](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179)

---

<div class="post-metadata">

**Author:** ![stefansaye](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@stefansaye](https://discuss.elastic.co/u/stefansaye)\
**Post date:** [June 5, 2016, 1:57pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/18 "2016-06-05T13:57:50Z")

</div>

hi dadoonet,  
i try to using setscroll to get all results as you said,

.setScroll(new TimeValue(60000))  
.setSize(100)  
but it's only return 100 hits ,what's wrong with it ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 5, 2016, 6:25pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/19 "2016-06-05T18:25:41Z")

</div>

This is expected.

You must read the scroll API doc to understand how this works: [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-scroll.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-scroll.html)

---

<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:** [July 5, 2017, 10:46pm UTC](https://discuss.elastic.co/t/how-to-set-more-query-to-elasticsearch-via-java-api/51841/20 "2017-07-05T22:46:06Z")

</div>


