在javascript中建立_msearch查询

时间:2018-08-28 14:23:48

标签: javascript node.js elasticsearch

我试图在Node.js中编写 _msearch 弹性搜索查询。但是我遇到了以下错误

{"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中创建了搜索模板。它在基巴纳州工作。我尝试在字符串的末尾添加\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" }  }`;

1 个答案:

答案 0 :(得分:1)

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

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