【问题标题】:Elasticsearch return typeElasticsearch 返回类型
【发布时间】:2020-02-07 06:42:20
【问题描述】:

我正在尝试简单地接收插入的项目,对其进行编辑并再次更新。不幸的是,这并不顺利。

代码尚未最终确定,只是 POC,如果您打算复制它,请不要在生产中使用这样的代码:)。

我的创建功能很好:

async create(index: string, obj: any, id: string): Promise<TModel> {
    let req: RequestParams.Create<TModel> = {
        body: obj,
        index: index,
        id: id,
    };
    try {
        var result = await this.client.create(req, {
            ignore:[409]
        });
        if (result.statusCode === 201) {
            return result.body;
        } else {
            console.log("Non succesfull call, result:", result);
            return null;
        }
    } catch (error) {
        console.log("create error", error);
        return null;
    }
}

我的更新功能大致相同

async update(index: string, obj: any, id: string): Promise<TModel> {
    try {
        let req: RequestParams.Update<TModel> = {
            body: obj,
            index: index,
            id: id
        };
        var result = await this.client.update(req);
        if (result.statusCode === 200 || result.statusCode === 201 || result.statusCode == 202 || result.statusCode == 204) {
            return result.body;
        } else {
            console.log("Non succesfull call, result:", result);
            return null;
        }
    } catch (error) {
        console.error("Error updating", error);
    }
}

我开始使用的搜索功能:

async search(index: string, searchBody: any): Promise<any> {

    try {
        var result = await this.client.search({
            index: index,
            body: searchBody
        });
        if (result.statusCode === 200) {
            return result.body.hits.hits;
        } else {
            console.log("Non succesfull call, result:", result);
            return null;
        }
    } catch (error) {
        console.log("Error getting result", error);
        return null;
    }
}

当插入和稍后搜索一个对象时,你当然会得到一个any 返回类型。了解数据的结构后,我会在某处执行以下操作以进行快速测试:

return result.body.hits.hits[0]._source

这给了我这个结果:

虽然对象的 JSON 格式似乎是正确的,但就像我创建的一样,它在更新数据时会引发错误:

所以我用谷歌搜索并尝试应用弹性自己提供的这个解决方案:

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/typescript.html

搜索代码:

async searchTyped(index: string, searchBody: any): Promise<TModel> {
    try {
        let req : RequestParams.Search<TModel> ={
            index : index,
             body : searchBody,
        };
        var result : ApiResponse<SearchResponse<TModel>> = await this.client.search(req);
        if (result.statusCode === 200) {
            if(result.body.hits.hits.length > 0){
                return result.body.hits.hits[0]._source;
            }
            return null;
        } else {
            console.log("Non succesfull call, result:", result);
            return null;
        }
    } catch (error) {
        console.log("Error getting result", error);
        return null;
    }
}

根据智能感知,这应该可以工作,_source 应该是 TModel 类型。

不幸的是,没有运气:

我是否在正确的道路上确保数据对象在更新之前是正确的?

我们希望坚持动态映射并且不定义所需的确切模型。我会假设使用此设置,请求正文包含 javascript Object 或 typescript 类型并不重要?

【问题讨论】:

    标签: node.js typescript elasticsearch


    【解决方案1】:

    最终它是什么对象并不重要,我只需要以不同的方式进行更新:

    async update(index: string, obj: any, id: string): Promise<any> {
        try {
            let req: RequestParams.Update<any> = {
                body: {
                    "doc": obj
                },
                index: index,
                id: id
            };
            var result = await this.client.update(req);
            if (result.statusCode === 200 || result.statusCode === 201 || result.statusCode == 202 || result.statusCode == 204) {
                return result.body;
            } else {
                console.log("Non succesfull call, result:", result);
                return null;
            }
        } catch (error) {
            console.error("Error updating", error);
        }
    }
    

    所以所有更新都应该在doc 之内,然后一切都很好。

    在本指南中找到:https://hub.packtpub.com/crud-create-read-update-delete-operations-elasticsearch/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-30
      • 2017-07-30
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2014-12-09
      相关资源
      最近更新 更多