如何解决StatusCodeError:[not_x_content_exception]压缩器检测只能在某些xcontent字节或压缩的xcontent字节上调用
您好,我是elasticsearch的新手,我尝试通过弹性搜索在Node.js中进行搜索。我让JSON.stringify()显示错误。互联网称数据格式存在问题。如果您在Node.JS中从事Elasticsearch工作。您可以提供代码帮助吗?在search.js中,我尝试使函数散装,插入json数据的索引。如果您需要在解析器代码中编写代码,我会从API放入JSON数据,但我认为search.js中会出错。
我的代码(search.js):
const es = require('elasticsearch');
const search = async (search_keyword,data) => {
const esClient = new es.Client({
host: 'localhost:9200',log: 'trace'
});
await esClient.indices.create({
index: 'test',body: data,});
await esClient.search({
index: 'test',body: {
query: {
multi_match: {
query: search_keyword,fields: ["id","name","snippet"],operator: "or"
}
}
}
})
};
module.exports=search;
server.js
const express = require("express");
const app = express();
const parser = require('./parser/parser');
const search=require('./elasticsearch/search')
;(async () => {
const result = await parser();
for (const item of result) {
const data = JSON.parse(item.data);
const output = data.items.map(({id,name,snippet}) => ({
id,snippet
}));
//console.log(output);
try {
await search("JS",output);
}catch (err) {
console.log(err);
}
}
})();
const server = app.listen(3001,() => {
console.log("listening on port %s...",server.address().port);
});
错误:
StatusCodeError: [not_x_content_exception] Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
at respond (D:\Programming\test-task\node_modules\elasticsearch\src\lib\transport.js:349:15)
at checkRespForFailure (D:\Programming\test-task\node_modules\elasticsearch\src\lib\transport.js:306:7)
at HttpConnector.<anonymous> (D:\Programming\test-task\node_modules\elasticsearch\src\lib\connectors\http.js:173:7)
at IncomingMessage.wrapper (D:\Programming\test-task\node_modules\lodash\lodash.js:4949:19)
at IncomingMessage.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1183:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
status: 500,displayName: 'InternalServerError',message: '[not_x_content_exception] Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes',path: '/test',query: {},body: {
error: {
root_cause: [Array],type: 'not_x_content_exception',reason: 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'
},status: 500
},statusCode: 500,response: '{"error":{"root_cause":[{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}],"type":"not_x_content_excep
tion","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"},"status":500}',toString: [Function],toJSON: [Function]
}
解决方法
我认为有问题的部分是
await esClient.indices.create({
index: 'test',body: data,});
因为data
是您要传递给搜索output
函数的search
:
const output = data.items.map(({id,name,snippet}) => ({
id,snippet
}));
保证是一个数组。 client.incides.create
函数不接受。检查this valid .create
usage,看它是否对您有进一步的帮助。
P.S .:搜索功能一次执行多项操作:
- 实例化客户端
- 创建一个索引(希望为文档建立索引-会失败。请改为查看
_bulk
indexing。) - 并执行搜索。
所有这些组合都违反了称为separation of concerns的原则。瞧瞧,搜索功能失败了,但它不应该失败,因为search
本身没有什么问题。即使它没有失败,其他人(或您在6个月的时间内)阅读该代码也会想知道为什么在那里执行了多个无关的操作。因此,进行一些重构是合理的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。