欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Elasticsearch结合spring的使用

时间:2023-08-16

一、spring项目能创建ES连接

public static void main(String[] args) throws Exception { // 创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 关闭ES客户端 esClient.close(); }

二、向ES中新建索引并新增一条数据

public static void main(String[] args) throws Exception { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 插入数据 IndexRequest request = new IndexRequest(); request.index("user").id("101"); User user = new User(); user.setName("张三"); user.setAge(25); user.setSex("男"); // 向ES插入数据,必须将数据转换位JSON格式 //这里使用的GSON,将Object对象转化为json字符串 ObjectMapper mapper = new ObjectMapper(); String userJson = mapper.writevalueAsString(user); request.source(userJson, XContentType.JSON); IndexResponse response = esClient.index(request, RequestOptions.DEFAULT); System.out.println(response.getResult()); // 关闭ES客户端 esClient.close(); }

三、向ES中批量新增多条数据

public static void main(String[] args) throws Exception { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 批量插入数据 BulkRequest request = new BulkRequest(); request.add(new IndexRequest().index("user").id("102").source(XContentType.JSON, "name", "李四", "age",30,"sex","女")); request.add(new IndexRequest().index("user").id("103").source(XContentType.JSON, "name", "王五", "age",35,"sex","男")); request.add(new IndexRequest().index("user").id("104").source(XContentType.JSON, "name", "赵六", "age",40,"sex","女")); request.add(new IndexRequest().index("user").id("105").source(XContentType.JSON, "name", "陈七", "age",55,"sex","男")); BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT); System.out.println(response.getItems()); // 关闭ES客户端 esClient.close(); }

四、修改ES中索引中的数据信息

public static void main(String[] args) throws Exception { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 修改数据 UpdateRequest request = new UpdateRequest(); request.index("user").id("101"); request.doc(XContentType.JSON, "sex", "女"); UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT); System.out.println(response.getResult()); // 关闭ES客户端 esClient.close(); }

五、删除ES中索引中的数据

public static void main(String[] args) throws Exception { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); DeleteRequest request = new DeleteRequest(); request.index("user").id("101"); DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT); System.out.println(response.toString()); // 关闭ES客户端 esClient.close(); }

六、批量删除ES中索引的相关数据

public static void main(String[] args) throws Exception { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 批量删除数据 BulkRequest request = new BulkRequest(); request.add(new DeleteRequest().index("user").id("101")); request.add(new DeleteRequest().index("user").id("102")); request.add(new DeleteRequest().index("user").id("103")); BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT); System.out.println(response.getItems()); // 关闭ES客户端 esClient.close(); }

七、获取ES中索引的所有数据

public static void main(String[] args) throws Exception { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 1、查询索引的所有数据 SearchRequest request = new SearchRequest(); request.indices("user"); // 构造查询条件 SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchAllQuery()); request.source(builder); SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); SearchHits hits = response.getHits(); System.out.println(hits.getTotalHits()); Iterator iterator = hits.iterator(); while (iterator.hasNext()) { SearchHit hit = iterator.next(); System.out.println(hit.getSourceAsString()); } // 关闭ES客户端 esClient.close(); }

八、根据条件查询ES中索引的数据

// 2、条件查询 : termQuery SearchRequest request = new SearchRequest(); request.indices("user"); request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30))); SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); SearchHits hits = response.getHits(); System.out.println(hits.getTotalHits()); for ( SearchHit hit : hits ) { System.out.println(hit.getSourceAsString()); }

九、 根据分页查询ES中索引的数据

// 3、分页查询 SearchRequest request = new SearchRequest(); request.indices("user"); SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); // (当前页码-1)*每页显示数据条数 builder.from(2); builder.size(2); request.source(builder); SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); SearchHits hits = response.getHits(); System.out.println(hits.getTotalHits()); for ( SearchHit hit : hits ) { System.out.println(hit.getSourceAsString()); }

十、根据模糊查询ES中索引的数据

// 4、模糊查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();builder.query(QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.TWO));request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());for ( SearchHit hit : hits ) { System.out.println(hit.getSourceAsString());}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。