如何为接近句子开头的搜索提供更多分数
query: ipad
phrase2: Apple other word Ipad pro 1
phrase1: Apple pro ipad
phrase2: Apple pro 1
phrase3: Ipad pro 1
GET _search
{
"query": {
"match" : {
"name" : {
"query" : "ipad"
}
** (....) **
}
}
}
按分数结果应用于查询顺序:
phrase3
phrase1
phrase2
我做了这个脚本,但它不起作用
GET /store/products/_search
{
"_source": "name",
"query": {
"function_score": {
"query": {
"query_string" : {
"query": "apple"
, "fields":[
"name"
]
}
},
"functions": [
{
"script_score":{
"script": "def termInfo = []; termInfo = _index['name'].get('apple',_POSITIONS); positions = [];score = 0;for (pos in termInfo) { if(positions.contains(pos.position)){ next;} score = score + pos.position;positions.add(pos.position)}return positions.size();"
}
}
],"boost_mode":"replace"
}
}
}
我无法访问变量" _index ..." 运行脚本会显示此错误
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... rmInfo = []; termInfo = _index['name'].get('apple ...",
" ^---- HERE"
], ...
我的eslastic search版本是5.0
答案 0 :(得分:0)
我认为你错过了ES5文档中的“内联”属性 https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-function-score-query.html#function-script-score
试试这个
"script_score":{
"script": {
"inline": "def termInfo = []; termInfo = _index['name'].get('apple',_POSITIONS); positions = [];score = 0;for (pos in termInfo) { if(positions.contains(pos.position)){ next;} score = score + pos.position;positions.add(pos.position)}return positions.size();"
}
}