springboot2.2.2集成6.5 Elasticsearch
2024-08-29 23:16:11
1.0POM文件
<!-- spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>compile</scope>
</dependency> <!-- elasticSearch -->
<!-- es核心jar包 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
2.YML文件
spring:
profiles:
active: dev-win
redis:
database: 0
host: xxx
lettuce:
pool:
max-active: 10
max-idle: 10
max-wait: 5000ms
min-idle: 5
password: ''
port: 6379
timeout: 3000
session:
redis:
flush-mode: on_save
namespace: clearing
store-type: redis
timeout: 86400
data:
elasticsearch:
cluster-nodes: 192.168.1.234:9300
cluster-name: docker-cluster
repositories:
enabled: true
3.domain
@Document(indexName = "test-boot",type = "goods",shards = 1,replicas = 0, refreshInterval = "-1")
@Data
public class Elastic implements Serializable {
@Id
private String id;
@Field
private String name;
@Field
private String content;
@Field
private String price;
}
4.Dao
@Component
public interface ESDao extends ElasticsearchRepository<Elastic, String> {
Elastic queryEmployeeById(String id);
}
5.controller
@RestController
@RequestMapping("/es")
public class ElasticSearchController { @Autowired
private ESDao er; //增加
@RequestMapping("/add/{id}")
public String add(@PathVariable("id")String id){ Elastic employee=new Elastic();
employee.setId(id);
employee.setName("Y.S.K");
employee.setContent("ooo");
employee.setPrice("26");
er.save(employee); System.err.println("add a obj");
return "success";
} //删除
@RequestMapping("/delete")
public String delete(){
Elastic employee=new Elastic();
employee.setId("1");
er.delete(employee); return "success";
} //局部更新
@RequestMapping("/update")
public String update(){ Elastic employee=er.queryEmployeeById("1");
employee.setName("哈哈");
er.save(employee); System.err.println("update a obj"); return "success";
} //查询
@RequestMapping("/query/{id}")
public Elastic query(@PathVariable("id")String id){ Elastic accountInfo=er.queryEmployeeById(id);
System.err.println(JSONUtil.parseObj(accountInfo)); return accountInfo;
} }
最新文章
- 继续(3n+1)猜想
- jquery 获取元素在浏览器中的绝对位置
- css3文字导航鼠标悬停气泡动画特效源码下载
- [转帖] Symbol Emotions Sticker 英文符号表情大全
- CSS笔记(六)链接
- SparkStreaming结合Kafka使用
- Mybatis的if test字符串比较问题
- Spark RDD概念学习系列之RDD与DSM的异同分析(十三)
- SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean
- c++Socket 异步通讯
- Android学习笔记--服务(Service)
- 2017级C语言教学总结
- SpriteBuilder给节点添加effect在32设备上发生crash
- BZOJ_1878_[SDOI2009]HH的项链_莫队
- 深度学习之卷积神经网络(CNN)详解与代码实现(二)
- Python tab键命令补全
- Java并发编程:Java Thread方法join的简单总结
- [转帖] Kubernetes如何使用ReplicationController、Replica Set、Deployment管理Pod ----文章很好 但是还没具体操作实践 也还没记住.
- [C++] string与int, float, double相互转换
- 使用 php 7.3.0 官网的压缩包安装 FastAdmin