javascript - 在 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" } }`;
解决方案
您需要确保删除左大括号之前的所有前导空格,并确保在最后一行之后有一个换行符。
此外,您可能需要删除json: true
并将application/json
内容类型更改为application/x-ndjson
.
推荐阅读
- angular - 将使用 ng-class 的类添加到 mat-menu
- sql - 如何明智地从表类别中获取最后一个值?
- google-cloud-platform - Google Cloud Run 是否支持流式响应数据?
- javascript - 在前端检测数据的变化,只上传这些,然后在后端更新数据
- java - 如何在 Spring Boot 中获取请求的客户端 IP 地址?
- r - 在 R 中读取 csv 文件时出现问题
- python - Python Pandas 按加载日期过滤
- r - 使用 R 处理谷歌地球引擎数据
- charts - Android studio anychart 圆形量规
- javascript - 为什么锚标签占用额外的空白空间?