共计 8245 个字符,预计需要花费 21 分钟才能阅读完成。
1.Elaticsearch 概述
1.1Elaticsearch 是什么
The Elastic Stack, 包含 Elasticsearch、Kibana、Beats 和 Logstash(也称为 ELK Stack)。可能安全可靠地获取任何起源、任何格局的数据,而后实时地对数据进行搜寻、剖析和可视化。Elaticsearch,简称为 ES,ES 是一个开源的高扩大的分布式全文搜索引擎,是整个 Elastic Stack 技术栈的外围。它能够近乎实时的存储、检索数据;自身扩展性很好,能够扩大到上百台服务器,解决 PB 级别的数据。
1.2Elaticsearch And Solr
Elasticsearch 和 Solr, 这两款都是基于 Lucene 搭建的,能够独立部署启动的搜索引擎服务软件。因为内核雷同,所以两者除了服务器装置、部署、治理、集群以外,对于数据的操作 批改、增加、保留、查问等等都非常相似。
1.3Elaticsearch 的利用案例
GitHub、维基百科、SoundCloud、百度、新浪、阿里、Stack Overflow
2. 相干软件的下载
2.1Elaticsearch7.6.x 版本下载
在 elasticsearch-7.6.1/config/elasticsearch.yml 配置文件加上 xpack.ml.enabled: false;则可进入到 bin 目录,./elasticsearch 启动 Elaticsearch。
新开一个黑窗口输出:curl localhost:9200,收到如下信息阐明启动胜利。
默认只容许本机拜访,如果想近程拜访能够批改 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.4ik 分词器
将下载好的 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. 测试索引的创立
@Test
public void test01() throws IOException {
//1. 创立索引申请
CreateIndexRequest request = new CreateIndexRequest("sanzhu_index");
//2. 执行创立申请
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");
// 增加数据的规定 PUT /sanzhu_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
// 增加数据进索引库
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. 批量插入数据
@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);// 敞开多个高亮,只第一个高亮
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();// 获取高亮内容
String n_name = "";
for (Text fragment : fragments) {n_name+=fragment;}
map1.put("name",n_name);
}
System.out.println("map1 =" + map1);
}