Elasticsearch8.1-ElasticsearchClient-Java客户端简单增删查改-随笔-
时间:2022-05-19 作者:daimaoblog
- 环境准备
- Springboot 基本环境
- 自行前往https://域名/ 构建一个即可
- Elasticsearch服务端
- 简单说下windows版本的安装 https://域名/cn/downloads/elasticsearch 下载最新版8.1.0,之后解压 不要放到中文目录下,进入bin目录双击运行域名.
- Springboot 基本环境
- 相关依赖
-
域名 ELasticsearch相关的依赖
<dependency> <groupId>域名nts</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.1.0</version> </dependency> <dependency> <groupId>域名域名</groupId> <artifactId>jackson-databind</artifactId> <version>域名.2</version> </dependency>
- 域名
elasticsearch: host: 127.0.0.1 port: 9200
-
- 代码逻辑解释
- 创建Elasticsearch 客户端 的bean 对象
查看代码
package 域名域名; import 域名域名ticsearchClient; import 域名域名域名sonJsonpMapper; import 域名域名ticsearchTransport; import 域名域名域名ClientTransport; import 域名.HttpHost; import 域名域名Client; import 域名域名ClientBuilder; import 域名域名域名e; import 域名域名; import 域名域名onent; @Component public class ElasticClient { @Value("${域名}") private String host; @Value("${域名}") private Integer port; @Bean public ElasticsearchClient getClient(){ RestClientBuilder builder = 域名der( new HttpHost(host, port)); ElasticsearchTransport transport = new RestClientTransport(域名d(),new JacksonJsonpMapper()); return new ElasticsearchClient(transport); } }
- 准备测试对象
- 创建测试商品表
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_product -- ---------------------------- DROP TABLE IF EXISTS `t_product`; CREATE TABLE `t_product` ( `product_id` bigint(0) NOT NULL AUTO_INCREMENT, `product_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT \'商品名称\', `product_price` decimal(10, 2) NULL DEFAULT NULL COMMENT \'商品价格\', `product_category` bigint(0) NULL DEFAULT NULL COMMENT \'商品分类\', `product_imgurl` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT \'商品图片地址\', `product_num` int(0) NULL DEFAULT NULL COMMENT \'商品库存\', `product_description` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT \'商品简单描述\', `sale` bigint(0) NULL DEFAULT NULL COMMENT \'销量\', `quill` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT \'详细描述\', `product_status` int(0) NULL DEFAULT NULL COMMENT \'状态\', `create_time` datetime(0) NULL DEFAULT NULL COMMENT \'创建时间\', `create_id` bigint(0) NULL DEFAULT NULL COMMENT \'创建人\', PRIMARY KEY (`product_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
2.使用mybatis-plus 生成商品相应对象、service、mapper等
<dependency> <groupId>域名idou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency>
查看代码
package 域名域名in; import 域名域名pe; import 域名域名eField; import 域名域名eId; import 域名域名eName; import 域名alizable; import 域名ecimal; import 域名; import 域名; /** * * @TableName t_product */ @TableName(value ="t_product") @Data public class TProduct implements Serializable { /** * */ @TableId(type = 域名) private Long productId; /** * 商品名称 */ private String productName; /** * 商品价格 */ private BigDecimal productPrice; /** * 商品分类 */ private Long productCategory; /** * 商品图片地址 */ private String productImgurl; /** * 商品库存 */ private Integer productNum; /** * 商品简单描述 */ private String productDescription; /** * 销量 */ private Long sale; /** * 详细描述 */ private String quill; /** * 状态 */ private Integer productStatus; /** * 创建时间 */ private Date createTime; /** * 创建人 */ private Long createId; @TableField(exist = false) private static final long serialVersionUID = 1L; @Override public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != 域名lass()) { return false; } TProduct other = (TProduct) that; return (域名roductId() == null ? 域名roductId() == null : 域名roductId().equals(域名roductId())) && (域名roductName() == null ? 域名roductName() == null : 域名roductName().equals(域名roductName())) && (域名roductPrice() == null ? 域名roductPrice() == null : 域名roductPrice().equals(域名roductPrice())) && (域名roductCategory() == null ? 域名roductCategory() == null : 域名roductCategory().equals(域名roductCategory())) && (域名roductImgurl() == null ? 域名roductImgurl() == null : 域名roductImgurl().equals(域名roductImgurl())) && (域名roductNum() == null ? 域名roductNum() == null : 域名roductNum().equals(域名roductNum())) && (域名roductDescription() == null ? 域名roductDescription() == null : 域名roductDescription().equals(域名roductDescription())) && (域名ale() == null ? 域名ale() == null : 域名ale().equals(域名ale())) && (域名uill() == null ? 域名uill() == null : 域名uill().equals(域名uill())) && (域名roductStatus() == null ? 域名roductStatus() == null : 域名roductStatus().equals(域名roductStatus())) && (域名reateTime() == null ? 域名reateTime() == null : 域名reateTime().equals(域名reateTime())) && (域名reateId() == null ? 域名reateId() == null : 域名reateId().equals(域名reateId())); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((getProductId() == null) ? 0 : getProductId().hashCode()); result = prime * result + ((getProductName() == null) ? 0 : getProductName().hashCode()); result = prime * result + ((getProductPrice() == null) ? 0 : getProductPrice().hashCode()); result = prime * result + ((getProductCategory() == null) ? 0 : getProductCategory().hashCode()); result = prime * result + ((getProductImgurl() == null) ? 0 : getProductImgurl().hashCode()); result = prime * result + ((getProductNum() == null) ? 0 : getProductNum().hashCode()); result = prime * result + ((getProductDescription() == null) ? 0 : getProductDescription().hashCode()); result = prime * result + ((getSale() == null) ? 0 : getSale().hashCode()); result = prime * result + ((getQuill() == null) ? 0 : getQuill().hashCode()); result = prime * result + ((getProductStatus() == null) ? 0 : getProductStatus().hashCode()); result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); result = prime * result + ((getCreateId() == null) ? 0 : getCreateId().hashCode()); return result; } @Override public String toString() { StringBuilder sb = new StringBuilder(); 域名nd(getClass().getSimpleName()); 域名nd(" ["); 域名nd("Hash = ").append(hashCode()); 域名nd(", productId=").append(productId); 域名nd(", productName=").append(productName); 域名nd(", productPrice=").append(productPrice); 域名nd(", productCategory=").append(productCategory); 域名nd(", productImgurl=").append(productImgurl); 域名nd(", productNum=").append(productNum); 域名nd(", productDescription=").append(productDescription); 域名nd(", sale=").append(sale); 域名nd(", quill=").append(quill); 域名nd(", productStatus=").append(productStatus); 域名nd(", createTime=").append(createTime); 域名nd(", createId=").append(createId); 域名nd(", serialVersionUID=").append(serialVersionUID); 域名nd("]"); return 域名ring(); } }
查看代码
package 域名域名ice; import 域名域名duct; import 域名域名域名vice; /** * @author 毛明辉 * @description 针对表【t_product】的数据库操作Service * @createDate 2022-04-06 14:40:35 */ public interface TProductService extends IService<TProduct> { }
package 域名域名; import 域名域名域名iceImpl; import 域名域名duct; import 域名域名ductService; import 域名域名ductMapper; import 域名域名ice; /** * @author 毛明辉 * @description 针对表【t_product】的数据库操作Service实现 * @createDate 2022-04-06 14:40:35 */ @Service public class TProductServiceImpl extends ServiceImpl<TProductMapper, TProduct> implements TProductService{ }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//域名//DTD Mapper 3.0//EN" "http://域名/dtd/mybatis-3-域名"> <mapper namespace="域名域名ductMapper"> <resultMap id="BaseResultMap" type="域名域名duct"> <id property="productId" column="product_id" jdbcType="BIGINT"/> <result property="productName" column="product_name" jdbcType="VARCHAR"/> <result property="productPrice" column="product_price" jdbcType="DECIMAL"/> <result property="productCategory" column="product_category" jdbcType="BIGINT"/> <result property="productImgurl" column="product_imgurl" jdbcType="VARCHAR"/> <result property="productNum" column="product_num" jdbcType="INTEGER"/> <result property="productDescription" column="product_description" jdbcType="VARCHAR"/> <result property="sale" column="sale" jdbcType="BIGINT"/> <result property="quill" column="quill" jdbcType="VARCHAR"/> <result property="productStatus" column="product_status" jdbcType="INTEGER"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> <result property="createId" column="create_id" jdbcType="BIGINT"/> </resultMap> <sql id="Base_Column_List"> product_id,product_name,product_price, product_category,product_imgurl,product_num, product_description,sale,quill, product_status,create_time,create_id </sql> </mapper>
- Elasticsearch相关
- 创建索引product,可以手动创建,客户端API方法如下,indexName传product即可,简单创建一下就行。
@Autowired private ElasticsearchClient elasticsearchClient; @PostMapping("/createIndex") public ResultJson createIndex(@RequestParam String indexName) throws IOException { 域名ces().create(createIndex -> 域名x(indexName)); return 域名ess(); }
- product 增删改 直接上代码 ,毕竟简单
package 域名域名ice; import 域名域名ltJson; import 域名域名duct; import 域名域名ticsearchClient; import 域名域名.DeleteRequest; import 域名域名.IndexRequest; import 域名域名.UpdateRequest; import 域名域名域名wired; import 域名域名ice; import 域名ception; @Service public class ElasticSearchService { @Autowired private ElasticsearchClient elasticsearchClient; /** * 上传商品相关数据到Elasticsearch * @param tProduct */ public ResultJson uploadProduct(TProduct tProduct) { IndexRequest<TProduct> req ; req = 域名( b-> 域名x("product").id(域名roductId()+"").document(tProduct)); try { 域名x(req); return 域名ess(); } catch (IOException e) { return 域名ure(域名ring()); } } /** * 修改 * @param tProduct * @return */ public ResultJson updateDocument(TProduct tProduct){ UpdateRequest<TProduct,TProduct> req; req = 域名( b-> 域名x("product").id(域名roductId()+"") .doc(tProduct) ); try { 域名te(req,域名s); return 域名ess(); } catch (IOException e) { return 域名ure(域名ring()); } } /** * 删除 * @param productId * @return */ public ResultJson deleteDocument(Long productId){ DeleteRequest req ; req = 域名( b-> 域名x("product").id(productId+"") ); try { 域名te(req); return 域名ess(); } catch (IOException e) { return 域名ure(域名ring()); } } }
在商品操作的时候调用一下即可
漏发了一个通用类,比较简单, 不发了。package 域名域名roller; import 域名域名ltJson; import 域名域名duct; import 域名域名ticSearchService; import 域名域名ductService; import 域名域名域名wired; import 域名.域名tation.*; import 域名; @RestController @RequestMapping("/api/product") public class ProductController { @Autowired private TProductService tProductService; @Autowired private ElasticSearchService elasticSearchService; @PostMapping("/save") public ResultJson save(@RequestBody TProduct tProduct){ 域名reateTime(new Date()); 域名(tProduct); return 域名adProduct(tProduct); } @PostMapping("/update") public ResultJson update(@RequestBody TProduct tProduct){ 域名teById(tProduct); return 域名teDocument(tProduct); } @PostMapping("/delete") public ResultJson delete(@RequestParam Long productId){ 域名veById(productId); return 域名teDocument(productId); } }
- 创建索引product,可以手动创建,客户端API方法如下,indexName传product即可,简单创建一下就行。
- 创建测试商品表
- 创建Elasticsearch 客户端 的bean 对象
- 成果展示
- 数据我update过,总之是已经传到Elasticsearch上面了。
- 查询,后面再补,这个才是关键。