How to set more query to elasticsearch via java API

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

* from log
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")

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

QueryStringQueryBuilder qs = QueryBuilders.queryStringQuery("keyword")

 BoolQueryBuilder keyboolqry = QueryBuilders.boolQuery();                                           
SearchResponse mibresponse = client.prepareSearch("logstash-record-*")
                        .setQuery(nameqry ) 
                       .setFetchSource(new String[] { "*" },new String[] {"message"})

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.

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 .


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-*")
                   .setFetchSource(new String[] { "*" },new String[] {"message"})

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 ?

What results do you get back?

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

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.


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

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

It's a bit more clear.

So yes there is pagination.

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

can i sort the results by timestamp with desc ?

Yes you can!

can you guild me how to do ?

A bit old but you get the idea: Sorting response hits using java api

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

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

This is expected.

You must read the scroll API doc to understand how this works: