关键词:
目录
一、pom文件依赖
-
引入相关依赖
<!-- elasticsearch 依赖 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch 的客户端 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch 依赖 2.x 的 log4j --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> <!-- junit 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
二、创建实体对象类
-
创建实体对象
package com.xz.esdemo.day2; import lombok.Data; @Data public class User private String name; private Integer age; private String sex;
三、文档操作代码示例
3.1、创建文档代码示例
-
创建文档代码示例
package com.xz.esdemo.day2; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.HttpHost; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; /** * @description: 创建文档 * @author: xz */ public class EsDocCreate public static void main(String[] args) throws Exception // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 IndexRequest indexRequest = new IndexRequest(); // 设置索引及唯一性标识 indexRequest.index("user").id("01"); // 创建数据对象 User user = new User(); user.setName("zhangsan"); user.setAge(20); user.setSex("男"); ObjectMapper objectMapper = new ObjectMapper(); String productJson = objectMapper.writeValueAsString(user); // 添加文档数据,数据格式为 JSON 格式 indexRequest.source(productJson, XContentType.JSON); // 客户端发送请求,获取响应对象 IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("_index:" + response.getIndex()); System.out.println("_id:" + response.getId()); System.out.println("_result:" + response.getResult()); // 关闭 es 客户端连接 client.close();
-
执行代码,查看控制台信息,如下图表示创建文档成功。
-
通过postman工具查看创建的文档信息,如下图所示:
3.2、修改文档代码示例
-
修改文档代码示例
package com.xz.esdemo.day2; import org.apache.http.HttpHost; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; /** * @description: 修改文档 * @author: xz */ public class EsDocUpdate public static void main(String[] args) throws Exception // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 UpdateRequest updateRequest = new UpdateRequest(); // 设置索引及唯一性标识 updateRequest.index("user").id("01"); // 设置请求体,对数据进行修改 updateRequest.doc(XContentType.JSON, "sex", "女"); // 客户端发送请求,获取响应对象 UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println("_index:" + response.getIndex()); System.out.println("_id:" + response.getId()); System.out.println("_result:" + response.getResult()); // 关闭 es 客户端连接 client.close();
-
执行代码,查看控制台信息,如下图表示创建文档成功。
-
通过postman工具查看创建的文档信息,如下图所示:
3.3、查询文档代码示例
-
查询文档代码示例
package com.xz.esdemo.day2; import org.apache.http.HttpHost; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; /** * @description: 查询文档 * @author: xz */ public class EsDocGet public static void main(String[] args) throws Exception // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 GetRequest getRequest = new GetRequest(); // 设置索引及唯一性标识 getRequest.index("user").id("01"); // 客户端发送请求,获取响应对象 GetResponse response = client.get(getRequest, RequestOptions.DEFAULT); System.out.println("_index:" + response.getIndex()); System.out.println("_type:" + response.getType()); System.out.println("_id:" + response.getId()); System.out.println("source:" + response.getSourceAsString()); // 关闭 es 客户端连接 client.close();
-
执行代码,查看控制台信息,如下图表示创建文档成功。
3.4、删除文档代码示例
-
删除文档代码示例
package com.xz.esdemo.day2; import org.apache.http.HttpHost; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; /** * @description: 删除文档 * @author: xz */ public class EsDocDelete public static void main(String[] args) throws Exception // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 DeleteRequest deleteRequest = new DeleteRequest(); // 设置索引及唯一性标识 deleteRequest.index("user").id("01"); // 客户端发送请求,获取响应对象 DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(response.toString()); // 关闭 es 客户端连接 client.close();
-
执行代码,查看控制台信息,如下图表示创建文档成功。
-
通过postman工具查看创建的文档信息,如下图所示表示删除文档成功:
elasticsearch入门——elasticsearch7.8.0版本数据格式
一、Elasticsearch7.8.0版本数据格式Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。Elasticsearch里存储文档数据和关系型数据库MySQL存储数据的概念进行一个类比,如下图:ES里的Index可以看做一个库ES里的Ty... 查看详情
elasticsearch入门——elasticsearch7.8.0版本和kibana7.8.0版本的下载安装(win10环境)(代码片段)
目录一、Elasticsearch7.8.0版本下载、安装1.1、官网下载地址1.2、下载步骤1.3、安装步骤(需要jdk11及以上版本支持)1.4、启动后,控制台中文乱码问题解决二、Node下载、安装(安装Kibana之前需要先安装Node)2.1、Node官网下... 查看详情
elasticsearch7.8.0版本入门——javaapi操作(索引操作)(代码片段)
目录一、创建索引代码示例二、查询索引代码示例二、删除索引代码示例一、创建索引代码示例创建索引代码示例packagecom.xz.esdemo.day1;importorg.apache.http.HttpHost;importorg.elasticsearch.client.RequestOptions;importorg.elasticsearch.client.RestClient;impor 查看详情
elasticsearch入门——elasticsearch7.8.0版本指定jdk11版本
目录一、背景二、Elasticsearch7.8.0版本指定JDK11版本步骤一、背景本地安装jdk1.8版本后,安装Elasticsearch7.8.0需要jdk11及以上版本支持二、Elasticsearch7.8.0版本指定JDK11版本步骤下载jdk11解压即用安装包,解压到具体磁盘目录在系... 查看详情
elasticsearch入门——elasticsearch7.8.0版本指定jdk11版本
目录一、背景二、Elasticsearch7.8.0版本指定JDK11版本步骤一、背景本地安装jdk1.8版本后,安装Elasticsearch7.8.0需要jdk11及以上版本支持二、Elasticsearch7.8.0版本指定JDK11版本步骤下载jdk11解压即用安装包,解压到具体磁盘目录在系... 查看详情
elasticsearch7.8.0版本入门——javaapi操作(环境准备)(代码片段)
目录一、创建springboot项目二、pom.xml文件引入相关maven依赖三、创建客户端对象一、创建springboot项目创建springboot项目步骤参考此博文链接:https://wwwxz.blog.csdn.net/article/details/91977374二、pom.xml文件引入相关maven依赖引入elasticsearc... 查看详情
elasticsearch7.8.0版本入门——javaapi操作(文档操作)(代码片段)
目录一、pom文件依赖二、创建实体对象类三、文档操作代码示例3.1、创建文档代码示例3.2、修改文档代码示例3.3、查询文档代码示例3.4、删除文档代码示例一、pom文件依赖引入相关依赖<!--elasticsearch依赖--><dependency><gro... 查看详情
elasticsearch7.8.0版本入门——javaapi操作(批量操作文档)(代码片段)
目录一、pom文件依赖二、批量操作文档代码示例2.1、批量创建文档代码示例2.2、批量删除文档代码示例一、pom文件依赖引入相关依赖<!--elasticsearch依赖--><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsea... 查看详情
elasticsearch7.8.0版本入门——集群部署(linux环境-centos7)(代码片段)
目录一、三台服务器信息二、Elasticsearch7.8.0单机部署三、Elasticsearch7.8.0集群部署3.1、分别再三台服务器中都安装Elasticsearch7.8.03.2、修改192.168.136.23服务器中elasticsearch配置文件3.2、修改192.168.136.24服务器中elasticsearch配置文件3.3、修... 查看详情
elasticsearch入门——elasticsearch7.8.0版本索引操作(代码片段)
目录一、创建索引1.1、创建索引概述1.2、创建索引示例1.3、重复创建索引示例二、查看索引2.1、查看所有索引2.2、查看单个索引三、删除索引3.1、删除索引一、创建索引1.1、创建索引概述对比关系型数据库,创建索引就等同... 查看详情
elasticsearch入门——elasticsearch7.8.0版本文档操作(代码片段)
目录一、文档概述二、创建文档示例2.1、创建文档(生成随机id)2.2、创建文档(自定义唯一性标识)三、查看文档示例3.1、根据主键查看文档3.2、查看所有文档四、修改文档示例4.1、全局修改文档4.1、局部修改文档五、删除文档示例... 查看详情
elasticsearch入门——elasticsearch7.8.0版本文档操作(代码片段)
目录一、文档概述二、创建文档示例2.1、创建文档(生成随机id)2.2、创建文档(自定义唯一性标识)三、查看文档示例四、修改文档示例五、修改字段示例六、删除文档示例6.1、根据文档的唯一性标识删除文档6.2、条件删除文档一、... 查看详情
elasticsearch入门——elasticsearch7.8.0版本和kibana7.8.0版本的下载安装(win10环境)(代码片段)
目录一、Elasticsearch7.8.0版本下载、安装1.1、官网下载地址1.2、下载步骤1.3、安装步骤(需要jdk11及以上版本支持)1.4、启动后,控制台中文乱码问题解决二、Node下载、安装(安装Kibana之前需要先安装Node)2.1、Node官网下... 查看详情
elasticsearch7.8.0版本入门——javaapi操作(索引操作)(代码片段)
目录一、创建索引代码示例二、查询索引代码示例二、删除索引代码示例一、创建索引代码示例创建索引代码示例packagecom.xz.esdemo.day1;importorg.apache.http.HttpHost;importorg.elasticsearch.client.RequestOptions;importorg.elasticsearch.client.RestClient;impor... 查看详情
elasticsearch7.8.0版本入门——elasticsearch7.8.0映射操作(代码片段)
目录一、映射的概述二、创建映射的示例2.1、首先,创建索引2.2、然后,再创建好的索引基础上,创建映射2.3、映射属性说明2.4、查看创建的映射2.5、最后,创建文档2.6、根据文档中name属性条件查询文档理解映射... 查看详情
elasticsearch7.8.0docker安装及入门最基本操作(代码片段)
Docker安装ElasticSearch拉取镜像#最新版本7,8.0dockerpullelasticsearch:7.8.0启动集群#基本启动命令#-e"discovery.type=single-node"单节点集群#-eES_JAVA_OPTS="-Xms512m-Xmx512m"制定运行参数,不然如果机器内存太小,启动后会非常卡顿#--name 查看详情
elasticsearch7.8.0版本入门——elasticsearch-head插件的安装(win10环境)(代码片段)
目录一、elasticsearch-head插件的介绍二、Elasticsearch7.8.0及Node安装三、Grunt安装四、elasticsearch-head插件的下载五、elasticsearch-head插件的安装六、访问elasticsearch-head插件一、elasticsearch-head插件的介绍elasticsearch-head是用于监控Elasticsearch... 查看详情
elasticsearch7.8.0版本入门——javaapi操作(环境准备)(代码片段)
目录一、创建springboot项目二、pom.xml文件引入相关maven依赖三、创建客户端对象一、创建springboot项目创建springboot项目步骤参考此博文链接:https://wwwxz.blog.csdn.net/article/details/91977374二、pom.xml文件引入相关maven依赖引入elasticsearc... 查看详情