您好,我是Elasticsearch的新手,我尝试使用 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.jsconst 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,
name,
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,
name,
snippet
}));
保证是一个数组。而且client.incides.create
函数不接受。检查this valid .create
usage,看看它是否对您有进一步的帮助。附注:搜索功能可同时执行多项操作:
_bulk
indexing。)所有这一切都违反了称为separation of concerns的原理。看到了,搜索功能失败了,但是应该没有,因为
search
本身没有什么问题。即使它没有失败,其他人(或您在6个月的时间内)阅读该代码也会想知道为什么在那里执行了多个不相关的 Action 。因此,进行一些重构是合理的。
关于node.js - StatusCodeError:[not_x_content_exception]压缩器检测只能在某些xcontent字节或压缩的xcontent字节上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63363301/