试试这个查询
GET text/_search{ "query": { "function_score": { "query": { "match": { "text": "unemployment" } }, "functions": [ { "script_score": { "script": { "source": """ def docval = doc['text.keyword'].value; def length = docval.length(); def index = (float) docval.indexOf('unemployment'); // the sooner the word appears the better so 'invert' the 'index' return index > -1 ? (1 / index) : 0; """ } } } ], "boost_mode": "sum" } }}使用自动生成的映射
{ "text" : { "mappings" : { "properties" : { "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "text" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } }}请注意,这是区分大小写的,因此也有一个小写标准化的关键字字段,然后在脚本分数脚本中对其进行访问是合理的。这可能会让您走上正确的道路。



