使用打字稿在Node App中将Elasticjs查询转换为原生Elastic Search DSL查询

时间:2019-01-10 09:55:12

标签: typescript elasticsearch elasticsearch-dsl

我正尝试将使用弹性js完成的查询转换为全尺寸库,因为自2014年以来全尺寸librabry已有一段时间没有更新。我想将使用全尺寸编写的所有查询转换为原生弹性搜索DSL。我在执行时遇到一个小问题。 如下所示

function getFilterNative(field: string, type: string, values: string[]): any {
    if (type === "equals") {

         return ejs.TermFilter(field, values[0].toLowerCase());

    }
     var arrFilter: Array<any> = [];
     for (var i in values) {
        arrFilter.push(ejs.TermFilter(field, values[i].toLowerCase()));
    }
    return ejs.OrFilter(arrFilter);
}

我正在努力实现这样的目标

function getFilterNative(field: string, type: string, values: string[]): any {
    if (type === "equals") {

        //return ejs.TermFilter(field, values[0].toLowerCase());
        var termFilter ={
            "term":{}
    //error in here when compiling the ts file
//error TS2403: Subsequent variable declarations must have the same type.  Variable 'termFilter' must be of type '{ "term": {}; }', but here has type '{ terms: { field: string; }; }'.




        };
        termFilter["term"][field] =  values[0].toLowerCase();
    }
    var orFilter = {filter:
        {
            or: {
                filters: []
            }
        }};
    var arrFilter: Array<any> = [];
    for (var i in values) {
        var termFilter = {
            terms: {
                field: values[i].toLowerCase()
            }
        };
        arrFilter.push(termFilter)
        //arrFilter.push(ejs.TermFilter(field, values[i].toLowerCase()));
    }
    orFilter.filter.or.filters = arrFilter;
    return orFilter;
    //return ejs.OrFilter(arrFilter);
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在我尝试了如何为自己解决的解决方案做我之后,我愿意提出建议以使我的答案更好。

function getFilterNative(field: string, type: string, values: string[]): any {
    if (type === "equals") {
        var filterVal: { [k: string]: any } = {};
        filterVal[field] = values[0].toLowerCase();
        var termFilter = {
            term: filterVal
        };
        return termFilter;
    };

    var orFilter = {
        filter:
            {
                or: {
                    filters: []
                }
            }
    };
    var arrFilter: Array<any> = [];
    for (var i in values) {
        var filterVal: { [k: string]: any } = {};
        filterVal[field] = values[i].toLowerCase()
        var termFilter = {
            term: filterVal
        };
        arrFilter.push(termFilter)
    }
    orFilter.filter.or.filters = arrFilter;
    return orFilter;
}