代码之家  ›  专栏  ›  技术社区  ›  wuno

用弹性搜索JavaScript API实现批量数据的索引

  •  1
  • wuno  · 技术社区  · 6 年前

    背景

    在我的节点应用程序中,我正试图索引来自SQL表的返回值。我一直在阅读文档,我想我应该使用bulk函数来完成这项工作。我试图使用嵌套数据类型,但发现文档有点不清楚。

    这里有三个功能在起作用。

    1. 指数
    2. 电脑绘图
    3. 大量的

    当我尝试添加一个索引时,它工作得很好。但是,当我尝试添加散列索引时,它表示索引不存在。这使我假设我需要创建索引,然后使用 putMapping . 不管怎样,我都会出错。最近我发现了一个错误,

    “请求正文是必需的”},“状态”:400}',

    问题

    我在尝试创建索引时遇到此错误,我正尝试在没有body键的情况下创建索引。我想我需要先用映射创建索引,这样我就可以输入大量数据了。我想我的问题是我没有使用正确的函数。

    例子

    指数

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

    电脑绘图

    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({})
    

    问题

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

    我的目标是大容量插入具有 里面的东西。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Val    6 年前

    你没有使用正确的函数。 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() 从数据库中提取数据。