node.js - 如何使用 Nodejs 将父/子文档索引到 Elasticsearch 中?

标签 node.js elasticsearch

我可以使用这个curl命令轻松索引:

curl -XPUT 'http://localhost:9200/prototype_2012.12.27/chow-clfg/Cg-IKkxdTstKAAAAlw-?parent=chow-demo' -d '{
  "chow-clfg": {
    "@type": "chow-clfg",
    "clfg": "Cg6h4VCcMexXAAAAsQc",
    "@timestamp": "2012-12-27T08:24:00.000Z",
    "count": 1
  }
}'

结果:

{
  "ok": true,
  "_index": "prototype_2012.12.27",
  "_type": "chow-clfg",
  "_id": "Cg-IKkxdTstKAAAAlw-",
  "_version": 1
}

这几乎立即返回结果。

但是,当我使用 Nodejs 的 http.request 方法尝试时:

var http = require('http');
var index = "prototype_2012.12.27";
var server = "chow-clfg";

var options = {
 host: 'localhost',
 port: '9200',
 path: index+'/'+server+'/Cg-IKkxdTstKAAAAlw-?parent=chow-demo',
 method: 'PUT'
};

var req = http.request(options, function(res){
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');

    res.on('data', function (chunk) {
     console.log(chunk);
    });
});

req.on('error', function(e) {
 console.log('problem with request: ' + e.message);
});

req.write('{"chow-clfg":{"@type":"chow-clfg","clfg":"Cg6h4VCcMexXAAAAsQc","@timestamp":"2012-12-27T08:24:00.000Z","count":1}}');
req.end;

这只是中途暂停该过程,并且不会向我提供有关其是否成功的反馈。另外,当我在索引中搜索该文档时,我找不到它。

因此,我是否犯了编码错误?或者有什么办法可以解决这个问题吗?

最佳答案

为什么不使用 elasticsearch 模块及其带有 parent 参数的 index 方法?

关于node.js - 如何使用 Nodejs 将父/子文档索引到 Elasticsearch 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14051921/

相关文章:

javascript - 查看 Node 请求体

node.js - 如何在 deno 中删除运行时导入缓存?

elasticsearch - 按嵌套对象的长度过滤查询。即。 min_child

elasticsearch - 带有term和function_score,parsing_exception的Elasticsearch has_child查询

elasticsearch - 如何查询一个elasticsearch join字段名?

javascript - 使用 Node 在 Azure 函数中发送 zip 文件作为响应的最佳方式?

angularjs - 如何在 ng-autocomplete 中添加用户的当前位置

javascript - Sails Js 中的动态数据库连接

ElasticSearch:只允许本地请求

database - 是否可以实时自动从 elasticsearch 中删除早于例如 10 天的数据?