0
点赞
收藏
分享

微信扫一扫

ElasticSearch_09_ES 7.x版本的两个变动


系列文章目录

文章目录

  • ​​系列文章目录​​
  • ​​前言​​
  • ​​一、application.yaml 中 spring.data.elasticsearch.cluster-name,spring.data.elasticsearch.cluster-nodes 属性过时​​
  • ​​1.1 降低spring的版本​​
  • ​​1.2 通过@Configuration修饰的配置类​​
  • ​​二、移除type属性​​
  • ​​2.1 ES中type属性的版本演变​​
  • ​​2.2 原因分析​​
  • ​​总结​​

前言

elasticsearch 7.x版本的两个变动
1、application.yaml 中 spring.data.elasticsearch.cluster-name,spring.data.elasticsearch.cluster-nodes 属性过时
2、移除type属性

一、application.yaml 中 spring.data.elasticsearch.cluster-name,spring.data.elasticsearch.cluster-nodes 属性过时

在搞elasticsearch的时候,发现用的版本比较高,导致很多以前的方法都过时了,主要原因是:

ElasticSearch_09_ES 7.x版本的两个变动_大数据

1.1 降低spring的版本

将spring-boot版本降低,可参考下图

ElasticSearch_09_ES 7.x版本的两个变动_twitter_02

1.2 通过@Configuration修饰的配置类

所以这里我们使用RestHighLevelClient
写一个配置类:

@Configuration
public class ElasticsearchConfig {
@Bean
RestHighLevelClient elasticsearchClient() {
ClientConfiguration configuration = ClientConfiguration.builder()
.connectedTo("192.168.1.6:9200")
//.withConnectTimeout(Duration.ofSeconds(5))
//.withSocketTimeout(Duration.ofSeconds(3))
//.useSsl()
//.withDefaultHeaders(defaultHeaders)
//.withBasicAuth(username, password)
// ... other options

.build();
RestHighLevelClient client = RestClients.create(configuration).rest();
return client;
}
}

创建的实体类(省略了setget toString方法):

@Document(indexName = "atguigu",type = "book")
public class Book {
private Integer id;
private String bookName;
private String author;

创建的Repository接口:

public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
}

测试方法:

@Test
public void test02(){
Book book=new Book();
bookRepository.save(book);
}

需要注意的是,原来的index方法已经过时,这里我试着用了save方法,貌似是可以的

测试:

发现最后根本就没有book,也就是说索引没有创建成功,我觉得应该是实体类中@Document(indexName = “atguigu”,type = “book”)这个注解里面type没有生效,后来查到,在ES7.x版本中已经移除了type。

二、移除type属性

2.1 ES中type属性的版本演变

Elasticsearch 官网提出的近期版本对 type 概念的演变情况如下:
在 5.X 版本中,一个 index 下可以创建多个 type;
在 6.X 版本中,一个 index 下只能存在一个 type;
在 7.X 版本中,直接去除了 type 的概念,就是说 index 不再会有 type。

Elasticsearch 7.x
不建议在请求中指定类型。例如,为文档建立索引不再需要document type。新索引API适用PUT {index}/_doc/{id}于显式ID和POST {index}/_doc 自动生成的ID。请注意,在7.0中,它_doc是路径的永久部分,并且表示端点名称而不是文档类型。
将include_type_name在创建索引,索引模板,地图API参数默认为false。完全设置该参数将导致弃用警告。
的_default_映射类型被去除。

Elasticsearch 8.x
不再支持在请求中指定类型。
该include_type_name参数已删除。

2.2 原因分析

1、为何要去除 type 的概念?

原因分析
答: 因为 Elasticsearch 设计初期,是直接查考了关系型数据库的设计模式,存在了 type(数据表)的概念。但是,其搜索引擎是基于 Lucene 的,这种 “基因”决定了 type 是多余的。 Lucene 的全文检索功能之所以快,是因为 倒序索引 的存在。而这种 倒序索引 的生成是基于 index 的,而并非 type。多个type 反而会减慢搜索的速度。为了保持 Elasticsearch “一切为了搜索” 的宗旨,适当的做些改变(去除 type)也是无可厚非的,也是值得的。

解决办法:
在 6.0 的时候,已经默认只能支持一个索引一个 type 了,
在 7.0 的时候,新增了一个参数 include_type_name ,即让所有的 API 是 type 相关的,这个参数在 7.0 默认是 true,
在 8.0 的时候,参数 include_type_name 会默认改成 false,也就是不包含 type 信息了,这个是 type 用于移除的一个开关。当 include_type_name 参数设置成 false 后,所有增删改查搜索操作返回结果里面的关键字 _type 都将被移除。

例如:
索引操作:PUT {index}/{type}/{id}需要修改成PUT {index}/_doc/{id}
Mapping 操作:PUT {index}/{type}/_mapping 则变成 PUT {index}/_mapping
如果包含父子关系,使用 join 字段来构建。

#创建索引(不需要指定type)
PUT twitter
{
"mappings": {
"_doc": {
"properties": {
"type": { "type": "keyword" },
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" },
"content": { "type": "text" },
"tweeted_at": { "type": "date" }
}
}
}
}

#修改索引(不需要指定type)
PUT twitter/_doc/user-kimchy
{
"type": "user",
"name": "Shay Banon",
"user_name": "kimchy",
"email": "shay@kimchy.com"
}

#搜索 (不需要指定type,直接指定 index 为 twitter 就好了)
GET twitter/_search
{
"query": {
"bool": {
"must": {
"match": {
"user_name": "kimchy"
}
},
"filter": {
"match": {
"type": "tweet"
}
}
}
}
}

#重建索引 (直接指定 index 的源source和目的dest)
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}

因此在索引的时候,我输入的:http://ip地址:9200/atguigu/_doc/_search

ElasticSearch_09_ES 7.x版本的两个变动_spring_03

出来那么多是因为test02方法执行了好多次,save了好几次

总结

elasticsearch 7.x版本的两个变动
变动1:application.yaml 中 spring.data.elasticsearch.cluster-name,spring.data.elasticsearch.cluster-nodes 属性过时
解决1:要么降低springboot的版本,要么使用 @Configuration 代替 application.yaml 中的设置

变动2:移除type属性
解决2:所有增删改查搜索操作返回结果里面的关键字 _type 都将被移除


举报

相关推荐

0 条评论