簡體   English   中英

如何使用Javascript在Elasticsearch中創建索引,並從數組中獲取行名?

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

我想使用Javascript在Elasticsearch中創建索引。 每列的名稱在一個數組的不同位置,而每行的每個字段在另一個數組中。 我應該如何“填充”索引主體? 假設有一個:

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

然后我想要類似的東西:

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

如果我正確理解您的話,這似乎可以完成您想要的:

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

這是一個實現它的獨立HTML文檔:

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

這樣可以解決您的問題嗎?

謝謝,我解決了這個問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM