0

Here I only have to allow few data coming from JSON and index in Elasticsearch. So what is the perfect way to do it, rather than delete each separately?

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);

For Example: I only have to pass ("name","id","address") and rest of them I don't want to pass it. But there's lots of other data(s) are available in same JSON.

{
    "userId":"34234234234gjgjg894734g",
  "company":{ 
  "id" : "21", 
  "addressee" : {
      "addressee" : "EXAMPLE", 
      "title_name" : "", 
      "salutation" : "Dear:", 
      "comments" : "", 
      "email" : "[email protected]", 
      "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", 
}

This is the actual JSON, And I want below as the elasticsearch index data to be pushed.

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

1 Answer 1

1

How about whitelisting?

// 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);
Sign up to request clarification or add additional context in comments.

14 Comments

still getting the whole data
Very doubtful... can you update your question with the actual code you now have?
Updated, but not major change, added JSON
The problem is that everything is nested under the company field. so simply replace elasticData[field]; by 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);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.