Root mapping definition has unsupported parameters: [all : {analyzer=ik_max_wor

你们好,我是金金金。

场景

  • 我正在使用Springboot整合elasticsearch,在创建索引(分词器) 运行报错,如下

排查

排查之前我先贴一下代码

java 复制代码
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; // 注意这个包


@SpringBootTest
public class RestHighLevelClientTest {
  // 当前客户端是手工维护的,因此不能通过自动装配的形式加载对象
  private RestHighLevelClient client;
  
  @BeforeEach
  void setUp() {
    HttpHost host = HttpHost.create("http://localhost:9200"); // 配置ES服务器地址与端口9200
    RestClientBuilder builder = RestClient.builder(host);
    client = new RestHighLevelClient(builder);
  }
  
  @AfterEach
  void tearDown() throws IOException {
    client.close();
  }
  
  @Test
  void testCreateIndexByIK() throws IOException {
    CreateIndexRequest request = new CreateIndexRequest("books");
    String json = "{\n" +
      "    \"mappings\": {\n" +
      "        \"properties\": {\n" +
      "            \"id\": {\n" +
      "                \"type\": \"keyword\"\n" +
      "            },\n" +
      "            \"name\": {\n" +
      "                \"type\": \"text\",\n" +
      "                \"analyzer\": \"ik_max_word\",\n" +
      "                \"copy_to\": \"all\"\n" +
      "            },\n" +
      "            \"type\": {\n" +
      "                \"type\": \"keyword\"\n" +
      "            },\n" +
      "            \"description\": {\n" +
      "                \"type\": \"text\",\n" +
      "                \"analyzer\": \"ik_max_word\",\n" +
      "                \"copy_to\": \"all\"\n" +
      "            },\n" +
     "            \"all\": {\n" +
      "                \"type\": \"text\",\n" +
      "                \"analyzer\": \"ik_max_word\"\n" +
      "            }\n" +
      "        }\n" +
      "    }\n" +
      "}";

    //设置请求中的参数
    request.source(json, XContentType.JSON);
    client.indices().create(request, RequestOptions.DEFAULT);
  }
}
  • 上面就是我的测试代码,很可惜它报错了。
  • 之前我在postman测试发送的请求参数没报错,所以我就很纳闷

所以我怀疑是不是在代码里面是不是写法不一样?当然我也在百度找到了方法,请看解决

造成error的原因

缺少参数导致识别不了(导入对包就没这个问题)

  • 我当前导入的CreateIndexRequest包是 import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;下的

解决

第一种解决方式 : 当导入的是import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;,在json请求参数里面需要加上一个type_name属性

可能不太建议这种方式,毕竟idea爆出了如下提示,还是用第二种方式吧

第二种解决方式 : 当然也可以不加 type_name也一样可以成功,导入import org.elasticsearch.client.indices.CreateIndexRequest;这个包下的CreateIndexRequest即可

  • 编写有误还请大佬指正,万分感谢。
相关推荐
yrldjsbk1 天前
第 3 章 实战项目 1:通用用户管理后端(接单高频需求)
spring boot·maven·mybatis
qq_262496091 天前
Elasticsearch 核心参数调优指南
大数据·elasticsearch
v***59831 天前
springBoot连接远程Redis连接失败(已解决)
spring boot·redis·后端
Coder_Boy_1 天前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范实现细节
大数据·开发语言·人工智能·spring boot
5***b971 天前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端
qq_318121591 天前
Java大厂面试故事:Spring Boot、微服务与AI场景深度解析
java·spring boot·redis·微服务·ai·kafka·spring security
木风小助理1 天前
Elasticsearch生产环境最佳实践指南
大数据·elasticsearch·搜索引擎
L***d6701 天前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
袁慎建1 天前
如何发布自定义 Spring Boot Starter
spring boot
haokan_Jia1 天前
【一、地质灾害气象风险预警互联系统-自由编辑预警区域,打包生成预警成果】
spring boot