Java操作ElasticSearch(三、查询)
查询的步骤如下
- 创建 SearchSourceBuilder 对象
- 添加查询条件 QueryBuilders
- 添加排序、分页等其他条件
- 创建 SearchRequest 对象,并指定索引库名称
- 发起请求,得到结果
- 获取总条数
- 获取 SearchHits 数组,并遍历
- 获取其中的 _source,是 JSON 数据
- 把 _source 反序列化为 User 对象
查询所有 match_all
@Test public void test011() throws IOException { // 创建请求对象,用于描述本次请求语义 SearchRequest request = new SearchRequest("user"); // 创建查询语义对象 // 设置查询类型 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // 添加SearchSourceBuilder对象到SearchRequest对象中 request.source(searchSourceBuilder); // 发送请求给es SearchResponse response = client.search(request, RequestOptions.DEFAULT); // 处理响应结果 SearchHits searchHits = response.getHits(); // 总条数 System.out.println("命中的总条数: " + searchHits.getTotalHits().value); // 12 // 返回数据数组 SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
分词查询 match
@Test public void test12() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("note", "唱歌表演")); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
词条查询 term
@Test public void test14() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.termQuery("gender", "女")); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits()); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
范围查询 range
@Test public void test15() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age"); // 查询年龄大于20岁 小于25岁的同学 queryBuilder.gt(20).lt(25); searchSourceBuilder.query(queryBuilder); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits()); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
模糊查询 fuzzy
@Test public void test16() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.fuzzyQuery("note", "三")); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits()); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
布尔查询 bool
@Test public void test13() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); // 添加条件 queryBuilder.must(QueryBuilders.matchQuery("note", "Java")); queryBuilder.mustNot(QueryBuilders.termQuery("gender", "女")); searchSourceBuilder.query(queryBuilder); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits()); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }