SpringBoot整合Elasticsearch 分布式搜索详解
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
1.安装Elasticsearch
Elasticsearch 基础语法使用:
创建索引,类型,文档:
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
查询所有索引:_cat/indices?v
get http://localhost:9200/_cat/indices?v
get http://localhost:9200/megacorp
删除索引:
delete http://localhost:9200/megacorp
轻量查询
GET http://localhost:9200/megacorp/_search
指定查询:
GET http://localhost:9200/megacorp/_search?q=title:小明
使用表达式查询:
GET /megacorp/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
全文搜索:
GET /megacorp/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
短语搜索:
GET /megacorp/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
高亮搜索:
GET /megacorp/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
about //查询字段名称
springBoot集成Elasticsearch
1.pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.yml配置:
spring:
elasticsearch:
rest:
uris: http://localhost:9200
username: elasticsearch
3.使用代码操作es
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
List<Book> findByName(String name);
}
@Autowired
BookRepository bookRepository;
/**
* 新建文档
*/
@Test
void contextLoads() {
Book book = new Book();
book.setId(1);
book.setName("小明");
bookRepository.index(book);
}
/**
* 查询数据
*/
@Test
void getBook(){
List<Book> byName = bookRepository.findByName("小明");
System.out.println(byName);
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/qq_41971087/article/details/116334454
粉丝福利:Java从入门到入土学习路线图
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈
评论