SpringBoot整合Elasticsearch 分布式搜索詳解
點擊上方藍色字體,選擇“標星公眾號”
優(yōu)質(zhì)文章,第一時間送達
1.安裝Elasticsearch
Elasticsearch 基礎語法使用:
創(chuàng)建索引,類型,文檔:
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);
}
/**
* 查詢數(shù)據(jù)
*/
@Test
void getBook(){
List<Book> byName = bookRepository.findByName("小明");
System.out.println(byName);
}
版權聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/qq_41971087/article/details/116334454
粉絲福利:Java從入門到入土學習路線圖
??????

??長按上方微信二維碼 2 秒
感謝點贊支持下哈 
評論
圖片
表情


