ElasticSearch与SpringBoot的巨坑
刚学习elasticsearch,看了某人的教程,防止版本不符冲突,做了以下处理
<properties>
<java.version>1.8</java.version>
<elasticsearch>7.6.1</elasticsearch>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
创建索引请求的时候报错:
Exception in thread "I/O dispatcher 1" java.lang.AssertionError
一开始以为是es的内存设置太小了,扩大后,依旧错,但是还是能够创建成功,便没有理会
创建文档的时候还是一样报错,代码如下:
void testAddDocument() throws IOException {
//创建对象
User user = new User("wefwef",22,new Date());
//创建请求
IndexRequest request = new IndexRequest("chunuo");
//id
request.id("12");
request.timeout(TimeValue.timeValueSeconds(1));
//连接超时时间
request.timeout("1s");
//转为json字符串
request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求,响应结果
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status()); //对应我们命令返回的状态 CREATED,更新的 话是update
}
错误提示:
Exception in thread "I/O dispatcher 1" java.lang.AssertionError
与
Validation Failed: 1: type is missing;
查询了下,Validation Failed: 1: type is missing;错误需要添加以下两个jar包
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch}</version>
</dependency>
这个是官方提出的
这时我查看了IndexRequest构造函数,需要一个type的,相信大家都知道type很快就会被弃用,不过我还是测试了一下
IndexRequest request = new IndexRequest(“chunuo”,“type”);
结果竟然好使了,就是报了以下错误,但至少可以添加
Exception in thread "I/O dispatcher 1" java.lang.AssertionError
然后我开始了解了springboot与elasticsearch的兼容问题,将版本进行锁定后,一切毛病都没了
IndexRequest request = new IndexRequest(“chunuo”);
方法也不需要type参数了,操作如下
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch}</version>
</dependency>
</dependencies>
</dependencyManagement>
今晚真的是入了深坑,整了一个晚上,同样的问题希望可以帮到更多的小伙伴,今天遇到的错误,我是没能从搜索引擎找到解决方案