网站的ui规范,seo电商,自己建的网站有乱码,网站排名软件利搜在企业环境中#xff0c;Elasticsearch的索引CRUD#xff08;创建Create、读取Read、更新Update、删除Delete#xff09;操作是非常基础且频繁使用的功能。这些操作对于管理和维护数据至关重要#xff0c;尤其是在处理大规模数据集和需要实时搜索与分析的应用场景中。 目录… 在企业环境中Elasticsearch的索引CRUD创建Create、读取Read、更新Update、删除Delete操作是非常基础且频繁使用的功能。这些操作对于管理和维护数据至关重要尤其是在处理大规模数据集和需要实时搜索与分析的应用场景中。 目录
Mapping映射属性
索引库的CRUD
基于Kibana的DevTools
创建索引库和映射 查询索引库
修改索引库
删除索引库
基于Java客户端Java REST Client
初始化RestHighLevelClient
准备索引映射
创建索引库
删除索引库
查询索引库 在Elasticsearch中Index就类似数据库表Mapping映射就类似表的结构。我们要向es中存储数据必须先创建Index和Mapping。
Mapping映射属性
Mapping是对索引库中文档的约束常见的Mapping属性包括
字段数据类型常见的简单类型有
1.字符串text可分词的文本、keyword精确值例如品牌、国家、ip地址
2.数值long、integer、short、byte、double、float
3.布尔boolean
4.日期date
5.对象object index是否创建索引默认为true
6.analyzer使用哪种分词器
7.properties该字段的子字段信息 举例说明下面的json文档
{age: 21,weight: 52.1,isMarried: false,info: 谢谢欣赏x,email: zy123.cn,score: [99.1, 99.5, 98.9],name: {firstName: 云,lastName: 赵}
}
对应的每个字段映射Mapping 索引库的CRUD
基于Kibana的DevTools
创建索引库和映射
# 创建索引库
PUT /user
{mappings: {properties: {info:{type: text,analyzer: ik_smart,index: true},age:{type: byte},email:{type: keyword,index: false},name:{type: object,properties: {firstName:{type: keyword},lastName:{type: keyword}}}}}
}实现效果如下 查询索引库
GET /user
实现效果如下 修改索引库
倒排索引结构虽然不复杂但是一旦数据结构改变比如改变了分词器就需要重新创建倒排索引这简直是灾难。因此索引库一旦创建无法修改mapping。
但是允许添加新的字段到mapping中
# 修改索引库
PUT /user/_mapping
{properties: {id: {type: byte}}
}
实现效果如下 删除索引库
删除完后再查询
# 删除索引库
DELETE /user# 查询索引库
GET /user实现效果如下 说明删除成功
基于Java客户端Java REST Client
ES官方提供了各种不同语言的客户端用来操作ES。这些客户端的本质就是组装DSL语句通过http请求发送给ES。
新增一个测试项目es-demo就一个普通的spring boot项目
初始化RestHighLevelClient
在pom.xml添加es的依赖 !-- es 依赖--dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.12.1/version/dependencydependencygroupIdorg.elasticsearch/groupIdartifactIdelasticsearch/artifactIdversion7.12.1/version/dependency
在elasticsearch提供的API中与elasticsearch一切交互都封装在一个名为RestHighLevelClient的类中必须先完成这个对象的初始化建立与elasticsearch的连接。 private RestHighLevelClient client;/*** 创建ES客户端*/BeforeEachvoid setUp() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://自己的虚拟机地址:9200)));}/*** 关闭ES客户端* throws IOException*/AfterEachvoid tearDown() throws IOException {if (client ! null) {client.close();}}/*** 测试连接*/Testvoid testConnection() {System.out.println(client client);}
运行测试连接效果如下 解读连接ES客户端成功 接下来对索引的CRUD是需要通过对映射来实现的。
准备索引映射
PUT /items
{mappings: {properties: {id: {type: keyword},name:{type: text,analyzer: ik_max_word},price:{type: integer},stock:{type: integer},image:{type: keyword,index: false},category:{type: keyword},brand:{type: keyword},sold:{type: integer},commentCount:{type: integer,index: false},isAD:{type: boolean},updateTime:{type: date}}}
}
基于Java客户端的CRUD代码基本分为三步
1.创建Request对象。因为是创建索引库的操作因此Request是CreateIndexRequest。
2.添加请求参数。其实就是Json格式的Mapping映射参数。因为json字符串很长这里是定义了静态字符串常量MAPPING_TEMPLATE让代码看起来更加优雅。
3.发送请求。client.indices()方法的返回值是IndicesClient类型封装了所有与索引库操作有关的方法。例如创建索引、删除索引、判断索引是否存在等。
创建索引库 /*** 创建索引* throws IOException*/Testvoid testCreateIndex() throws IOException {// 1.创建Request对象CreateIndexRequest request new CreateIndexRequest(items);// 2.准备请求参数request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3.发送请求client.indices().create(request, RequestOptions.DEFAULT);}为了验证索引是否创建成功创建一个方法 /*** 判断索引是否存在* throws IOException*/Testvoid testGetIndex() throws IOException {// 1.创建Request对象GetIndexRequest request new GetIndexRequest(items);// 2.发送请求boolean exists client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(存在索引 exists);}
实现效果如下 items索引创建成功 删除索引库 /*** 删除索引* throws IOException*/Testvoid testDeleteIndex() throws IOException {// 1.创建Request对象DeleteIndexRequest request new DeleteIndexRequest(items);// 2.发送请求client.indices().delete(request, RequestOptions.DEFAULT);}
调用前面的判断索引是否存在方法实现效果如下 删除索引items成功 查询索引库
就是修改判断索引是否存在方法主要返回内容一大堆。 /*** 查询索引* throws IOException*/Testvoid testQueryIndex() throws IOException {// 1.创建Request对象GetIndexRequest request new GetIndexRequest(items);// 2.发送请求GetIndexResponse getIndexResponse client.indices().get(request, RequestOptions.DEFAULT);System.out.println(getIndexResponse getIndexResponse);}实现效果如下 查询索引items成功 注意开发项目使用的是基于基于Java客户端Java REST Client来开发的。基于Kibana的DevTools一般用来测试。