简体   繁体   中英

How to take create, using Javascript, an Index in Elasticsearch with rows' name taken from an array?

I want to create an Index in Elasticsearch using Javascript. The name of each column is in a different position of an array and every field of each row in another array. How should I "fill" the body of the index? Let's say a have:

arr_1 = [row1, row2, row3, row4];
arr_2 = [field1, field2, field3, field4];

then I want something like:

client.index(
    index: name_index,
    type: name_type,
    body: {
        row1 : field1; //arr1_[1] : arr_2[1],
        row2 : field2; //arr1_[2] : arr_2[2],
        .....
        ....
    }
)

This seems to accomplish what you want, if I'm understanding you correctly:

var arr_1 = ["row1", "row2", "row3", "row4"];
var arr_2 = ["field1", "field2", "field3", "field4"];

var doc_body = {};
arr_1.map(function(val, idx){ doc_body[val] = arr_2[idx]; });

client.index({ 
    index: 'test_index',
    type: 'mytype',
    id: '1', 
    body: doc_body
});

Here is a self-contained html document that implements it:

<html>
    <head>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/elasticsearch/3.0.2/elasticsearch.jquery.min.js"></script>
    </head>
    <body>
        <div><button id="delete_index_btn">Delete Index</button></div>
        <div><button id="create_index_btn">Create Index</button></div>
        <div><button id="add_docs_btn">Add Docs</button></div>
        <div>
            <h3>Response:</h3>
            <pre id="response"></pre>
        </div>
        <script type="text/javascript">
            $(function(){
                var client = new $.es.Client({
                    hosts: 'localhost:9200'
                });

                $("#delete_index_btn").click(function(){
                    client.indices.delete({ 
                        index: 'test_index' 
                    }).then(function(resp){
                        $("#response").append("\nclient.indices.delete: " + JSON.stringify(resp));
                    }, function(err){
                        $("#response").append("\nclient.indices.delete ERROR: " + JSON.stringify(err));
                    });
                });

                $("#create_index_btn").click(function(){
                    client.indices.create({ 
                        index: 'test_index' 
                    }).then(function(resp){
                        $("#response").append("\nclient.indices.create: " + JSON.stringify(resp));
                    }, function(err){
                        $("#response").append("\nclient.indices.create ERROR: " + JSON.stringify(err));
                    });
                });

                $("#add_docs_btn").click(function(){
                    var arr_1 = ["row1", "row2", "row3", "row4"];
                    var arr_2 = ["field1", "field2", "field3", "field4"];
                    var doc_body = {};
                    arr_1.map(function(val, idx){ doc_body[val] = arr_2[idx]; });

                    client.index({ 
                        index: 'test_index',
                        type: 'mytype',
                        id: '1', 
                        body: doc_body
                    }).then(function(resp){
                        $("#response").append("\nclient.index: " + JSON.stringify(resp));
                    }, function(err){
                        $("#response").append("\nclient.index ERROR: " + JSON.stringify(err));
                    });
                });
            });

        </script>
    </body>
</html>

Does that solve your problem?

Thank you, I solved using this:

var doc_body= {};

for(i = 0; i = arr_1.length; i++){
    doc_body[arr_1[i]] = arr_2[i];
}

client.index({ 
    index: 'test_index',
    type: 'mytype',
    id: '1', 
    body: doc_body
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM