Node.js

[Node] node로 Elasticsearch index에 필드 추가

SongMinu 2019. 11. 20. 16:44
728x90

add_fields.js

var elasticsearch = require('elasticsearch');
var rl = require('readline');

var conf = require('./conf');

var data = require('./field_data/index_fields');
var info;

//npm install elasticsearch
var client = new elasticsearch.Client({
    hosts: ["https://" + conf.els_id + ":" + conf.els_pw + "@" + conf.els_ip + ":" + conf.els_port]
});

var r = rl.createInterface({
    input: process.stdin,
    output: process.stdout
});

r.question("Input function : ", async function (answer) {
    var input = answer.split(" ");
    if (input == 'all') {
        var data_key = Object.keys(data);
        for (var i in data_key) {
            info = data[data_key[i]]();
            let run = await getFieldsInfo(info);
        }
    } else {
        info = data[input]();
        let run = await getFieldsInfo(info);
    }
    r.close();
});

async function getFieldsInfo(data) {
    var index = data.index;
    var prop = data.properties;
    console.log("Info....................................");
    console.log("index : " + index);
    console.log("properties : " + prop);
    let run = await addFields(index, prop);
};

let addFields = function (idx, info) {
    return new Promise(function (resolve, reject) {
        client.indices.putMapping({
            index: idx,
            type: 'doc',
            body: {
                properties: info
            }
        }, function(err, resp, status) {
            if (err) {
                console.log("err!!!");
                console.log(err);
                console.log("End---------------------");
                reject(err)
            } else {
                console.log(resp);
                console.log("End---------------------");
                resolve(resp);
            }
        });
    });
};

 

index_fields.js

exports.index_field_1 = function () {
    return {
        index: 'ts_accident_template',
        properties: {
            "ADD_FIELD1": {
                "type":"keyword"
            },
            "ADD_FIELD2": {
                "type": "keyword"
            },
            "ADD_OBJECT": {
                "properteis":{
                    "OBJ_1": {
                        "type":"keyword"
                    },
                    "OBJ_2": {
                        "type": "integer"
                    }
                }
            }
        }
    }
};

exports.index_field_2 = function () {
    return {
        index: 'ts_institution_assets',
        properties: {
            "ADD_FIELD1": {
                "type":"keyword"
            }
        }
    }
}

 

node add_fields.js 입력하면 Input function : 이라고 나온다

그럼 거기다 index_fiedls.js에서 만든 함수를 입력하면 해당 함수 안에 있는 데이터를 가져와서

인덱스명과 맵핑 정보를 이레스틱에 넣는 방식

all을 치면 index_fiedls.js에 있는 함수들을 다 가져와서 하나씩 처리함

 

input 형식으로 만든 이유

일하다보니 이미 만들어져있는 인덱스에 필드를 주가해야하는 경우가 계속 생기고 있는데

파일 하나에 인덱스별로 맵핑 정보를 넣어서 필요할 때마다 해당 인덱스만 처리하는걸 하고 싶었음

특히 다른 서버에 패치를 할 때 이 파일을 이용하면 편할것 같아서 이런식으로 만듬

 

https://github.com/smw0807/minu_1/tree/master/node/add_data

반응형