我在ElasticSearch 6索引中有一个可以与regexp匹配的字段。我需要对搜索结果进行排序,以便具有匹配值的文档排在匹配值之前,而不匹配。可以在排序子句中使用regexp吗?
示例文档:
placeholder
我想到了这样的脚本排序方式:
"mappings" : {
"unit" : {
"properties" : {
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
有可能吗?还有其他解决方法吗?谢谢。
答案 0 :(得分:1)
我知道了。排序子句应如下所示:
"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
}"
}
}
}
请注意,regexp周围需要'/'符号。