赞
踩
pom依赖引入
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>8.3.2</version>
- </dependency>
- <dependency>
- <groupId>co.elastic.clients</groupId>
- <artifactId>elasticsearch-java</artifactId>
- <version>8.3.2</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-client</artifactId>
- <version>8.3.2</version>
- </dependency>
- <dependency>
- <groupId>jakarta.json</groupId>
- <artifactId>jakarta.json-api</artifactId>
- <version>2.1.1</version>
- </dependency>
application.yaml配置文件ip地址写法:
- spring:
- main:
- allow-bean-definition-overriding: true
- servlet:
- multipart:
- max-request-size: 100MB
- max-file-size: 100MB
- # 搜索引擎
- elasticsearch:
- rest:
- # username: elastic
- # password: elastic
- uris: 127.0.0.1:9200
ElasticsearchConfig类:
- @Component
- public class ElasticsearchConfig {
-
- @Bean
- public ElasticsearchClient elasticsearchClient(RestClient restClient) {
- ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
- ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
- return elasticsearchClient;
- }
-
- }
将数据写入es,比如我们要将Info实体的数据写入es:
- IndexRequest<Object> indexRequest = new IndexRequest.Builder<>()
- .index("info") //索引
- .id(info.getTid()+"") //主键
- .document(info) //需要写入es的实体
- .build();
- try {
- elasticsearchClient.index(indexRequest);
- } catch (ElasticsearchException | IOException e) {
- log.error("",e);
- }
es的查询、修改、删除:
- @Autowired
- private ElasticsearchClient elasticsearchClient;
-
- /**
- * 通过id查询
- */
- public static void get() {
- try {
- long tid = 1889L;
- Query query = QueryBuilders.ids().values(tid + "").build()._toQuery();
- SearchResponse<Info> response = elasticsearchClient
- .search(s -> s.index("info").query(query), Info.class);
- for (Hit<Info> hit : response.hits().hits()) {
- Info source = hit.source();
- System.err.println("Found product " + source);
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
-
- /**
- * 根据id修改数据
- */
- public static void edit() {
- try {
- long tid = 1889L;
- Map<String, Object> map = new HashMap<>();
- map.put("status", 1);
- // 构建修改的请求,因为修改数据es会默认1秒后生效,需要加上refresh(Refresh.True)立即生效
- UpdateResponse<Info> response = elasticsearchClient.update(
- e -> e.index("info").id(tid + "").refresh(Refresh.True).doc(map),
- Info.class);
- } catch (Exception e) {
- // TODO: handle exception
- }
-
- }
-
-
- /**
- * 删除
- */
- public static void delete() {
- try {
- long tid = 1889L;
- elasticsearchClient.delete(e -> e.index("info").id(tid + ""));
- } catch (Exception e) {
- // TODO: handle exception
- }
-
- }
-
-
- /**
- * 删除索引
- */
- public static void deleteAll() {
- try {
- DeleteIndexResponse deleteIndexResponse = elasticsearchClient.indices().delete(s -> s.index("info"));
- } catch (ElasticsearchException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
下期分享ElasticsearchClient查询分页、高级查询的方法~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。