In a painless script how do I iterate through document keys based on a pattern?

If somebody stumbles into this when they ddg'ing or so,

I figured this out, I feel pretty dumb it took this long to figure something so simple out.

I found this documentation very useful.

double total = 0.0;
for (key in params._source.keySet()) {
    if (key.startsWith("total")) {
        total = total + doc[key].value;
    }
}