发生缘由
- 学习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
,代码如下:文章来源:https://www.toymoban.com/news/detail-504902.html
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" +
。将其删去即可。文章来源地址https://www.toymoban.com/news/detail-504902.html
到了这里,关于ES:Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!