FunctionScoreQueryBuilder


(Panagiota Papagianni) #1

Hi,

I want to upgrade from ES 1.7 to 6.0. I made all necessary changes and now I have a problem with FunctionScoreQueryBuilder.

I create a BoolQueryBuilder filter and works fine. Now I want to add score to my results but I can't use this filter in FunctionScoreQueryBuilder because of its type.

Here is my code using in ES 1.7

FunctionScoreQueryBuilder fsqb = new FunctionScoreQueryBuilder(QueryBuilders.filteredQuery(!esc.keywords ? QueryBuilders.matchAllQuery() : QueryBuilders.wildcardQuery("searchField", ""+esc.keywords.toUpperCase()+""), andFilter));

    fsqb.add(ScoreFunctionBuilders.scriptFunction("_score * (doc['bocHotelId'].value!=0? 50000:1) "))
    fsqb.add(ScoreFunctionBuilders.scriptFunction("_score * (doc['bookingUrl'].value!=null && doc['bookingUrl'].value!='null' && doc['bookingUrl'].value!='' ? 40000:1) "))
    fsqb.add(ScoreFunctionBuilders.scriptFunction("_score * (doc['claimed'].value!=0? 30000:1) "))
    fsqb.add(ScoreFunctionBuilders.scriptFunction("_score * (doc['logoFilename'].value!=null && doc['logoFilename'].value!=''? 20000:1) "))
    fsqb.add(ScoreFunctionBuilders.scriptFunction("_score * (doc['source'].value.equals('filoxeno') && doc['source'].value!=null? 10000:1) "))

And this is my code for filter creation in ES 6.0
def buildFilter(ElasticSearchCriteria esc){
BoolQueryBuilder andfilter = new BoolQueryBuilder();

    if(esc.polygonType.equals("periphery")){
        andfilter.filter(QueryBuilders.termQuery("municipality",esc.polygonId))
        /*filter=FilterBuilders.termFilter("periphery", esc.polygonId)*/

    }

    else if(esc.polygonType.equals("state")){
        andfilter.filter(QueryBuilders.termQuery("state",esc.polygonId))

    }else if(esc.test.equals("yes")){
        andfilter.filter(QueryBuilders.termQuery("municipality",esc.polygonId))
        /*filter=FilterBuilders.termFilter("municipality", esc.polygonId)*/
    }

    else{
        andfilter.filter(QueryBuilders.termQuery("municipality",esc.polygonId))
    }

    if(esc.type.equals("accommodation")){
        andfilter.filter(QueryBuilders.termQuery("filoxenoType",esc.type))
    }
    else if(esc.type.equals("restaurant")){
        andfilter.filter(QueryBuilders.termQuery("filoxenoType",esc.type))
    }
    else if(esc.type.equals("rental")){
        andfilter.filter(QueryBuilders.termQuery("filoxenoType",esc.type))
    }
    else if(esc.type.equals("activity")){
        andfilter.filter(QueryBuilders.termQuery("filoxenoType",esc.type))
    }
    else{
        andfilter.filter(QueryBuilders.termQuery("filoxenoType",esc.type))
    }

    andfilter.filter(QueryBuilders.termQuery("language",esc.language))
    andfilter.filter(QueryBuilders.termQuery("published",1))

    esc.terms.each{ k, v ->
        if(k.toString().contains(".")){
            if( !v[0].getClass().isArray() ){
                andfilter.filter(QueryBuilders.nestedQuery(k.toString().split(/\./)[0],QueryBuilders.termQuery(k.toString(), v[0]),ScoreMode.None))

            }
            else{
                v[0].each {
                    andfilter.filter(QueryBuilders.nestedQuery(k.toString().split(/\./)[0],QueryBuilders.termQuery(k.toString(), it),ScoreMode.None))
                }
            }
        }else if(k.toString().contains("availability")){
            andfilter.filter(QueryBuilders.existsQuery("bookingUrl"))

        }
        else{
            if( !v[0].getClass().isArray() ){
                andfilter.filter(QueryBuilders.termQuery(k, v[0]))
            }
            else{
                v[0].each {
                    andfilter.filter(QueryBuilders.termQuery(k, it))
                }
            }
        }
    }

    if(esc.keywords){
        andfilter.filter(QueryBuilders.wildcardQuery("searchField", "*"+esc.keywords.toUpperCase()+"*"))
    }

    return andfilter
}

Is there any idea how can I use FunctionScoreQueryBuilder with this filter, or how can I change this filter in order to use it in FunctionScoreQueryBuilder??

Thank you in advance!!


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.