我在ElasticSearch 6索引中有一个可以与regexp匹配的字段。我需要对搜索结果进行排序,以便具有匹配的值的文档排在那些不匹配的值之前。有什么方法可以在排序子句中使用regexp吗?
示例文件:
"mappings" : {
"unit" : {
"properties" : {
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
我考虑过这样的脚本排序:
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"source": "regex('some_regexp_here').match(doc['description'].value) ? 1 : 0 ",
},
"order" : "desc"
}
}
可能吗?还有其他解决方法吗?谢谢。
最佳答案
我想通了。 Sort子句应如下所示:
"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周围需要'/'符号。
关于regex - ElasticSearch按正则表达式排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52007895/