1种方法:
Java 范围查询(时间范围查询示例)来自: https://blog.csdn.net/zx711166/article/details/77847103
RangeQueryBuilder 构建范围查询条件;
可以使用 RangeQueryBuilder 作为时间范围查询。
示例如下:
public class EsRange{ public void RangeSearch(TransportClient client){ //时间范围的设定 RangeQueryBuilder rangequerybuilder = QueryBuilders .rangeQuery("inputtime") .from("2016-7-21 00:00:01").to("2016-7-21 00:00:03"); //生成DSL查询语句 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(rangequerybuilder); //System.out.println(sourceBuilder.toString()); //查询建立 SearchRequestBuilder responsebuilder = client .prepareSearch("pointdata") .setTypes("pointdata"); SearchResponse myresponse=responsebuilder .setQuery(QueryBuilders.boolQuery() .must(QueryBuilders.matchPhraseQuery //must表示and ("pointid","W3.UNIT1.10LBG01CP301") ) .must(rangequerybuilder)) .setFrom(0).setSize(50) //分页 .setExplain(true) .execute() .actionGet(); SearchHits hits = myresponse.getHits(); for(int i = 0; i < hits.getHits().length; i++) { System.out.println(hits.getHits()[i].getSourceAsString()); } } }
第二种方法:
public interface ItemRepository extends ElasticsearchRepository<Item,Long>{ /** * 根据价格区间查询 * @param price1 * @param price2 * @return */ List<Item> findByPriceBetween(double price1, double price2); }