0

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],
        .....
        ....
    }
)
Marco
  • 1
  • 3
  • 1
    Here is a JavaScript "zip" function that seems to do what you want: http://stackoverflow.com/questions/4856717/javascript-equivalent-of-pythons-zip-function – Sloan Ahrens Jan 06 '15 at 15:01
  • I've seen the solution but I don't understand how it should help me – Marco Jan 07 '15 at 09:47

2 Answers2

0

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?

Sloan Ahrens
  • 8,588
  • 2
  • 29
  • 31
0

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
});
Marco
  • 1
  • 3