json - Elasticsearch:如何动态存储所有json对象

标签 json elasticsearch sails.js

我正在尝试通过elasticsearch存储所有Json对象。

client.create({
                index: 'index',
                type: 'type',
                id:"1"
                body:result[0]
                },function (error,response)
                   {
                     if (error) 
                     {
                        console.log('elasticsearch cluster is down!');
                      } 
                      else 
                      {
                        console.log('All is well');
                      }
            });

在这个result[0]中,我获得了Json对象的第一个值,但是我需要动态存储所有Json对象。

我得到的输出是:
  -> POST http://localhost:9200/index/type/1?op_type=create
  {
    "Name": "Martin",
    "Age": "43",
    "Address": "trichy"
  }
  <- 201
  {
    "_index": "index",
    "_type": "type",
    "_id": "1",
    "_version": 4,
    "created": true
  }

但是我需要这样的输出:
 -> POST http://localhost:9200/index/type/1?op_type=create
      {
        "Name": "Martin",
        "Age": "43",
        "Address": "trichy"
      },
      {
        "Name": "vel",
        "Age": "23",
        "Address": "chennai"
      },
      {
        "Name": "ajay",
        "Age": "23",
        "Address": "chennai"
      }
      <- 201
      {
        "_index": "index",
        "_type": "type",
        "_id": "1",
        "_version": 4,
        "created": true
      }

最佳答案

您需要使用bulk endpoint以便同时发送许多文档。

主体在每个文档中包含两行,第一行包含文档的索引,类型和ID,文档本身位于下一行。冲洗并重复每个文档。

client.bulk({
  body: [
    // action description
    { index:  { _index: 'index', _type: 'type', _id: 1 } },
     // the document to index
    { Name: 'Martin', Age: 43, Address: 'trichy' },
    { index:  { _index: 'index', _type: 'type', _id: 2 } },
    { Name: 'vel', Age: 23, Address: 'chennai' },
    { index:  { _index: 'index', _type: 'type', _id: 3 } },
    { Name: 'ajay', Age: 23, Address: 'chennai' }
  ]
}, function (err, resp) {
  // ...
});

我怀疑您的result数组是昨天从your other question获得的JSON。如果是这样,那么您可以动态地构建主体,如下所示:
var body = [];
result.forEach(function(row, id) {
    body.push({ index:  { _index: 'index', _type: 'type', _id: (id+1) } });
    body.push(row);
});

然后,您可以在body调用中使用bulk,如下所示:
client.bulk({
  body: body
}, function (err, resp) {
  // ...
});

关于json - Elasticsearch:如何动态存储所有json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372608/

相关文章:

java - 使用匹配短语(精确搜索)子句的 Elasticsearch Java API Boost 查询

elasticsearch - Elasticsearch |如何获取具有相应匹配值的原始搜索查询

javascript - sails-mongo 适配器,规范化错误消息

java - @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) 不适用于微服务上下文

java - 通过 json 使用 java (eclipse) 从 MySQL-DB 检索字符串

java - Android Studio MalformedJsonException

logging - Kibana:日志和计算字段之间的持续时间

javascript - 为什么有些跨域 JSON 请求会失败,而有些则不会?

node.js - Sails/Waterline 中的软删除

javascript - SailsJS,SocketIO,没有收到消息