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

相關文章
相關標籤/搜索