判断是否存在该字段,用法和SQL语句中的exist类似。
private static void existSearch() throws IOException { String type = "_doc"; String index = "test1"; // 查询指定的索引库 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 设置查询条件 sourceBuilder.query(QueryBuilders.existsQuery("msgcode")); searchRequest.source(sourceBuilder); System.out.println("存在查询的DSL语句:"+sourceBuilder.toString()); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 结果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); String string = hit.getSourceAsString(); System.out.println("存在查询的Map结果:" + map); System.out.println("存在查询的String结果:" + string); }); System.out.println("\n=================\n"); } 范围查询和SQL语句中<>使用方法一样,其中gt是大于,lt是小于,gte是大于等于,lte是小于等于。
private static void rangeSearch() throws IOException{ String type = "_doc"; String index = "test1"; SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 设置查询条件 sourceBuilder.query(QueryBuilders.rangeQuery("sendtime").gte("2019-01-01 00:00:00").lte("2019-12-31 23:59:59")); searchRequest.source(sourceBuilder); System.out.println("范围查询的DSL语句:"+sourceBuilder.toString()); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 结果 searchResponse.getHits().forEach(hit -> { String string = hit.getSourceAsString(); System.out.println("范围查询的String结果:" + string); }); System.out.println("\n=================\n"); } 正则查询ES可以使用正则进行查询,查询方式也非常的简单,代码示例如下:
private static void regexpSearch() throws IOException{ String type = "_doc"; String index = "test1"; // 查询指定的索引库 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 设置查询条件 sourceBuilder.query(QueryBuilders.regexpQuery("message","xu[0-9]")); searchRequest.source(sourceBuilder); System.out.println("正则查询的DSL语句:"+sourceBuilder.toString()); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 结果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); String string = hit.getSourceAsString(); System.out.println("正则查询的Map结果:" + map); System.out.println("正则查询的String结果:" + string); }); System.out.println("\n=================\n"); } 查询测试结果所有查询总数:6
普通查询的DSL语句:{"from":0,"size":5,"timeout":"60s","query":{"term":{"uid":{"value":"1234","boost":1.0}}}}
=================
或查询语句:{"query":{"bool":{"must":[{"bool":{"should":[{"term":{"uid":{"value":1234,"boost":1.0}}},{"term":{"uid":{"value":12345,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},{"term":{"phone":{"value":"12345678909","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
或查询结果:{msgcode=1, uid=12345, phone=12345678909, message=qq, sendtime=2019-03-14 01:57:04}
=================
模糊查询语句:{"query":{"bool":{"must":[{"wildcard":{"message":{"wildcard":"xu","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
模糊查询结果:{msgcode=2, uid=12345, phone=123456789019, sendtime=2019-03-14 01:57:04, message=xuwujing study Elasticsearch}
模糊查询结果:{uid=123456, phone=12345678909, message=xu1, sendtime=2019-03-14 01:57:04}
=================
存在查询的DSL语句:{"query":{"exists":{"field":"msgcode","boost":1.0}}}
存在查询的Map结果:{msgcode=2, uid=12345, phone=123456789019, sendtime=2019-03-14 01:57:04, message=xuwujing study Elasticsearch}
存在查询的String结果:{"uid":12345,"phone":123456789019,"msgcode":2,"sendtime":"2019-03-14 01:57:04","message":"xuwujing study Elasticsearch"}
存在查询的Map结果:{msgcode=1, uid=12345, phone=12345678909, message=qq, sendtime=2019-03-14 01:57:04}
存在查询的String结果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}
=================