赞
踩
项目用到了ES,不是很难,但是资料感觉比较少,而且方式还不少,但大多不适合自己,于是自己写个小demo测试了一下.
刚开始用的springdata,开发环境没问题,但是发到线上环境的时候有冲突,于是改成用RestHighLecelClient
依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.6.0</version> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.6.0</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.6.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.0.10.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.6</version> </dependency> </dependencies> </project>
实体类
public class Users { private String name; private int age; public Users() { } public Users(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Users{" + "name='" + name + '\'' + ", age=" + age + '}'; }
配置类
@Configuration
public class RestClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client=new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
//如果是集群
// RestHighLevelClient client = new RestHighLevelClient(
// RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")
// , new HttpHost("127.0.0.1", 9201, "http")
// , new HttpHost("127.0.0.1", 9202, "http")));
return client;
}
}
实现增删改查
package com.jt.dubbo.controller; import com.alibaba.fastjson.JSON; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @Author: 覃永福 * @Date: 2020/06/27 0:33 * @state: */ @org.springframework.web.bind.annotation.RestController public class RestController { @Autowired(required = false) private RestHighLevelClient restHighLevelClient; @RequestMapping("hello") public String hello() { return "success"; } /** * 添加索引 * * @return */ @RequestMapping("saveIndex") public String saveIndex() { try { CreateIndexRequest qyf = new CreateIndexRequest("qyf"); restHighLevelClient.indices().create(qyf, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return "success"; } /** * 查询索引是否存在 * * @return */ @RequestMapping("existIndex") public String existIndex() { try { GetIndexRequest qyf = new GetIndexRequest("qyf"); boolean exists = restHighLevelClient.indices().exists(qyf, RequestOptions.DEFAULT); System.out.println(exists); } catch (IOException e) { e.printStackTrace(); } return "success"; } /** * 添加文档 * * @return */ @RequestMapping("addDocument") public String addDocument() { try { Users user = new Users(); user.setName("覃cc1"); user.setAge(20); IndexRequest request = new IndexRequest("qyf"); request.source(JSON.toJSONString(user), XContentType.JSON); request.id("1"); IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(index.toString()); } catch (IOException e) { e.printStackTrace(); } return "success"; } @RequestMapping("getDocument") public String getDocument() { GetResponse documentFields = null; try { GetRequest qyf = new GetRequest("qyf", "1"); documentFields = restHighLevelClient.get(qyf, RequestOptions.DEFAULT); System.out.println(documentFields); } catch (IOException e) { e.printStackTrace(); } return documentFields.toString(); } /** * 更新文档 * * @return */ @RequestMapping("updateDocument") public String updateDocument() { UpdateResponse update = null; try { UpdateRequest qyf = new UpdateRequest("qyf", "1"); Users users = new Users(); users.setName("覃帅帅"); UpdateRequest doc = qyf.doc(JSON.toJSONString(users), XContentType.JSON); update = restHighLevelClient.update(qyf, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return update.toString(); } /** * 删除文档 * @return */ @RequestMapping("deleteDocument") public String deleteDocument() { try { DeleteRequest qyf = new DeleteRequest("qyf", "1"); DeleteResponse delete = restHighLevelClient.delete(qyf, RequestOptions.DEFAULT); System.out.println(delete.toString()); } catch (IOException e) { e.printStackTrace(); } return "success"; } /** * 批量添加文档 * 返回false表示没有失败 * * @return */ @RequestMapping("saveDocuments") public String saveDocuments() { try { BulkRequest bulkRequest = new BulkRequest(); List<Users> list = new ArrayList<>(); for (int i = 0; i < 100; i++) { list.add(new Users("覃帅帅" + i, 20 + 2 * i)); } for (int i = 0; i < list.size(); i++) { bulkRequest.add(new IndexRequest("qyf").id("" + i + 10086).source(JSON.toJSONString(list.get(i)), XContentType.JSON)); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.hasFailures()); } catch (IOException e) { e.printStackTrace(); } return "success"; } /** * 批量查询 * * @return */ @RequestMapping("findList") public List<Users> findList() { List<Users> list = null; try { //构建查询器 SearchRequest request = new SearchRequest("qyf"); //设置查询条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //多条件查询 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); //范围查询 boolQueryBuilder.must(QueryBuilders.rangeQuery("age") .gte(50).lte(90)); //模糊匹配 boolQueryBuilder.must(QueryBuilders.wildcardQuery("name","*1*")); //多条件查询放入条件中 sourceBuilder.query(boolQueryBuilder); //分页, ES从第0页开始,不设置默认只会显示10条记录 sourceBuilder.from(0); sourceBuilder.size(500); //排序 sourceBuilder.sort("age",SortOrder.ASC); //将条件放入查询器 request.source(sourceBuilder); //请求 SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT); SearchHit[] hits = search.getHits().getHits(); list = new ArrayList<>(); for (SearchHit hit : hits) { Users users = new Users(); users.setName(hit.getSourceAsMap().get("name").toString()); users.setAge(Integer.parseInt(hit.getSourceAsMap().get("age").toString())); list.add(users); } } catch (IOException e) { e.printStackTrace(); } System.out.println("记录数:" + list.size()); System.out.println(list); return list; } /** *查询全部 */ @RequestMapping("findAll") public List<Users> findAll() { List<Users> list = null; try { //构建查询器 SearchRequest rq = new SearchRequest("qyf"); //设置查询条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //ES从第0页开始,不设置默认只会显示10条记录 sourceBuilder.from(0); sourceBuilder.size(500); //将条件放入查询器 rq.source(sourceBuilder); SearchResponse search = restHighLevelClient.search(rq, RequestOptions.DEFAULT); SearchHit[] hits = search.getHits().getHits(); list = new ArrayList<>(); for (SearchHit hit : hits) { Users users = new Users(); users.setName(hit.getSourceAsMap().get("name").toString()); users.setAge(Integer.parseInt(hit.getSourceAsMap().get("age").toString())); list.add(users); } } catch (IOException e) { e.printStackTrace(); } System.out.println("记录数:" + list.size()); System.out.println(list); return list; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。