Painless equivalent of simple groovy script (substring issues?)


(reflection) #1

The following works in groovy but not in painless; any ideas what I'm doing wrong (using elasticsearch-dsl-py but that shouldn't matter)?:

.bucket('protocol', 'terms', script={
          'inline': 'def protocol = doc["name"].value[0..4]; \
                     if (protocol == "https") { \
                       return "https"; \
                     } else if (protocol[0..3] == "http") { \
                       return "http"; \
                     } else { \
                       return "other"; \
                     }',
          'lang': 'groovy'
        }, size=100)

.bucket('protocol', 'terms', script={
          'inline': 'def protocol = doc["name"].value.substring(0, 4); \
                     if (protocol == "https") { \
                       return "https"; \
                     } else if (protocol.substring(0, 3) == "http") { \
                       return "http"; \
                     } else { \
                       return "other"; \
                     }',
          'lang': 'painless'
        }, size=100)

(Nik Everett) #2

'https://blah'.substring(0, 5) returns https for me. Looks like substring is exclusive while Groovy's [0..4] is inclusive.


(reflection) #3

Thanks, Nik! Feel dumb for just assuming that groovy's slick notation and substring were the same.


(system) #4

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