【问题标题】:Indexing bulk data with Elastic Search Javascript API使用 Elastic Search Javascript API 索引批量数据
【发布时间】:2018-07-27 01:42:50
【问题描述】:

背景

在我的节点应用程序中,我试图从 SQL 表中索引返回的值。我一直在阅读文档,我认为我应该使用批量功能来做到这一点。我正在尝试使用嵌套数据类型,但我发现文档有点不清楚。

这里有三个功能。

  1. 索引
  2. 放置映射
  3. 散装

当我尝试添加一个索引时,它工作正常。但是当我尝试添加批量索引时,它说索引不存在。这让我假设我需要创建索引,然后使用putMapping 添加嵌套映射。无论哪种方式,我尝试都会出错。最近我收到错误,

"请求正文是必需的"},"status":400}',

问题

当我尝试创建在没有正文键的情况下尝试创建的索引时出现此错误。我在想我需要先用映射创建索引,这样我才能输入批量数据。我认为我的问题是我没有使用正确的功能。

示例

索引

exports.createIndex = () => {
    esClient.index({
        index: "products",
        type: "product",
        refresh: "true"
    }, (error, response) => {
        if(error) {
            console.log('Put Index Error ', error);
        } else {
            this.createMapping();
            console.log('Put Index Response ', response);
        }
    });
};

putMapping

exports.createMapping = () => {
    esClient.indices.putMapping({
        index: "products",
        type: "product",
        body: {
            mappings: {
                product: {
                    properties: {
                        variantId: { type: "text" },
                        productId: { type: "text" },
                        keyStrengths: { type: "text" },
                        something: {
                            type: "nested",
                            properties: {
                                type: { type: "text" },
                                label: { type: "text" },
                                items: {
                                    type: "nested",
                                    properties: {
                                        value: {
                                            type: "text"
                                        },
                                        characteristic: {
                                            type: "text"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }, (error, response) => {
        if(error) {
            console.log('Put Index Error ', error);
        } else {
            console.log('Put Index Response ', response);
        }
    });
};

散装

   esClient.bulk({})

问题

请告诉我创建新索引的正确方法,然后批量插入从数据库返回的数据。我不清楚我是否需​​要使用所有这三个功能,或者我在哪里出错了。

我的目标是批量插入具有数组的对象数组 里面的对象。

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    您没有使用正确的功能。 esClient.index() 用于索引文档,而不是创建索引。在您的情况下,正确的做法是调用esClient.indices.create() 以便使用适当的映射创建索引,然后您可以调用esClient.bulk()。它是这样的:

    exports.createIndex = () => {
        esClient.indices.create({
            index: "products",
            body: {
                mappings: {
                    product: {
                        properties: {
                            variantId: { type: "text" },
                            productId: { type: "text" },
                            keyStrengths: { type: "text" },
                            something: {
                                type: "nested",
                                properties: {
                                    type: { type: "text" },
                                    label: { type: "text" },
                                    items: {
                                        type: "nested",
                                        properties: {
                                            value: {
                                                type: "text"
                                            },
                                            characteristic: {
                                                type: "text"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }, (error, response) => {
            if(error) {
                console.log('Create Index Error ', error);
            } else {
                console.log('Create Index Response ', response);
            }
        });
    };
    

    完成后,您可以调用esClient.bulk() 并从数据库中提取数据。

    【讨论】:

    • 谢谢你!如果您不介意,在调用批量时,该函数中的类型是什么?在示例中,它显示类型、文本。我会再次将其称为嵌套类型,还是它如何知道使用我之前创建的映射?
    • 我建议您为批量部分创建另一个问题。向我们展示你是如何做到的。
    • 你介意告诉我这个函数在文档中的位置吗?我可以找到indices.create 但找不到indices.createIndex()。我不断得到,esClient.indices.createIndex 不是函数
    • 糟糕,我的错,该函数被称为indices.create 而不是indices.createIndex,对此感到抱歉。我已经更新了我的答案。
    猜你喜欢
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多