ES 创建索引解析 DSL 解析异常
背景
ES java 客户端 RestClient 创建索引DSL解析异常,已排除可能错误:
- 客户端 ES 依赖与服务器 ES 版本不一致。
ES 6.*与ES 7.*DSL 语句不兼容。- json 格式错误。
各依赖版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
</dependencies>
单元测试代码
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@Slf4j
@SpringBootTest
class DemoApplicationTests {
String mappingJson = "{\n" +
" \"mappings\": \n" +
" {\n" +
" \"properties\": \n" +
" {\n" +
" \"id\":{\"type\": \"keyword\"},\n" +
" \"name\":{\"type\": \"text\",\"analyzer\": \"ik_max_word\"},\n" +
" \"address\":{\"type\": \"keyword\",\"index\": false},\n" +
" \"price\":{\"type\": \"integer\"},\n" +
" \"score\":{\"type\": \"integer\"},\n" +
" \"brand\":{\"type\": \"keyword\"},\n" +
" \"star_name\":{\"type\":\"keyword\"},\n" +
" \"business\":{\"type\":\"keyword\"},\n" +
" \"location\":{\"type\":\"geo_point\"},\n" +
" \"pic\":{\"type\": \"keyword\", \"index\": false}\n" +
" }\n" +
" }\n" +
"}";
@Test
void contextLoads() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.0.102:9200")
));
CreateIndexRequest request = new CreateIndexRequest("demo");
request.source(mappingJson, XContentType.JSON);
client.indices().create(request, RequestOptions.DEFAULT);
client.close();
}
}
问题描述
异常信息
ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [properties]: Root mapping definition has unsupported parameters:
原因分析
RestClient 客户端 create 方法有两个版本,其中一个版本已过时,不能在 ES 7.* 版本中使用。
//可用版本
import org.elasticsearch.client.indices.CreateIndexRequest;
//过时版本
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
解决方案
导入可用版本的 CreateIndexRequest 库。
//可用版本
import org.elasticsearch.client.indices.CreateIndexRequest;

浙公网安备 33010602011771号