简体   繁体   中英

Elasticsearch using 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

select * from log , web where l.loghost = w.webhost and @datetime between '2016-05-20' AND '2016-05-25' 

log and web are different types, and indices are set to logstash-log-* and logstash-web* , @timestamp format looks like "2016-05-20T17:14:01.037Z"

Now I have the following Java code but i don't know how to set between two dates ,so it does not return expected output

  SearchResponse response = client.prepareSearch("logstash-log-*","logstash-web-*")
   .setTypes("log","web")
  .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  .setFetchSource(new String[]{"*"}, null)
 .setQuery(QueryBuilders.queryStringQuery("1.2.3.4").field("*_host"))// Query
  .execute()
 .actionGet();

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

You need to combine a range query with your query_string query inside a bool/filter query :

QueryStringQueryBuilder qs = QueryBuilders.queryStringQuery("1.2.3.4").field("*_host");
RangeQueryBuilder range = QueryBuilders.rangeQuery("@timestamp")
    .gte("2016-05-20T00:00:00.000Z")
    .lte("2016-05-25T00:00:00.000Z");

and then

...
.setQuery(QueryBuilders.boolQuery().filter(qs).filter(range))
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM