迁移 Elastic Search:Elastic Search 2.x - 7.x custom analyzer 的升级

773 阅读1分钟

首先由于种种原因先抛开 spring-boot-elasticsearch 这个库对于 es 的实现,公司先有版本的 es mapping 转换的结果通过上面的分析肯定是差很多了,虽然不一定用到 spring-boot-elasticsearch 这个库,但是我必须参考它的代码,能用就用。

先看官网:

Spring Data Elasticsearch - Reference Documentation

  • Earlier versions of Spring Data Elasticsearch used a Jackson based conversion, Spring Data Elasticsearch 3.2.x introduced the Meta Model Object Mapping. As of version 4.0 only the Meta Object Mapping is used, the Jackson based mapper is not available anymore and the MappingElasticsearchConverter is used.

在 3.2.x 之前的版本中 es 还是用的 jackson,4.0 之后用的是自己实现的 MappingElasticsearchConverter.java

看看源码,参考 spring-data 的设计:

首先:继承了 ElasticsearchRepository 的 repository ,并且当前 index 不存在,才会自动创建 index ,这点确实不错。

image

@Override
	protected boolean doExists(String indexName) {
		GetIndexRequest request = new GetIndexRequest(indexName);
		return restTemplate.execute(client -> client.indices().exists(request, RequestOptions.DEFAULT));
	}

Model 默认先去读是否设置了 @Mapping(自定义手写 mapping json 的文本的地址),没有再去找 @Document 处理映射关系。

image

处理映射之前再检查一下是否用了 @DynamicTemplates 制定了 ES 的 Index 模板,决定是否复用。

开始构建 mapping 了 :

image

会忽略 @Transient 或者被 ignore 的字段,构建结束可以得到一个 :

{
    "properties":{
        "date":{
            "type":"date"
        },
        "location":{
            "type":"geo_point"
        }
    }
}

github 归档 : github.com/pkwenda/Blo…