Trouble building query from Java

Hello all,

I'm having trouble creating a function score query to match this query I made via HTTP:

curl -X POST
'http://localhost:9200/myIndex/_search?explain=&offset=1000&limit=10'
-H 'cache-control: no-cache'
-H 'content-type: application/json'
-H 'postman-token: cc6fe50f-ef69-08b8-2e8b-c301b964a109'
-d '{
"from": 0,
"size": 2000,
"query": {
"function_score": {
"boost_mode": "replace",
"score_mode": "sum",
"functions": [
{
"filter": {
"match": {
"propertyToMatch1": "valueToMatch1"
}
},
"weight": 1
},
{
"filter": {
"match": {
"propertyToMatch1": "valueToMatch2"
}
},
"weight": 1
}],
"query" : {
"bool" : {
"must" : [ {
"match" : {
"propertyToNotMatch1" : {
"query" : true,
"type" : "boolean"
}
}
}, {
"match" : {
"propertyToNotMatch2" : {
"query" : true,
"type" : "boolean"
}
}
}, {
"bool" : {
"should" : {
"match" : {
"propertyToNotMatch3" : {
"query" : "value3",
"type" : "boolean"
}
}
}
}
}, {
"bool" : {
"should" : [ {
"match" : {
"propertyToMatch1" : {
"query" : "valueToMatch1",
"type" : "boolean"
}
}
}, {
"match" : {
"propertyToMatch1" : {
"query" : "valueToMatch1",
"type" : "boolean"
}
}
}]
}
}, {
"bool" : {
"should" : {
"match" : {
"propertyToNotMatch4" : {
"query" : "value4",
"type" : "boolean"
}
}
}
}
} ]
}
}
}
},
"_source": false,
"sort": [{
"_score": {
"order": "desc"
}
}],
"track_scores": true
}'

The main trouble I'm having with here is adding the filter properties that is embedded in the functions array above. the ScoreFunctionBuilders doesn't seem to have what I'm looking for. It does have weightFactorFunction, but it doesn't seem to allow me to add a filter with it. If I could get some direction for adding a filter to the functions array, I can probably figure out the rest.

This is all I have so far for building the function score query:

    FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(buildSearchQuery(getSearchAttributes(request)));
    functionScoreQueryBuilder.scoreMode(FiltersFunctionScoreQuery.ScoreMode.Sum.name());
    functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE.name());

    functionScoreQueryBuilder.add(ScoreFunctionBuilders.weightFactorFunction(1));

Thanks for taking the time for reading this! Any help is appreciated.

I figured it out, for anyone having that question in the future. You can do something like this (note this is 2.4 code):

    FunctionScoreQueryBuilder functionScoreQueryBuilder = functionScoreQuery(buildSearchQuery(getSearchAttributes(request)));
    functionScoreQueryBuilder.scoreMode(FiltersFunctionScoreQuery.ScoreMode.Sum.name());
    functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE.name());
    
    functionScoreQueryBuilder.add(matchQuery("propertyToMatch1", "valueToMatch1"), weightFactorFunction(3));

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