前言
此篇博客还是一些基础操作,没什么可写的,需要的同学直接抄作业进行测试就可以
上一节写了连接和测试新增操作,这一节写java操作ES的基本操作,也就是增删改查,在这里补充一点知识,我们之前用了指定的索引进行指定添加
有一个情况是,如果我们指定了一个不存在的索引时,他会创建一个新的索引,例如我们这个ik1是不存在的,执行添加后也可以添加进去,索引库会新增一个叫ik1的索引,如图
一、查询
注:查询时需要查询存在的索引,不存在会报错,提示not found
/*** 查询测试*/@Testpublic void testRetrieve() throws IOException {GetRequest getRequest = new GetRequest("ik","5");GetResponse response = client.get(getRequest,RequestOptions.DEFAULT);System.out.println(response);}
二、更新
忽略警告
/*** 更新** @throws IOException*/@Testpublic void testUpdate() throws IOException {HashMap<String,Object> map = new HashMap<>();map.put("name","哈西布鲁根");map.put("content","拳皇99");UpdateRequest updateRequest = new UpdateRequest("ik","5").doc(map);UpdateResponse updateResponse =client.update(updateRequest,RequestOptions.DEFAULT);System.out.println(updateRequest);}
三、删除
/*** 删除*/@Testpublic void testDelete() throws IOException {DeleteRequest deleteRequest = new DeleteRequest("ik","5");DeleteResponse deleteResponse = client.delete(deleteRequest,RequestOptions.DEFAULT);System.out.println(deleteRequest);}
四、批量增删改
其实就是增加了一个容器BulkRequest,把所有操作放到了这个容器里,然后同意操作
为了查看效果,注释了删除
/*** 批量增删改*/@Testpublic void testCUD() throws IOException {BulkRequest request = new BulkRequest();/** 增加IndexRequest* XContentType键值对*/request.add(new IndexRequest("ik").id("5").source(XContentType.JSON,"name","罗翔","content","法外狂徒张三"));/** 更新UpdateRequest*/request.add(new UpdateRequest("ik","5").doc(XContentType.JSON,"name","罗翔","content","法外狂徒李四"));/** 删除DeleteRequest*/
// request.add(new DeleteRequest("ik").id("5"));BulkResponse bulkResponse = client.bulk(request,RequestOptions.DEFAULT);System.out.println(bulkResponse.toString());}
五、查询
1.查询全部
查询全部,可变参数可指定多个索引,如果结果超过十条,默认返回十条
/*** 查询** @throws IOException*/@Testpublic void testQuery() throws IOException {//可以指定多个索引,用逗号隔开SearchRequest searchRequest = new SearchRequest("ik","shop");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//查询所有searchSourceBuilder.query(QueryBuilders.matchAllQuery());searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);System.out.println(response);}
查询结果是一个JSON,转了一下格式,看着清晰一点,查出来的内容是不能直接使用的,所以还需要转换格式
取出我们想要的部分,图例说明更清晰一些
有三种方式,根据实际使用情况选择,详见注释
/*** 查询** @throws IOException*/@Testpublic void testQuery() throws IOException {//可以指定多个索引,用逗号隔开SearchRequest searchRequest = new SearchRequest("ik","shop");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//查询所有,如果结果超过10条,默认显示10条searchSourceBuilder.query(QueryBuilders.matchAllQuery());searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);//调整结构,取出需要的部分long value = response.getHits().getTotalHits().value;System.out.println(response);if(0<value){SearchHit[] hits = response.getHits().getHits();for(SearchHit hit : hits){System.out.println(hit.getIndex());System.out.println(hit.getId());System.out.println(hit.getScore());//三种方式,根据实际使用情况选择System.out.println(hit.getSourceAsMap());System.out.println(hit.getSourceAsString());System.out.println(hit.getSourceRef());System.out.println("==========");}}}
2.按照条件查询
按照条件查询可以分为单字段单索引,单字段多索引,多字段单索引,多字段多索引,多个用逗号隔开就行,有点区别,但区别不大,详见代码注释
/*** 根据条件查询** @throws IOException*/@Testpublic void testQueryMatch() throws IOException {//可以指定多个索引,用逗号隔开SearchRequest searchRequest = new SearchRequest("ik","shop");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//单字段匹配-->字段对值
// searchSourceBuilder.query(QueryBuilders.matchQuery("content","中国"));//多字段匹配-->值对多字段searchSourceBuilder.query(QueryBuilders.multiMatchQuery("中国","content","name"));searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);//调整结构,取出需要的部分long value = response.getHits().getTotalHits().value;System.out.println(response);if(0<value){SearchHit[] hits = response.getHits().getHits();for(SearchHit hit : hits){System.out.println(hit.getIndex());System.out.println(hit.getId());System.out.println(hit.getScore());//三种方式,根据实际使用情况选择System.out.println(hit.getSourceAsMap());System.out.println(hit.getSourceAsString());System.out.println(hit.getSourceRef());System.out.println("==========");}}}
3.排序
/*** 排序** @throws IOException*/@Testpublic void testQuerySortHighLevel() throws IOException {//可以指定多个索引,用逗号隔开SearchRequest searchRequest = new SearchRequest("ik","shop");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//多字段匹配-->值对多字段searchSourceBuilder.query(QueryBuilders.multiMatchQuery("中国","content","name"));//从第几位开始分几条,分别是索引下标,大小//分页查询默认查询倒序searchSourceBuilder.from(0).size(5);
// //正序写法
// searchSourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC));/** 指定字段排序* 注意不是数据库里面的字段,是es里面的字段* 默认正序,倒序.order(SortOrder.DESC))** 指定字段后,分数排序失效*/searchSourceBuilder.sort(SortBuilders.fieldSort("id"));searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);//调整结构,取出需要的部分long value = response.getHits().getTotalHits().value;System.out.println(response);if(0<value){SearchHit[] hits = response.getHits().getHits();for(SearchHit hit : hits){System.out.println(hit.getIndex());System.out.println(hit.getId());System.out.println(hit.getScore());//三种方式,根据实际使用情况选择System.out.println(hit.getSourceAsMap());System.out.println(hit.getSourceAsString());System.out.println(hit.getSourceRef());System.out.println("==========");}}}
4.高亮
/*** 排序** @throws IOException*/@Testpublic void testQuerySortHighLevel() throws IOException {//可以指定多个索引,用逗号隔开SearchRequest searchRequest = new SearchRequest("ik","shop");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//多字段匹配-->值对多字段searchSourceBuilder.query(QueryBuilders.multiMatchQuery("中国","content","name"));//从第几位开始分几条,分别是索引下标,大小//分页查询默认查询倒序searchSourceBuilder.from(0).size(5);
// //正序写法searchSourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC));/** 指定字段排序* 注意不是数据库里面的字段,是es里面的字段* 默认正序,倒序.order(SortOrder.DESC))** 指定字段后,分数排序失效*/
// searchSourceBuilder.sort(SortBuilders.fieldSort("id"));//高亮HighlightBuilder highlightBuilder = new HighlightBuilder().field("content").preTags("<p style='color:red'>").postTags("</>");searchSourceBuilder.highlighter(highlightBuilder);searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);//调整结构,取出需要的部分long value = response.getHits().getTotalHits().value;System.out.println(response);if(0<value){SearchHit[] hits = response.getHits().getHits();for(SearchHit hit : hits){System.out.println(hit.getIndex());System.out.println(hit.getId());System.out.println(hit.getScore());//三种方式,根据实际使用情况选择System.out.println(hit.getSourceAsMap());System.out.println(hit.getSourceAsString());System.out.println(hit.getSourceRef());//显示高亮.fragments取文本System.out.println("hl: "+String.valueOf(hit.getHighlightFields().get("content").fragments()[0]));System.out.println("==========");}}}
写在最后
一些基础用法,详见注释,都能用