elasticsearchwildcard-mapping

Get Elastic Search Results with least difference between two Stringified lists


I have one of the field indexed as String of list of strings eg> "["string1", "string2","string3","string4","string5"]"

In that column I have values:

  1. "["string1", "string2","string3","string4","string5"]"
  2. "["string1", "string2","string3","string4","string5","string6","string7"]"

on this columns, lets call it "testColumn", I will pass a input as "query": "["string1", "string2","string3","string4"*" (with wildcard) I want my results to be matched with the least difference with my input string.

so I want my first output "["string1", "string2","string3","string4","string5"]" and then "["string1", "string2","string3","string4","string5","string6","string7"]" because my query string matches both the records but, the difference between the length of both string is less in first record.

I have tried using the wildcard but didn't work.

"wildcard":{
       "testColumn": {
              "boost": 0.0,
              "value":"[\"string1\", \"string2\",\"string3\",\"string4\"*"
       }
}

Solution

  • How about using script to sort the matches by length?

    "sort": {
       "_script": {
          "type": "number",
          "order": "desc",
          "script": {
             "lang": "painless",
             "source": "doc['testColumn'].value.length()"
           }
       }
    }