测试elasticsearch保存时报找不到类型的错误
java测试存储数据到es时报错:...ActionRequestValidationException: Validation Failed: 1: type is missing...
/**
* 测试存储数据到 es
*/
@Test
public void indexData() throws IOException {
IndexRequest indexRequest = new IndexRequest("users");
indexRequest.id("1"); //数据的id
// indexRequest.source("username","zhangsan","age",18,"gender","男");
User user = new User();
String jsonString = JSON.toJSONString(user);
indexRequest.source(jsonString, XContentType.JSON); //要保存的内容
//执行保存操作
IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//提取有用的响应数据
System.out.println(index);
}
解决:
尝试导入以下三个依赖:
<!-- 使用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>
有的导入上面三个包了,但还是报错,可以将版本改成7.1.0,或者尝试其他版本试一下。