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即可

  • 编写有误还请大佬指正,万分感谢。
相关推荐
t***p9354 小时前
idea创建SpringBoot自动创建Lombok无效果(解决)
spring boot·后端·intellij-idea
d***81724 小时前
解决SpringBoot项目启动错误:找不到或无法加载主类
java·spring boot·后端
自不量力的A同学7 小时前
Spring Boot 4.0.0 正式发布
java·spring boot·后端
q***98528 小时前
什么是Spring Boot 应用开发?
java·spring boot·后端
豆奶特浓69 小时前
Java面试模拟:当搞笑程序员谢飞机遇到电商秒杀与AIGC客服场景
java·spring boot·微服务·面试·aigc·高并发·电商
踏浪无痕11 小时前
PageHelper 防坑指南:从兜底方案到根治方案
spring boot·后端
通往曙光的路上12 小时前
焚决糟糕篇
java·spring boot·tomcat
6***v41712 小时前
spring boot 项目打印sql日志和结果,使用logback或配置文件
spring boot·sql·logback
q***656912 小时前
使用Canal将MySQL数据同步到ES(Linux)
linux·mysql·elasticsearch
3***g20512 小时前
如何使用Spring Boot框架整合Redis:超详细案例教程
spring boot·redis·后端