Search code examples
typescriptelasticsearchelasticsearch-dsl

Converting the elasticjs query to native elastic search DSL query in node app using typescript


I am trying to convert my query that has been done using elastic js which is full scale library now that fullscale librabry has not updated for a while since 2014. I wanted to convert all my queries that are written using fullscale to native elastic search DSL. I am having a small problem while doing. This is what it looks like below

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

I am trying to achieve something like this

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

Any help will be appreciated Thanks.


Solution

  • After me experimenting how to do i for the solution which is working for me i am open for suggestions in making my answer better.

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