elasticsearch 自定义ID

本文详细介绍了在Elasticsearch中如何自定义文档的ID,包括自定义ID的场景、核心代码示例、遇到的问题及最终的解决方案。

elasticsearch 自定义ID

场景描述

    es采集关系型数据库(mysql)中的数据 进行id 精准搜索, 由于mysql 中的ID 生成规则为 分布式ID(19位 ) 使用 long 类型接收并写入到es 中 会有精度的丢失 (es 版本 5.5.3 )

es采集核心代码


 public static void insertOne(String index, String type, Object object) {
        if (object != null) {
            try {
                Map<String, Object> objMap = (Map<String, Object>) JSON.toJSON(object);
                try {
                    IndexRequest indexRequest = new IndexRequest(index, type, objMap.get("id").toString());
                    indexRequest.source(objMap);
                    highLevelClient.indexAsync(indexRequest, RequestOptions.DEFAULT, new ActionListener<IndexResponse>() {
                        @Override
                        public void onResponse(IndexResponse indexResponse) {

                        }

                        @Override
                        public void onFailure(Exception e) {
                            HttpUtil.sendError2SLS(e.getLocalizedMessage(), "es insert one actionListener failure");
                        }
                    });
                } catch (Exception e) {
                    HttpUtil.sendError2SLS(e.getLocalizedMessage(), "es insert one ");
                }


            } catch (Exception e) {
                HttpUtil.sendError2SLS(e.getLocalizedMessage(), "es insert one ");
            }
        }
    }
  

问题描述

   inertOne 方法中的参数对象都有一个 标量元素  long id ;
   当数据插入到 es 中时  由于 关系型数据库(mysql)中的id 为 bigint 类型  写入到es 中的 id 
  精度有损失,故 通过 must termQuery 查询id 时 总是得不到数据 ,

解决方案

  将对象中的 id 类型从long 类型转换为 String 类型  这样就保证了 写入到es中的数据中 _id  和 id 字段
  的数据值是一样的 ,经过测试也可以使用 id 查询
### 使用Spring Boot搭建与Elasticsearch自定义集群集成应用的最佳实践 #### 1. 添加Maven依赖 为了使Spring Boot项目能够与Elasticsearch交互,需在`pom.xml`文件中加入必要的依赖项。对于较新的版本组合,推荐使用如下配置: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <!-- 版本号应匹配所使用的Spring Boot和Elasticsearch --> </dependency> <!-- 如果需要更高级别的REST API调用功能,则可以添加此依赖 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <!-- 同样注意保持版本一致性 --> </dependency> ``` 上述依赖分别提供了对Spring Data Elasticsearch的支持以及通过HTTP协议操作Elasticsearch的能力[^2]。 #### 2. 配置Elasticsearch连接参数 编辑项目的application.properties或application.yml来指定目标Elasticsearch集群的信息,包括但不限于主机地址、端口号等基本信息;当涉及到安全性设置时(如启用了X-Pack),还需提供认证凭证等相关细节。 ```yaml spring: data: elasticsearch: cluster-name: my-cluster # 替换成实际的集群名称 cluster-nodes: localhost:9300 # 列表形式给出所有节点IP及传输端口,默认为TCP/9300 # 对于远程部署的情况,请替换为主机的真实公网IP elasticsearch: rest: uris: http://localhost:9200 # RESTful接口URL列表,逗号分隔多个URI username: elastic # 若已启用安全特性,则填写对应的用户名 password: changeme # 密码也在此处声明 ``` 这段配置指定了如何定位并接入特定的Elasticsearch集群,并处理可能存在的身份验证需求[^4]。 #### 3. 创建实体类映射至索引结构 基于领域模型创建Java对象表示文档数据,在此类中标注字段属性以便框架自动完成JSON序列化过程中的转换工作。例如: ```java import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "product", type = "_doc") // 定义存储这些记录的目标索引名及其类型 public class Product { @Id private String id; // 文档唯一标识符 private String name; // 商品名字 private double price; // 单价 // Getters and Setters... } ``` 此处展示了怎样利用注解机制将业务逻辑层的对象关联到具体的Elasticsearch索引之上[^1]。 #### 4. 编写Repository接口继承预设好的CRUD方法集 借助Spring Data提供的模板代码快速实现基本的数据存取服务,只需定义好泛型参数即可获得一系列现成的操作函数供开发者调用。 ```java import org.springframework.data.repository.CrudRepository; interface ProductRepository extends CrudRepository<Product, Long> { } ``` 这一步骤简化了针对持久化层的具体编码任务,使得应用程序可以直接专注于核心业务流程的设计与开发。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值