【问题标题】:Passing only few data to the JSON.parse() into Elasticsearch仅将少量数据传递给 JSON.parse() 到 Elasticsearch
【发布时间】:2020-01-25 16:17:23
【问题描述】:

在这里,我只需要允许来自 JSON 的少量数据和 Elasticsearch 中的索引。那么,最好的方法是什么,而不是分别删除每个呢?

var elasticData= JSON.parse(result.body);
        elasticData = elasticData.res;
        elasticData["id"] = elasticData._id;
        delete elasticData["__v"];
        delete elasticData["_id"];
        delete elasticData["addressee"];
        delete elasticData["abbreviation"];
        delete elasticData["care_of"];
        delete elasticData["address_1"];
        delete elasticData["address_2"];
        delete elasticData["company_id_legacy"];        
        elasticSearch_common.createCompanyIndex(elasticData);

例如:我只需要通过("name","id","address"),其余的我不想通过。但是在同一个 JSON 中还有很多其他数据可用。

{
    "userId":"34234234234gjgjg894734g",
  "company":{ 
  "id" : "21", 
  "addressee" : {
      "addressee" : "EXAMPLE", 
      "title_name" : "", 
      "salutation" : "Dear:", 
      "comments" : "", 
      "email" : "one@one.com", 
      "phone" : "", 
      "fax" : "", 
      "extention_group" : "", 
      "ext" : ""
  }, 
  "abbreviation" : "", 
  "care_of" : "", 
  "address_1" : "2", 
  "address_2" : "", 
  "address_3" : "", 
  "state" : "CA", 
  "zip" : "90024", 
  "is_deleted" : false, 
  "company_code" : "sdas", 
  "parent_company" : null, 
  "name" : "NIOE", 
  "createdBy" : "Dat", 
  "modifiedBy" : "Dat", 
  "createdDate" : "2019-08-22T19:10:50.000+0000", 
  "modifiedDate" : "2019-08-22T19:10:50.000+0000", 
}

这是实际的 JSON,我想在下面作为要推送的 elasticsearch 索引数据。

{ name: 'NIOE',
  id: '21' }

【问题讨论】:

    标签: javascript arrays node.js json elasticsearch


    【解决方案1】:

    白名单怎么样?

    // raw data you get from wherever
    const elasticData = JSON.parse(result.body);
    
    // object to index into ES
    const toIndex = {};
    
    // whitelist of fields to index
    ["name", "id", "address"].forEach(field => {
      toIndex[field] = elasticData.company ? elasticData.company[field] : null;
    });
    
    // assert: toIndex now contains only the fields you want to index
    elasticSearch_common.createCompanyIndex(toIndex);
    

    【讨论】:

    • 仍在获取全部数据
    • 非常值得怀疑...你能用你现在拥有的实际代码更新你的问题吗?
    • 已更新,但不是重大变化,添加了 JSON
    • 问题是所有东西都嵌套在company字段下。所以只需将elasticData[field]; 替换为elasticData.company[field];
    • var elasticData= JSON.parse(result.body); elasticData = elasticData.res; elasticData["id"] = elasticData._id; const toIndex = {}; ["name", "id","address"].forEach(field => { toIndex[field] = elasticData.company[field]; }); console.log(elasticData); elasticSearch_common.createCompanyIndex(elasticData);
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2014-03-08
    • 2018-01-25
    • 2018-01-20
    相关资源
    最近更新 更多