一 整合
整合代码放在 https://gitee.com/elvinle/jd_pachong.git 中, 并不复杂
elasticsearch 官方推荐使用 rest 方式进行操作.
二 库操作
private static String INDEX_DEFAULT = "elvin"; private static String INDEX_JD = "jd_goods"; @Autowired RestHighLevelClient client; //region 索引库操作 //创建索引 @Test public void createIndex() throws Exception { CreateIndexRequest createReq = new CreateIndexRequest(INDEX_DEFAULT); CreateIndexResponse resp = client.indices().create(createReq, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(resp)); } //判断索引是否存在 @Test public void existsIndex() throws Exception { GetIndexRequest createIndexReq = new GetIndexRequest(INDEX_DEFAULT); boolean exists = client.indices().exists(createIndexReq, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(exists)); } //删除索引 @Test public void deleteIndex() throws Exception { DeleteIndexRequest deleteReq = new DeleteIndexRequest(INDEX_DEFAULT); AcknowledgedResponse resp = client.indices().delete(deleteReq, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(resp)); } //endregion
建库一般都是提前建好的.
三 文档操作
//region 文档操作 //创建文档 @Test public void addDoc() throws Exception{ User user = User.builder() .id("1") .name("elvin") .age(29) .work("java软件开发工程师") .hobby(new ArrayList<String>() {{ add("篮球"); add("羽毛球"); add("乒乓球"); add("游泳"); }}) .desc("一个喜欢运动, 爱老婆的boy").build(); IndexRequest req = new IndexRequest(INDEX_DEFAULT); req.id(user.getId()); req.timeout("5s"); req.source(JSON.toJSONString(user), XContentType.JSON); IndexResponse resp = client.index(req, RequestOptions.DEFAULT); System.out.println("添加文档 -> " + req.toString()); System.out.println("结果 -> " + resp.toString()); System.out.println("状态 -> " + resp.status()); } //判断文档是否存在 @Test public void existsDoc() throws Exception{ GetRequest req = new GetRequest(INDEX_DEFAULT, "1"); boolean exists = client.exists(req, RequestOptions.DEFAULT); System.out.println("文档是否存在 : " + exists); } //根据id获取文档 @Test public void getDoc() throws Exception{ GetRequest req = new GetRequest(INDEX_DEFAULT, "1"); GetResponse resp = client.get(req, RequestOptions.DEFAULT); System.out.println("获取到的文档 : " + resp.getSourceAsString()); } //修改文档 @Test public void updateDoc() throws Exception{ User user = new User(); user.setDesc("热爱生活和热爱工作"); UpdateRequest req = new UpdateRequest(INDEX_DEFAULT, "1"); req.id("1"); req.timeout("5s"); req.doc(JSON.toJSONString(user), XContentType.JSON); UpdateResponse resp = client.update(req, RequestOptions.DEFAULT); System.out.println("修改文档 --> " + req.toString()); System.out.println("修改结果 --> " + resp.status()); //getDoc(); } //根据id删除文档 @Test public void deleteDoc() throws Exception{ DeleteRequest req = new DeleteRequest(INDEX_DEFAULT, "1"); DeleteResponse resp = client.delete(req, RequestOptions.DEFAULT); System.out.println("获取到的文档 : " + resp.status()); } //批量添加 @Test public void bulkAddDoc() throws Exception{ List<User> userList = new ArrayList<>(); for (int i = 1; i < 11; i++) { User user = User.builder() .id(String.valueOf(i)) .name("elvin" + i) .age(29) .work("java软件开发工程师") .hobby(new ArrayList<String>() {{ add("篮球"); add("羽毛球"); add("乒乓球"); add("游泳"); }}) .desc("一个喜欢运动, 爱老婆的boy") .build(); userList.add(user); } BulkRequest req = new BulkRequest(); req.timeout("5s"); for (int i = 0; i < userList.size(); i++) { req.add(new IndexRequest(INDEX_DEFAULT) .id(String.valueOf(i + 1 )) .source(JSON.toJSONString(userList.get(i)), XContentType.JSON) ); } BulkResponse resp = client.bulk(req, RequestOptions.DEFAULT); System.out.println("批量添加文档 -> " + resp.hasFailures()); } //查询文档 @Test public void searchDoc() throws Exception{ SearchRequest req = new SearchRequest(INDEX_JD); //构建查询条件 SearchSourceBuilder reqSourceBuilder = new SearchSourceBuilder(); //设置分页 reqSourceBuilder.from(0); reqSourceBuilder.size(5); //设置高亮 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field("title"); highlightBuilder.preTags("<span style='color:red'>"); highlightBuilder.postTags("</span>"); reqSourceBuilder.highlighter(highlightBuilder); //term查询 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "java"); reqSourceBuilder.query(termQueryBuilder); reqSourceBuilder.timeout(new TimeValue(5, TimeUnit.SECONDS)); req.source(reqSourceBuilder); SearchResponse resp = client.search(req, RequestOptions.DEFAULT); System.out.println("测试查询文档-----"+JSON.toJSONString(resp.getHits())); System.out.println("====================="); for (SearchHit documentFields : resp.getHits().getHits()) { System.out.println("测试查询文档--遍历参数--"+documentFields.getSourceAsMap()); } System.out.println("----------替换高亮列-----------"); //解析结果 ArrayList<Map<String, Object>> list = new ArrayList<>(); for (SearchHit hit : resp.getHits().getHits()) { //解析高亮的字段 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); HighlightField title = highlightFields.get("title"); Map<String, Object> sourceAsMap = hit.getSourceAsMap(); //解析高亮的字段 if(title != null){ Text[] fragments = title.fragments(); String n_title = ""; for (Text text : fragments) { n_title += text; } //将高亮的字段替换掉原来的字段 sourceAsMap.put("title", n_title); } list.add(sourceAsMap); } list.forEach(n-> System.out.println(JSON.toJSONString(n))); } //endregion
文档操作中, 可能查询比较多用一点. 增删改可以通过 canal 来完成.