首页 > 解决方案 > 在 javascript 中构建 _msearch 查询

问题描述

我尝试在 Nodejs 中编写_msearch elasticsearch查询。但我收到以下错误

{"module":"Search","type":"Success","response":{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The msearch request must be terminated by a newline [\n]"}],"type":"illegal_argument_exception","reason":"The msearch request must be terminated by a newline [\n]"},"status":400}}

我已经使用 kibana 在 elasticsearch 中创建了搜索模板。它在 kibana 中工作。我试图\n在字符串末尾添加,仍然得到同样的错误。这是我的完整代码。

    let doc=`{"index":"my_index"}
{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.sent", "value":"0" }  }
{"index":"my_index"}
{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.status", "value":"open" }  }
{"index":"my_index"}
{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.status", "value":"click" }  }
{"index":"my_index"}
{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.status", "value":"bounce" }  }
`;
            let reqObj = {
                uri: API_URL+INDEX+'_msearch/template',
                method: 'GET',
                headers: { "content-type": "application/json" },
                body: doc,
                json: true
            };
            return new Promise((resolve,reject)=>{
                request(reqObj, (err, res, body) => {
                    if(err) return reject(this.Response("Search","error",err));
                    return resolve(this.Response("Search","Success",body));
                });
            });

我试图删除多余的空格仍然是相同的结果

let doc=`{"index":"my_index"}{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.sent", "value":"0" }  }{"index":"my_index"}{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.status", "value":"open" }  }{"index":"my_index"}{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.status", "value":"click" }  }{"index":"my_index"}{  "id": "count", "params": {  "domain":"'+domain+'", "condition":"attributes.status", "value":"bounce" }  }`;

标签: javascriptnode.jselasticsearch

解决方案


您需要确保删除左大括号之前的所有前导空格,并确保在最后一行之后有一个换行符。

此外,您可能需要删除json: true并将application/json内容类型更改为application/x-ndjson.


推荐阅读