ElasticSearch学习笔记-相关度得分记录

最近想要修改调整一下ElasticSearch里面Doc的Score,因而在ES官网查阅了一下,相关的介绍和说明仍是很是详细的,能作的修改调整也仍是比较多的,须要根据具体的情形去选择相应的方式作合适的调整修改,这里作个简单的记录,以便后续使用方便。html

相关解释描述能够参考链接:dom

https://www.elastic.co/guide/cn/elasticsearch/guide/current/controlling-relevance.htmlelasticsearch

https://www.elastic.co/guide/cn/elasticsearch/guide/current/function-score-query.htmlide

https://www.elastic.co/guide/cn/elasticsearch/guide/current/boosting-by-popularity.html函数

https://www.elastic.co/guide/cn/elasticsearch/guide/current/function-score-filters.htmlui

https://www.elastic.co/guide/cn/elasticsearch/guide/current/random-scoring.htmlspa

https://www.elastic.co/guide/cn/elasticsearch/guide/current/decay-functions.htmlcode

https://www.elastic.co/guide/cn/elasticsearch/guide/current/script-score.htmlorm

 

如下是API操做的部分记录:htm

 

ScoreFunctionBuilder scoreFunctionBuilder = 
ScoreFunctionBuilders.fieldValueFactorFunction(fieldName)
	.factor(boostFactor).modifier(modifier).missing(missing).setWeight(weight);
线性衰减函数  一旦直线与横轴 0 相交,全部其余值的评分都是 0.0
ScoreFunctionBuilders.linearDecayFunction(fieldName, origin, scale)
	.setDecay(decay).setOffset(offset).setWeight(weight)
指数衰减函数  先剧烈衰减而后变缓 
ScoreFunctionBuilders.exponentialDecayFunction(fieldName, origin, scale)
	.setDecay(decay).setOffset(offset).setWeight(weight)
高斯衰减函数  高斯函数是钟形的——它的衰减速率是先缓慢,而后变快,最后又放缓。
ScoreFunctionBuilders.gaussDecayFunction(fieldName, origin, scale)
	.setDecay(decay).setOffset(offset).setWeight(weight)
origin  中心点或字段可能的最佳值,落在原点 origin上的文档评分 _score 为满分 1.0 。
scale  衰减率,即一个文档从原点 origin下落时,评分 _score改变的速度。
decay  从原点 origin衰减到 scale所得的评分 _score,默认值为 0.5 。
offset  以原点 origin为中心点,为其设置一个非零的偏移量 offset覆盖一个范围,而不仅是单个原点。
	在范围 -offset <= origin <= +offset内的全部评分 _score 都是 1.0 。
随机评分
ScoreFunctionBuilders.randomFunction(seed)
权重因素
ScoreFunctionBuilders.weightFactorFunction(weight)

 

脚本评分

 
  1. String timeField = getTypeTimeField(type);

  2. if (StringUtils.isBlank(timeField)) {

  3. searchRequestBuilder.setQuery(buildBoolQuery(params.keywords(), attributes));

  4. } else {

  5. String inlineScript = ElasticScriptUtils.scriptWithScoreAndTime(timeField, System.currentTimeMillis());

  6. Map<String, Object> sparams = new HashMap<>();

  7. Script script = new Script(inlineScript, ScriptType.INLINE, "groovy", sparams);

  8. ScoreFunctionBuilder scoreFunctionBuilder = ScoreFunctionBuilders.scriptFunction(script);

  9. searchRequestBuilder.setQuery(QueryBuilders.functionScoreQuery(

  10. buildBoolQuery(params.keywords(), attributes), scoreFunctionBuilder));

  11. }

 

 
  1. public class ElasticScriptUtils {

  2.  
  3. public static String scriptWithScoreAndTimeV1(String field, long currentTime) {

  4. String script = ""

  5. + "field = (null==_source." + field + "?\"1970-01-01 12:00:00\":source." + field + ");"

  6. + "format = \"\";"

  7. + "ds = field.trim().split(\":\");"

  8. + "if (ds.length == 3) {"

  9. + " format = \"yyyy-MM-dd HH:mm:ss\";"

  10. + "} else if (ds.length == 2) {"

  11. + " format = \"yyyy-MM-dd HH:mm\";"

  12. + "} else if (ds.length == 1) {"

  13. + " ds = d.trim().split(\" \");"

  14. + " if (ds.length == 2) {"

  15. + " format = \"yyyy-MM-dd HH\";"

  16. + " } else if (ds.length == 1) {"

  17. + " ds = d.trim().split(\"-\");"

  18. + " if (ds.length == 3) {"

  19. + " format = \"yyyy-MM-dd\";"

  20. + " } else if (ds.length == 2) {"

  21. + " format = \"yyyy-MM\";"

  22. + " } else if (ds.length == 1) {"

  23. + " format = \"yyyy\";"

  24. + " }"

  25. + " }"

  26. + "};"

  27. + "parse_date = Date.parse(format, field).getTime();"

  28. + "return _score.doubleValue() + (parse_date / " + currentTime + ");";

  29. return script;

  30. }

  31.  
  32. public static String scriptWithScoreAndTime(String fields, long currentTime) {

  33. String script = ""

  34. + "temp_fields = \"" + fields + "\".trim().split(\",\");"

  35. + "target_field = temp_fields[0];"

  36. + "temp_field = _source.target_field;"

  37. + "for (i in 2.. temp_fields.length) {"

  38. + " if (null != temp_field) break;"

  39. + " target_field = temp_fields[i-1];"

  40. + " temp_field = _source.target_field;"

  41. + "};"

  42. + "field = (null==temp_field ? \"1970-01-01 12:00:00\" : temp_field);"

  43. + "format = \"\";"

  44. + "ds = field.trim().split(\":\");"

  45. + "if (ds.length == 3) {"

  46. + " format = \"yyyy-MM-dd HH:mm:ss\";"

  47. + "} else if (ds.length == 2) {"

  48. + " format = \"yyyy-MM-dd HH:mm\";"

  49. + "} else if (ds.length == 1) {"

  50. + " ds = d.trim().split(\" \");"

  51. + " if (ds.length == 2) {"

  52. + " format = \"yyyy-MM-dd HH\";"

  53. + " } else if (ds.length == 1) {"

  54. + " ds = d.trim().split(\"-\");"

  55. + " if (ds.length == 3) {"

  56. + " format = \"yyyy-MM-dd\";"

  57. + " } else if (ds.length == 2) {"

  58. + " format = \"yyyy-MM\";"

  59. + " } else if (ds.length == 1) {"

  60. + " format = \"yyyy\";"

  61. + " }"

  62. + " }"

  63. + "};"

  64. + "parse_date = Date.parse(format, field).getTime();"

  65. /**

  66. + "println _score.doubleValue(); println (parse_date / " + currentTime + ");"

  67. **/

  68. + "return _score.doubleValue() + (parse_date / " + currentTime + ");";

  69. return script;

  70. }

  71.  

 

https://www.elastic.co/guide/cn/elasticsearch/guide/current/decay-functions.html

相关文章
相关标签/搜索