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

  • 编写有误还请大佬指正,万分感谢。
相关推荐
张较瘦_6 小时前
SpringBoot3 | SpringBoot中Entity、DTO、VO的通俗理解与实战
java·spring boot·后端
程序猿小蒜8 小时前
基于springboot的医院资源管理系统开发与设计
java·前端·spring boot·后端·spring
老华带你飞10 小时前
社团管理|基于Java社团管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
Linux_Nathan11 小时前
【服务部署】ELFK架构篇之Elasticsearch
elk·elasticsearch·架构
老华带你飞11 小时前
汽车销售|汽车报价|基于Java汽车销售系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·汽车
TinpeaV11 小时前
Elasticsearch8(ES)保姆级菜鸟入门教程
大数据·spring boot·elasticsearch·搜索引擎·全文检索·postman
象象翔11 小时前
AI+若依(实战篇)
java·人工智能·spring boot·spring
小王师傅6612 小时前
【轻松入门SpringBoot】从 0 到 1 搭建 SpringBoot 工程-中
java·spring boot·spring
qq_3482318513 小时前
Spring Boot开发过程中常见问题
java·spring boot·后端