ElasticSearch学习
各个工具之间的版本对应 需要一样
ElasticSearch的安装
1、下载安装包 解压即可
2、解决跨域问题:在config中的elasticsearch.yml中添加:
http.cors.enabled: true
http.cors.allow-origin: "*"
复制代码
3、打开bin中的elasticsearch.bat文件
5、es中的索引就是一个数据库
ElasticSearch-head安装
1、下载 解压(数据展示工具)
2、在安装路径下 cnpm install
3、npm run start
Kibana安装
1、解压
2、汉化:
在E:\ElasticSearch\kibana-7.6.1-windows-x86_64\config\kibana.yml中添加
i18n.locale: "zh-CN"
复制代码
3、启动 kibana.bat
4、测试
ES核心概念
概述
es是面对文档的的! 一切都是json
Relational DB | ES |
---|---|
数据库 | 索引 |
表 | types(映射类型) |
行 | documents(一条条数据记录,es搜索的最小单位) |
字段 | fields |
物理设计
es在后台把每个索引分成多个分片,每个分片在集群中的不同服务器间迁移
默认的集群名字是elasticsearch
逻辑设计
一个索引类型中,包含多个文档。当索引一篇文档时,可以通过索引–>类型–>文档id 找到。id不必是整数,实际上是字符串。
倒排索引—提高检索效率的关键
原始数据 | 索引列表(倒排索引) | ||
---|---|---|---|
博客文章ID | 标签(token) | 标签 | 博客文章ID |
1 | python | python | 1,2,3 |
2 | python | linux | 3,4 |
3 | linux,python | ||
4 | linux |
IK分词器
将安装包解压到es中的plugins文件夹中
然后使用kibana测试
ik_max_word 最细粒度划分
ik_smart 最少切分
但是有问题:
这时 需要将詹晓楠手动加入到字典中。
在ik/config文件中添加dict(utf-8)
并在IKAnalyzer.cfg.xml中修改配置
然后重启es
Rest风格 关于索引的基本操作
1、创建一个索引,不指定类型的话 ,es会配置默认类型
PUT /索引名/~类型名~/文档id
{请求体}
复制代码
/2、只创建索引规则
3、获得索引信息
GET 索引名
复制代码
GET _cat 可以获得更多信息
复制代码
4、更新索引
POST /索引名/~类型名~/文档id/_update
复制代码
5、删除索引
DELETE 索引名
复制代码
关于文档的操作
简单搜索
GET 索引名/类型名/_search?q=查询条件
GET 索引名/类型名/_search?q=name:spb
复制代码
复杂搜索
GET 索引名/类型名/_search
{
"query":{
"match":{
//查询条件
"name": "狂神"
}
},
//结果过滤,只显示出来的字段
"_source":["name","desc"]
}
//全部查询
GET 索引名/类型名/_search
{
"query":{
"match_all":{
}
}
}
复制代码
==排序:==
==分页:==
==多条件查询must == and should == or must_not == !===
==过滤条件==
==精确查询:==
term: 直接通过倒排索引指定的词条进程精确查找的。(只需将match换为term)
match:会使用分词器解析。
两个类型 text(会被分词器分析 、进行拆分) 和 keyword(不会被分词器分析)
==高亮查询:==
集成SpringBoot
自定义版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
复制代码
对索引的操作:
ElasticSearchClientConfig.java
package com.spb.compare.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 ElasticSearchClientConfig {
//需要将对象注入到spring
//<beans id=restHighLevelClient class=RestHighLevelClient>
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1" , 9200 , "http")));
return client;
}
}
复制代码
测试类:
package com.spb.compare;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class CompareApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
//测试索引的创建 CreateIndexRequest
@Test
void testCreate() throws IOException {
//1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest("kuang_index");
//2、客户端执行创建请求 IndicesClient ,请求后获得响应
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.index());
}
//测试获取索引 GetIndexRequest , 判断是否存在
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("kuang_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//测试删除索引
@Test
void testDeleteIndex() throws IOException{
DeleteIndexRequest request = new DeleteIndexRequest("kuang_index");
//删除
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
//返回为真 则删除成功
System.out.println(delete.isAcknowledged());
}
@Test
void contextLoads() {
}
}
复制代码
对文档的操作
package com.spb.compare;
import com.alibaba.fastjson.JSON;
import com.spb.compare.entity.TestUser;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class CompareApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
//测试索引的创建 CreateIndexRequest
@Test
void testCreate() throws IOException {
//1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest("kuang_index");
//2、客户端执行创建请求 IndicesClient ,请求后获得响应
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.index());
}
//测试获取索引 GetIndexRequest , 判断是否存在
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("kuang_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//测试删除索引
@Test
void testDeleteIndex() throws IOException{
DeleteIndexRequest request = new DeleteIndexRequest("kuang_index");
//删除
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
//返回为真 则删除成功
System.out.println(delete.isAcknowledged());
}
//测试 添加文档
@Test
void testAddDocument() throws IOException {
//创建对象
TestUser testUser = new TestUser("狂神说" , 20);
//创建请求
IndexRequest request = new IndexRequest("kuang_index");
//规则 put kuang_index/_doc/id
request.id("1");
request.timeout("1s");
//数据放入请求 先转为json格式
IndexRequest source = request.source(JSON.toJSONString(testUser), XContentType.JSON);
//客户端发送请求
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status());
}
//获取文档 ,判断是否存在
@Test
void testIsExists() throws IOException {
GetRequest getRequest = new GetRequest("kuang_index", "1");
//不获取返回的_source的上下文了
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
//获取文档信息
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("kuang_index", "1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
//打印文档内容
System.out.println(getResponse.getSourceAsString());
System.out.println(getResponse);
}
//更新文档信息
@Test
void testUpdateDocument() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("kuang_index", "1");
updateRequest.timeout("1s");
TestUser testUser = new TestUser("苏鹏博", 18);
//更新规则
updateRequest.doc(JSON.toJSONString(testUser) , XContentType.JSON);
//执行
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
//删除文档信息
@Test
void testDeleteDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("kuang_index", "1");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
//批量插入数据
@Test
void testBulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<TestUser> userList = new ArrayList<>();
userList.add(new TestUser("spb1" , 3));
userList.add(new TestUser("spb2" , 3));
userList.add(new TestUser("spb3" , 3));
userList.add(new TestUser("spb4" , 3));
userList.add(new TestUser("spb5" , 3));
userList.add(new TestUser("spb6" , 3));
userList.add(new TestUser("spb7" , 3));
for (int i = 0; i < userList.size(); i++) {
//更新和删除 修改此处就行
bulkRequest.add(new IndexRequest("kuang_index")
.id(""+(i+1))
.source(JSON.toJSONString(userList.get(i)) , XContentType.JSON));
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());//返回false 即为成功
}
//查询
//searchRequest 搜索请求
//SearchSourceBuilder 条件构造
//HighlightBuilder 构件高亮
//TermQueryBuilder 精确查询
// MatchAllQueryBuilder 匹配所有
// xxx QueryBuilder 实现查询
@Test
void testSearch() throws IOException {
//搜索请求
SearchRequest searchRequest = new SearchRequest("kuang_index");
//构件搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件 可以使用QueryBuilders 工具来实现
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "spb1");
sourceBuilder.query(termQueryBuilder);
//分页
sourceBuilder.from();
sourceBuilder.size();
sourceBuilder.timeout(new TimeValue(60 , TimeUnit.SECONDS));
//提交条件到请求中
searchRequest.source(sourceBuilder);
//执行请求
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("=====================================");
for (SearchHit documentFields : searchResponse.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
@Test
void contextLoads() {
}
}
复制代码