1.Elaticsearch概述
1.1Elaticsearch是什么
The Elastic Stack, 包括 Elasticsearch、Kibana、Beats 和 Logstash(也稱為 ELK Stack)。能夠安全可靠地獲取任何來源著角、任何格式的數(shù)據(jù),然后實時地對數(shù)據(jù)進行搜索、分析和可視化捡硅。Elaticsearch,簡稱為ES盗棵, ES是一個開源的高擴展的分布式全文搜索引擎壮韭,是整個Elastic Stack技術(shù)棧的核心北发。它可以近乎實時的存儲、檢索數(shù)據(jù)喷屋;本身擴展性很好琳拨,可以擴展到上百臺服務(wù)器,處理PB級別的數(shù)據(jù)屯曹。
1.2Elaticsearch And Solr
Elasticsearch和Solr,這兩款都是基于Lucene搭建的狱庇,可以獨立部署啟動的搜索引擎服務(wù)軟件。由于內(nèi)核相同是牢,所以兩者除了服務(wù)器安裝僵井、部署、管理驳棱、集群以外批什,對于數(shù)據(jù)的操作 修改、添加社搅、保存驻债、查詢等等都十分類似。
1.3Elaticsearch的應(yīng)用案例
GitHub形葬、維基百科合呐、SoundCloud、百度笙以、新浪淌实、阿里、Stack Overflow
2.相關(guān)軟件的下載
2.1Elaticsearch7.6.x版本下載
在elasticsearch-7.6.1/config/elasticsearch.yml配置文件加上xpack.ml.enabled: false猖腕;則可進入到bin目錄拆祈,./elasticsearch啟動Elaticsearch。
新開一個黑窗口輸入:curl localhost:9200倘感,收到如下信息說明啟動成功放坏。
默認(rèn)只允許本機訪問,如果想遠(yuǎn)程訪問可以修改config/elasticsearch.yml老玛,去掉network.host的注釋淤年,將它的值改成0.0.0.0,然后重新啟動Elasticsearch 蜡豹。
2.2Head插件
打開谷歌瀏覽器搜索谷歌商城麸粮,打開搜索elasticsearch head即可看見如下:
點擊添加至chrome,即可在擴展程序找到打開elasticsearch head余素。
2.3 Kibana7.6.1
打開kibana.yml配置文件豹休,將 i18n.locale: "en"-->i18n.locale: "zh-CN",可以將kibana漢化桨吊。進入bin目錄威根,./kibana啟動凤巨。
2.4 ik分詞器
將下載好的ik文件放到elasticsearch的plugins文件夾下面,重啟elasticsearch即可生效洛搀。
3.springboot-2.2.5整合ES-7.6.1
3.1依賴
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!--<!– 自定義es版本依賴敢茁,保證和本地一致–>-->
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
3.2配置ES連接
package com.sanzhu.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
}
}
3.3索引的CRUD
//1.測試索引的創(chuàng)建
@Test
public void test01() throws IOException {
//1.創(chuàng)建索引請求
CreateIndexRequest request = new CreateIndexRequest("sanzhu_index");
//2.執(zhí)行創(chuàng)建請求
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("response.index() = " + response.index());
}
//2.獲取索引
@Test
public void text02() throws IOException {
//1.獲取索引請求
GetIndexRequest request = new GetIndexRequest("sanzhu_index");
boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
System.out.println("exists = " + exists);
}
//3.刪除索引
@Test
public void test03() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("sanzhu_index");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println("response.isAcknowledged() = " + response.isAcknowledged());
}
3.4文檔的CRUD
//4.添加文檔
@Test
public void text04() throws IOException {
User user = new User();
user.setName("陳輝");
user.setAge(18);
IndexRequest request = new IndexRequest("sanzhu_index");
// 添加數(shù)據(jù)的規(guī)則 PUT /sanzhu_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
//添加數(shù)據(jù)進索引庫
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println("response.status() = " + response.status());
System.out.println("response.toString() = " + response.toString());
}
//5.判斷文檔是否存在
@Test
public void test05() throws IOException {
GetRequest getRequest = new GetRequest("sanzhu_index", "1");
getRequest.fetchSourceContext();
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println("exists = " + exists);
}
//6.獲取文檔信息
@Test
public void test06() throws IOException {
GetRequest getRequest = new GetRequest("sanzhu_index", "1");
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("documentFields.getSourceAsString() = " + documentFields.getSourceAsString());
System.out.println("documentFields = " + documentFields);
}
//7.更新文檔信息
@Test
public void test07() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("sanzhu_index", "1");
updateRequest.timeout("1s");
User user = new User();
user.setName("張家輝");
user.setAge(1918);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("update.status() = " + update.status());
System.out.println("update.toString() = " + update.toString());
}
//8.刪除文檔請求
@Test
public void test08() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("sanzhu_index", "1");
deleteRequest.timeout();
DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("response.status() = " + response.status());
System.out.println("response.toString() = " + response.toString());
}
//9.批量插入數(shù)據(jù)
@Test
public void test09() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setName("張家輝"+i+"號");
user.setAge(i+20);
users.add(user);
}
for (int i = 0; i < users.size(); i++) {
bulkRequest.add(
new IndexRequest("sanzhu_index").id("" + (i + 2))
.source(JSON.toJSONString(users.get(i)), XContentType.JSON)
);
}
BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("responses.status() = " + responses.status());
System.out.println("responses.hasFailures() = " + responses.hasFailures());
}
//單條件查詢
@Test
public void test14() throws IOException {
SearchRequest searchRequest = new SearchRequest("sanzhu_index");
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.timeout(TimeValue.timeValueSeconds(2));
//過濾字段
builder.fetchSource("name","age");
//分頁
builder.from(0);
builder.size(3);
//排序
builder.sort("_id", SortOrder.DESC);
//條件查詢
TermQueryBuilder query = QueryBuilders.termQuery("age", "99");
builder.query(query);
searchRequest.source(builder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
System.out.println("hit = " + hit);
System.out.println("hit = " + hit.getSourceAsMap());
}
}
// 多條件查詢
@Test
public void test15() throws IOException{
SearchRequest searchRequest = new SearchRequest("sanzhu_index");
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.timeout(TimeValue.timeValueSeconds(2));
//多條件查詢器
BoolQueryBuilder query = QueryBuilders.boolQuery();
//必須滿足
query.must(QueryBuilders.matchQuery("name","peter"));
//必須不滿足
query.mustNot(QueryBuilders.matchQuery("age","999"));
//或者 should查詢和must查詢一起用會無效
// query.should(QueryBuilders.matchQuery("age","777"));
// query.should(QueryBuilders.matchQuery("age","888"));
builder.query(query);
searchRequest.source(builder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
System.out.println("hit = " + hit);
}
}
//范圍查詢
@Test
public void test16() throws Exception{
SearchRequest searchRequest = new SearchRequest("sanzhu_index");
SearchSourceBuilder builder = new SearchSourceBuilder();
RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age").gte(200).lte(666);
builder.query(queryBuilder);
searchRequest.source(builder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
System.out.println("hit = " + hit);
}
}
//高亮查詢
@Test
public void test13() throws IOException {
SearchRequest searchRequest = new SearchRequest("sanzhu_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder query = QueryBuilders.termQuery("name", "peter");
searchSourceBuilder.query(query);
searchSourceBuilder.timeout(TimeValue.timeValueSeconds(1));
searchSourceBuilder.from(1);
searchSourceBuilder.size(5);
searchRequest.source(searchSourceBuilder);
//高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("<span style='color:red'>");
highlightBuilder.postTags("</span>");
highlightBuilder.requireFieldMatch(false);//關(guān)閉多個高亮,只第一個高亮
searchSourceBuilder.highlighter(highlightBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
System.out.println("hits.length = " + hits.length);
for (SearchHit hit : hits) {
//獲取高亮字段
Map<String, HighlightField> map = hit.getHighlightFields();
HighlightField name = map.get("name");
Map<String, Object> map1 = hit.getSourceAsMap();
//用高亮的字段替換原來的字段
if (name!=null){
Text[] fragments = name.fragments();//獲取高亮內(nèi)容
String n_name = "";
for (Text fragment : fragments) {
n_name+=fragment;
}
map1.put("name",n_name);
}
System.out.println("map1 = " + map1);
}