[这里,我只需要允许少量数据来自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" : "[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",
}
这是实际的JSON,下面我希望将其作为Elasticsearch索引数据进行推送。
{ name: 'NIOE',
id: '21' }
如何将其列入白名单?
// 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);
1)您可以创建一个类FullJsonModel,它将映射json数据
2)在这里,FullJsonModel将是您将映射json结果的类。
3)然后再创建一个类,该类仅包含您必填的字段
4)将FullJsonModel中的所需数据分配给您的RequiredJsonModel
5)将此RequiredJsonModel传递给您的ElasticData
public class Addressee
{
public string addressee { get; set; }
public string title_name { get; set; }
public string salutation { get; set; }
public string comments { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string extention_group { get; set; }
public string ext { get; set; }
}
public class Company
{
public string id { get; set; }
public Addressee addressee { get; set; }
public string abbreviation { get; set; }
public string care_of { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string address_3 { get; set; }
public string state { get; set; }
public string zip { get; set; }
public bool is_deleted { get; set; }
public string company_code { get; set; }
public object parent_company { get; set; }
public string name { get; set; }
public string createdBy { get; set; }
public string modifiedBy { get; set; }
public DateTime createdDate { get; set; }
public DateTime modifiedDate { get; set; }
}
public class FullJsonModel
{
public string userId { get; set; }
public Company company { get; set; }
}
下面是将保存json中所需数据的类:
public class RequiredJsonModel
{
public string name { get; set; }
public string id { get; set; }
}
这是将json数据映射到类的方法:
FullJsonModel objFullJson = JsonConvert.DeserializeObject<FullJsonModel>(YourJsonObject);
现在创建RequiredJsonModel的对象并为其分配值:
RequiredJsonModel objReqJson = new RequiredJsonModel();
objReqJson.name = objFullJson.Company.name;
objReqJson.id = objFullJson.Company.id;
现在将此objReqJson传递给您的弹性搜索