发生缘由

  • 学习ES中Java HighLevel Rest Client客户端API

运行环境

  • elasticsearch版本:7.12.1
  • jdk版本:jdk-8
  • 电脑系统:win10
  • Idea版本:2021.2

报错信息

org.elasticsearch.common.compress.NotXContentException: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes

	at org.elasticsearch.common.compress.CompressorFactory.compressor(CompressorFactory.java:42)
	at org.elasticsearch.common.xcontent.XContentHelper.convertToMap(XContentHelper.java:108)
	at org.elasticsearch.client.indices.CreateIndexRequest.source(CreateIndexRequest.java:276)
	at org.elasticsearch.client.indices.CreateIndexRequest.source(CreateIndexRequest.java:257)
	at com.linxuan.hotel.HotelDemoApplicationTests.createHotelIndex(HotelDemoApplicationTests.java:33)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at java.util.ArrayList.forEach(ArrayList.java:1249)

分析排查

  • Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
  • 压缩器检测只能在某些 xcontent 字节或压缩的 xcontent 字节上面调用

根据报错原因可以将错误定位至HotelDemoApplicationTests.java:33行,代码如下:

@Test
void createHotelIndex() throws IOException {
    // 1.创建Request对象
    CreateIndexRequest request = new CreateIndexRequest("hotel");
    // 2.准备请求的参数 DSL语句
    // 33行 报错
    request.source(MAPPING_TEMPLATE, XContentType.JSON);
    // 3.发送请求
    client.indices().create(request, RequestOptions.DEFAULT);
}

那么就可以将报错信息转为人话了:语句的类型不是JSON风格的或者JSON格式化错误了。

定位MAPPING_TEMPLATE,代码如下:

public static final String MAPPING_TEMPLATE = "PUT /hotel\n" +
            "{\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" +
            "      \"address\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"all\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}\n";

OK,原因找到了,因为添加了这一点代码:"PUT /hotel\n" +。将其删去即可。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐