I am trying to use a painless script sort and am experiencing different results when I change the size
value of the query, I would expect that since sort happens before paging that the same query with a different size
would always produce the same sort values.
In the context of an ecommerce marketplace the script sort is used to sort the result into chunks of products made my different sellers, then sort inside those chunks by "reputationScore".
My sort looks like:
sort: [{
_script: {
script: {
source: `
def sellerId = doc['sellerId'].value.toString();
def val = params.sellerMap[sellerId];
if (val == null) {
params.sellerMap[sellerId] = 0;
return 0;
} else {
params.sellerMap[sellerId] = val + 1;
return val + 1;
}`,
params: {
sellerMap: {},
},
},
type: 'number',
order: 'asc',
},
}, {
reputationScore: 'desc',
}]
The full query is not much more complicated:
GET local_variant_search/_search
{
"sort": [
{
"_script": {
"script": {
"source": "\n def sellerId = doc['sellerId'].value.toString();\n def val = params.sellerMap[sellerId];\n if (val == null) {\n params.sellerMap[sellerId] = 0;\n return 0;\n } else {\n params.sellerMap[sellerId] = val + 1;\n return val + 1;\n }",
"params": {
"sellerMap": {}
}
},
"type": "number",
"order": "asc"
}
},
{
"reputationScore": {
"order": "desc"
}
}
],
"size": 1000,
"from": 0
}
Using "size": 1
, "size": 10
, and "size": 1000
all produce different first results on my data set, notably results often have the script result in a score of 1
without another product with the same sellerId being assigned 0
, which stops happening when size
is the same as the result set.
Is there any insight about how these script sorts are executed that can help me understand this behavior?