準(zhǔn)備工作
準(zhǔn)備一個(gè)空的SpringBoot項(xiàng)目
寫入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
注意你的SpringBoot和你的es版本,一定要對應(yīng),如果不知道的可以查看這篇文章:https://blog.csdn.net/u014641168/article/details/130386872
我的版本是2.2.6,所以用的ES版本是 6.8.12,安裝es請看這篇文章:https://blog.csdn.net/u014641168/article/details/130622430
查看ES版本
配置
創(chuàng)建ES配置文件,下面有2個(gè)Bean,一個(gè)是你的ES有賬號密碼的,另一個(gè)默認(rèn)是沒有的。
package cn.ityao.es.config;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author tongyao
*/
@Configuration
public class ElasticsearchConfig {
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.ip}")
private String ip;
@Value("${elasticsearch.username}")
private String username;
@Value("${elasticsearch.password}")
private String password;
/**
* 創(chuàng)建帶HTTP Basic Auth認(rèn)證rest客戶端
*/
@Bean
public RestHighLevelClient restHighLevelClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
return new RestHighLevelClient(RestClient.builder(
new HttpHost[]{
new HttpHost(ip, port, "http")
}).setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider)));
}
//不帶用戶名密碼驗(yàn)證
//@Bean
public RestHighLevelClient restClient() {
return new RestHighLevelClient(RestClient.builder(new HttpHost[]{
new HttpHost(ip, port, "http")
}));
}
}
yml配置文件內(nèi)容
server:
# 服務(wù)端口
port: 9990
elasticsearch:
port: 9200
ip: 127.0.0.1
username: elastic
password: 123456
# 查看es信息時(shí)需要的序列化
spring:
jackson:
serialization:
FAIL_ON_EMPTY_BEANS: false
注入依賴
在controller下注入依賴
@Autowired
private RestHighLevelClient restHighLevelClient;
對索引的CURD
1、創(chuàng)建索引
/**
* 創(chuàng)建索引
*
* @return
* @throws IOException
*/
@GetMapping("/createIndex")
public Object createIndex() throws IOException {
//1.創(chuàng)建索引請求
CreateIndexRequest request = new CreateIndexRequest("testindex");
//2.客戶端執(zhí)行請求IndicesClient,執(zhí)行create方法創(chuàng)建索引,請求后獲得響應(yīng)
CreateIndexResponse response =
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
return response;
}
可以看到已經(jīng)添加成功了,但是一定注意,索引名稱,一定要小寫!
2、查詢索引
/**
* 查詢索引
*
* @return
* @throws IOException
*/
@GetMapping("/searchIndex")
public Object searchIndex() throws IOException {
//1.查詢索引請求
GetIndexRequest request = new GetIndexRequest("testindex");
//2.執(zhí)行exists方法判斷是否存在
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
return exists;
}
3、刪除索引
/**
* 刪除索引
*
* @return
* @throws IOException
*/
@GetMapping("delIndex")
public Object delIndex() throws IOException {
//1.刪除索引請求
DeleteIndexRequest request = new DeleteIndexRequest("testindex");
//執(zhí)行delete方法刪除指定索引
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
return delete.isAcknowledged();
}
對文檔的CRUD
1、新增文檔
注意:如果添加時(shí)不指定文檔ID,他就會(huì)隨機(jī)生成一個(gè)ID,ID唯一。
/**
* 新增文檔
*
* @return
* @throws IOException
*/
@GetMapping("/add")
public Object add() throws IOException {
//1.創(chuàng)建對象
User user = new User("張三", 21);
//2.創(chuàng)建請求(索引的名字)
IndexRequest request = new IndexRequest("indexdocument");
//3.設(shè)置規(guī)則 PUT /ljx666/_doc/1
//設(shè)置文檔id=6,設(shè)置超時(shí)=1s等,不設(shè)置會(huì)使用默認(rèn)的
//同時(shí)支持鏈?zhǔn)骄幊倘?request.id("6").timeout("1s");
request.id("6");
// 指定要寫入的 Index
request.type("_doc");
/*request.index("test");*/
/*request.timeout(TimeValue.timeValueSeconds(1));*/
request.timeout("1s");
//4.將數(shù)據(jù)放入請求,要將對象轉(zhuǎn)化為json格式
//XContentType.JSON,告訴它傳的數(shù)據(jù)是JSON類型
request.source(JSON.toJSONString(user), XContentType.JSON);
//5.客戶端發(fā)送請求,獲取響應(yīng)結(jié)果
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
return indexResponse;
}
2、查詢文檔中的數(shù)據(jù)
/**
* 獲取文檔中的數(shù)據(jù)
*
* @return
* @throws IOException
*/
@GetMapping("/get")
public Object get() throws IOException {
//1.創(chuàng)建請求,指定索引、文檔id(索引的名字)
GetRequest request = new GetRequest("indexdocument").id("6").type("_doc");
GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(getResponse);//獲取響應(yīng)結(jié)果
//getResponse.getSource() 返回的是Map集合
System.out.println(getResponse.getSourceAsString());//獲取響應(yīng)結(jié)果source中內(nèi)容,轉(zhuǎn)化為字符串
return getResponse;
}
3、更新文檔中的數(shù)據(jù)
注意:需要將User對象中的屬性全部指定值,不然會(huì)被設(shè)置為空,如User只設(shè)置了名稱,那么只有名稱會(huì)被修改成功,其他會(huì)被修改為null。
/**
* 更新文檔數(shù)據(jù)
*
* @return
* @throws IOException
*/
@GetMapping("/update")
public Object update() throws IOException {
//1.創(chuàng)建請求,指定索引、文檔id(索引的名字)
UpdateRequest request = new UpdateRequest("indexdocument","_doc","6");
User user = new User("小明", 21);
//將創(chuàng)建的對象放入文檔中
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());//更新成功返回OK
return updateResponse;
}
3、刪除文檔中的數(shù)據(jù)
/**
* 刪除文檔數(shù)據(jù)
*
* @return
* @throws IOException
*/
@GetMapping("/delete")
public Object delete() throws IOException {
//1.創(chuàng)建刪除文檔請求
DeleteRequest request = new DeleteRequest("indexdocument").id("6").type("_doc");
DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());//更新成功返回OK
return deleteResponse;
}
4、批量新增文檔中的數(shù)據(jù)
/**
* 批量新增文檔數(shù)據(jù)
*
* @return
* @throws IOException
*/
@GetMapping("/addBatch")
public Object addBatch() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
//設(shè)置超時(shí)
bulkRequest.timeout("10s");
List<User> list = new ArrayList<>();
list.add(new User("李四", 25));
list.add(new User("王五", 18));
list.add(new User("趙六", 30));
list.add(new User("田七", 26));
list.add(new User("劉八", 20));
int id = 1;
//批量處理請求
for (User user : list) {
//不設(shè)置id會(huì)生成隨機(jī)id
bulkRequest.add(new IndexRequest("indexdocument")
.id("" + (id++))
.type("_doc")
.source(JSON.toJSONString(user), XContentType.JSON));
}
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());//是否執(zhí)行失敗,false為執(zhí)行成功
return bulkResponse;
}
5、詢所有、模糊查詢、分頁查詢、排序、高亮顯示
/**
* 復(fù)雜的es查詢
* @return
* @throws IOException
*/
@GetMapping("test")
public Object test() throws IOException {
SearchRequest searchRequest = new SearchRequest("indexdocument");//里面可以放多個(gè)索引
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//構(gòu)造搜索條件
//此處可以使用QueryBuilders工具類中的方法
//1.查詢所有
sourceBuilder.query(QueryBuilders.matchAllQuery());
//2.查詢name中含有Java的
sourceBuilder.query(QueryBuilders.multiMatchQuery("張三", "name"));
//3.分頁查詢
sourceBuilder.from(0).size(5);
//4.按照score正序排列
sourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC));
//5.按照id倒序排列(score會(huì)失效返回NaN)
sourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.DESC));
//6.給指定字段加上指定高亮樣式
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("name").preTags("<span style='color:red;'>").postTags("</span>");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//獲取總條數(shù)
System.out.println(searchResponse.getHits().getTotalHits());
//輸出結(jié)果數(shù)據(jù)(如果不設(shè)置返回條數(shù),大于10條默認(rèn)只返回10條)
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
System.out.println("分?jǐn)?shù):" + hit.getScore());
Map<String, Object> source = hit.getSourceAsMap();
System.out.println("index->" + hit.getIndex());
System.out.println("id->" + hit.getId());
for (Map.Entry<String, Object> s : source.entrySet()) {
System.out.println(s.getKey() + "--" + s.getValue());
}
}
return searchResponse;
}
總結(jié)
1.大致流程
創(chuàng)建對應(yīng)的請求 --> 設(shè)置請求(添加規(guī)則,添加數(shù)據(jù)等) --> 執(zhí)行對應(yīng)的方法(傳入請求,默認(rèn)請求選項(xiàng))–> 接收響應(yīng)結(jié)果(執(zhí)行方法返回值)–> 輸出響應(yīng)結(jié)果中需要的數(shù)據(jù)(source,status等)
2.注意事項(xiàng)
如果不指定id,會(huì)自動(dòng)生成一個(gè)隨機(jī)id
正常情況下,不應(yīng)該這樣使用new IndexRequest(“indexName”),如果索引發(fā)生改變了,那么代碼都需要修改,可以定義一個(gè)枚舉類或者一個(gè)專門存放常量的類,將變量用final static等進(jìn)行修飾,并指定索引值。其他地方引用該常量即可,需要修改也只需修改該類即可。
elasticsearch相關(guān)的東西,版本都必須一致,不然會(huì)報(bào)錯(cuò)
elasticsearch很消耗內(nèi)存,建議在內(nèi)存較大的服務(wù)器上運(yùn)行elasticsearch,否則會(huì)因?yàn)閮?nèi)存不足導(dǎo)致elasticsearch自動(dòng)killed文章來源:http://www.zghlxwxcb.cn/news/detail-478179.html
文章參考:https://blog.csdn.net/zhiyikeji/article/details/128902860文章來源地址http://www.zghlxwxcb.cn/news/detail-478179.html
到了這里,關(guān)于SpringBoot 整合ElasticSearch實(shí)現(xiàn)模糊查詢,批量CRUD,排序,分頁,高亮的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!