Elasticsearch报错“ActionRequestValidationException: Validation Failed: 1: type is missing;“
Posted 二木成林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch报错“ActionRequestValidationException: Validation Failed: 1: type is missing;“相关的知识,希望对你有一定的参考价值。
异常
Exception in thread "main" org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;
at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:26)
at org.elasticsearch.action.index.IndexRequest.validate(IndexRequest.java:152)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1728)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1694)
at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:926)
at Elasticsearch05.main(Elasticsearch05.java:41)
代码
向Elasticsearch中插入单条数据:
public class Elasticsearch05 {
public static void main(String[] args) throws IOException {
// 传入IP地址和端口号
HttpHost httpHost = new HttpHost("localhost", 9200, "http");
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHost));
// 1.要在指定索引下创建文档,所以要先创建索引,再创建文档
IndexRequest request=new IndexRequest();
// index()方法设置索引名;id()方法设置唯一id标识
request.index("user").id("10001");
// 2.创建实体类对象,填充数据
User user=new User();
user.setName("张三丰");
user.setAge(30);
user.setSex("男");
// 3.利用jackson将实体类对象转换成JSON格式字符串
ObjectMapper mapper=new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
// 4.添加文档数据,数据格式为JSON格式
request.source(userJson, XContentType.JSON);
// 5.发送请求,获取响应结果
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println("_index: "+response.getIndex());
System.out.println("_id: "+response.getId());
System.out.println("_result: "+response.getResult());
// 一番操作后,关闭客户端连接
client.close();
}
}
原因
代码没有任何问题。
使用高级的client(RestHighLevelClient)是说还要依赖于其他两个jar:
- org.elasticsearch.client:elasticsearch-rest-client
- org.elasticsearch:elasticsearch
而我们在pom.xml导入的elasticsearch依赖是:
<!-- 使用elasticsearch必须导入的两个包 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.0</version>
</dependency>
而实际上应该导入三个坐标,如下:
<!-- 使用elasticsearch必须导入的三个包 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.1.0</version>
</dependency>
解决
导入elasticsearch、elasticsearch-rest-client、elasticsearch-rest-high-level-client三个的依赖坐标。
以上是关于Elasticsearch报错“ActionRequestValidationException: Validation Failed: 1: type is missing;“的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch 出现 “429 rejected” 报错,怎么办?
elasticsearch启动报错:can not run elasticsearch as root