es - elasticsearch search - DSL - function_score - script_score

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问:script_score有什么特点?
答:
在这里插入图片描述
问:script_score如何使用?
答:

# function_score
# 映射
PUT /function_score_2_test
{"mappings": {"properties": {"name": {"type": "text"},"num" : {"type": "integer"}}}
}# 索引
POST /function_score_2_test/_doc/1
{"name": "hello","num" : 1
}# 索引
POST /function_score_2_test/_doc/2
{"name": "hello good","num" : 100
}# 索引
POST /function_score_2_test/_doc/3
{"name": "good hello","num" : 5
}# 索引
POST /function_score_2_test/_doc/4
{"name": "hello hello","num" : 10
}# 搜索 - 普通匹配
GET /function_score_2_test/_search
{"query": {"match": {"name": "hello"}}
}# 结果
{"took" : 10,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 0.13927484,"hits" : [{"_index" : "function_score_2_test","_type" : "_doc","_id" : "4","_score" : 0.13927484,"_source" : {"name" : "hello hello","num" : 10}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "1","_score" : 0.12776,"_source" : {"name" : "hello","num" : 1}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "2","_score" : 0.099543065,"_source" : {"name" : "hello good","num" : 100}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "3","_score" : 0.099543065,"_source" : {"name" : "good hello","num" : 5}}]}
}# 搜索 - script_score
GET /function_score_2_test/_search
{"query": {"function_score": {"query": {"match": {"name": "hello"}},"script_score": {"script": {"source": "Math.log(2 + doc['num'].value)"}}}}
}# 结果
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 0.46038398,"hits" : [{"_index" : "function_score_2_test","_type" : "_doc","_id" : "2","_score" : 0.46038398,"_source" : {"name" : "hello good","num" : 100}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "4","_score" : 0.34608495,"_source" : {"name" : "hello hello","num" : 10}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "3","_score" : 0.19370186,"_source" : {"name" : "good hello","num" : 5}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "1","_score" : 0.1403587,"_source" : {"name" : "hello","num" : 1}}]}
}# 搜索 - script_score - 预设参数,以使script能够缓存
GET /function_score_2_test/_search
{"query": {"function_score": {"query": {"match": {"name": "hello"}},"script_score": {"script": {"params": {"a": 1.3,"b": 13}, "source": "params.a / Math.log(2 + doc['num'].value)"}}}}
}# 结果
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 0.1511798,"hits" : [{"_index" : "function_score_2_test","_type" : "_doc","_id" : "1","_score" : 0.1511798,"_source" : {"name" : "hello","num" : 1}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "4","_score" : 0.07286281,"_source" : {"name" : "hello hello","num" : 10}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "3","_score" : 0.06650152,"_source" : {"name" : "good hello","num" : 5}},{"_index" : "function_score_2_test","_type" : "_doc","_id" : "2","_score" : 0.027979836,"_source" : {"name" : "hello good","num" : 100}}]}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部