Elasticsearch Node.js客户端抛出parsing_exception:[parsing_excepti



我正在尝试开发一个scroll例程,以便使用Node.js处理存储在Elasticsearch中的一些元素,特别是@elastic/elasticsearch版本8.2.1

这就是我目前所拥有的:

async function* scrollSearch(query) {
let response = await client.search({
index: "my.elasticsearch.index",
query: JSON.parse(query),
scroll: "1m",
size: 100,
// sort: [{ modification_date: "desc", }],
// track_total_hits: true,
});
while (true) {
const sourceHits = response.hits.hits;
if (sourceHits.length === 0) {
break;
}
for (const hit of sourceHits) {
yield hit;
}
if (!response._scroll_id) {
break;
}
response = await client.scroll({
scroll_id: response._scroll_id,
scroll: "10s",
});
}
}

目前我只是在调试这个,所以我直接调用前面的函数:

const query = `
{
"query": {
"bool": {
"must": [
{
"match": {
"status": "THE_STATUS"
}
},
{
"exists": {
"field": "object1.field1"
}
},
{
"exists": {
"field": "object2.field1"
}
},
{
"exists": {
"field": "object2.field2"
}
},
{
"exists": {
"field": "object3.field1"
}
},
{
"exists": {
"field": "object3.field2"
}
},
{
"exists": {
"field": "object3.field3"
}
}
]
}
}
}
`;
for await (const hit of scrollSearch(query)) {
console.log(hit._source);
}

我总是收到以下查询错误:

ResponseError: parsing_exception: [parsing_exception] Reason: unknown query [query]
at SniffingTransport.request (node_modules/@elastic/transport/lib/Transport.js:476:27)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Client.SearchApi [as search] (node_modules/@elastic/elasticsearch/lib/api/api/search.js:65:12)
at async scrollSearch (file:///Users/x80486/Workshop/debug-tool/src/elasticsearch.service.js:15:18)
at async Context.<anonymous> (file:///Users/x80486/Workshop/debug-tool/test/my.test.js:132:22)

另一方面,如果我在Dev Tools接口中使用完全相同的查询,我会得到预期的结果:

POST my.elasticsearch.index/_search?scroll=1m
{
"query": {
"bool": {
"must": [
{
"match": {
"status": "THE_STATUS"
}
},
{
"exists": {
"field": "object1.field1"
}
},
{
"exists": {
"field": "object2.field1"
}
},
{
"exists": {
"field": "object2.field2"
}
},
{
"exists": {
"field": "object3.field1"
}
},
{
"exists": {
"field": "object3.field2"
}
},
{
"exists": {
"field": "object3.field3"
}
}
]
}
}
}
{
"_scroll_id" : "the scroll id value",
"took" : 266,
"timed_out" : false,
"_shards" : {
"total" : 12,
"successful" : 12,
"skipped" : 0,
"failed" : 0
},
...
}

有没有办法知道Node.js客户端不理解查询的哪一部分,或者通常验证这一点?

嗯,嗯。。。我只需要删除query对象/段。它必须是这样的:

{
"bool": {
"must": [
{
"match": {
"status": "THE_STATUS"
}
},
{
"exists": {
"field": "object1.field1"
}
},
{
"exists": {
"field": "object2.field1"
}
},
{
"exists": {
"field": "object2.field2"
}
},
{
"exists": {
"field": "object3.field1"
}
},
{
"exists": {
"field": "object3.field2"
}
},
{
"exists": {
"field": "object3.field3"
}
}
]
}
}

最新更新