KozhevnikovDmitry
KozhevnikovDmitry

Reputation: 1720

ElasticSearch sorting by regexp

I have a field in ElasticSearch 6 index that can be matched with regexp. I need to sort search result so documents with values, that matches go before the ones, that don't. Is there some way to use regexp in sorting clause?

Example document:

 "mappings" : {
  "unit" : {
    "properties" : {
      "description" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }

I thought about script sorting kind of this:

  "sort" : {
    "_script" : {
        "type" : "number",
        "script" : {
            "source": "regex('some_regexp_here').match(doc['description'].value) ? 1 : 0 ",
        },
        "order" : "desc"
      }
   }

Is it possible? Are there any other workarounds? Thank you.

Upvotes: 0

Views: 573

Answers (1)

KozhevnikovDmitry
KozhevnikovDmitry

Reputation: 1720

I figured this out. Sort clause should be like this:

"sort": {
  "_script": {
    "order": "desc",
    "type": "number",
    "script": {
      "source": 
         "def m = /my_regex_here/.matcher(doc['description'].value);
          if(m.matches()) {
            return 1
          } else {
            return 0
          }"
    }
  }
}

Note that '/' symbols around regexp are required.

Upvotes: 1

Related Questions