1
1
[[script-score]]
2
- === Scoring with Scripts
2
+ === 脚本评分
3
3
4
- Finally, if none of the `function_score`'s built-in functions suffice, you can
5
- implement the logic that you need with a script, using the `script_score`
6
- function.((("function_score query", "using script_score function")))((("script_score function")))((("relevance", "controlling", "scoring with scripts")))
4
+ 最后,如果所有 `function_score` 内置的函数都无法满足应用场景,可以使用 `script_score` 函数自行实现逻辑。((("function_score query", "using script_score function")))((("script_score function")))((("relevance", "controlling", "scoring with scripts")))
7
5
8
- For an example, let's say that we want to factor our profit margin into the
9
- relevance calculation. In our business, the profit margin depends on three
10
- factors:
6
+ 举个例子,想将利润空间作为因子加入到相关度评分计算,在业务中,利润空间和以下三点相关:
11
7
12
- * The `price` per night of the vacation home.
13
- * The user's membership level--some levels get a percentage `discount`
14
- above a certain price per night `threshold`.
15
- * The negotiated `margin` as a percentage of the price-per-night, after user
16
- discounts.
8
+ * `price` 度假屋每晚的价格。
9
+ * 会员用户的级别——某些等级的用户可以在每晚房价高于某个 `threshold` 阀值价格的时候享受折扣 `discount` 。
10
+ * 用户享受折扣后,经过议价的每晚房价的利润 `margin` 。
17
11
18
- The algorithm that we will use to calculate the profit for each home is as
19
- follows:
12
+ 计算每个度假屋利润的算法如下:
20
13
21
14
[source,groovy]
22
15
-------------------------
@@ -27,11 +20,8 @@ if (price < threshold) {
27
20
}
28
21
-------------------------
29
22
30
- We probably don't want to use the absolute profit as a score; it would
31
- overwhelm the other factors like location, popularity and features. Instead,
32
- we can express the profit as a percentage of our `target` profit. A profit
33
- margin above our target will have a positive score (greater than `1.0`), and a profit margin below our target will have a negative score (less than
34
- `1.0`):
23
+ 我们很可能不想用绝对利润作为评分,这会弱化其他如地点、受欢迎度和特性等因子的作用,而是将利润用目标利润 `target` 的百分比来表示,高于
24
+ 目标的利润空间会有一个正向评分(大于 `1.0` ),低于目标的利润空间会有一个负向分数(小于 `1.0` ):
35
25
36
26
[source,groovy]
37
27
-------------------------
@@ -43,9 +33,7 @@ if (price < threshold) {
43
33
return profit / target
44
34
-------------------------
45
35
46
- The default scripting language in Elasticsearch is
47
- http://groovy.codehaus.org/[Groovy], which for the most part looks a lot like
48
- JavaScript.((("Groovy", "script factoring profit margins into relevance calculations"))) The preceding algorithm as a Groovy script would look like this:
36
+ Elasticsearch 里使用 http://groovy.codehaus.org/[Groovy] 作为默认的脚本语言,它与JavaScript很像,((("Groovy", "script factoring profit margins into relevance calculations")))上面这个算法用 Groovy 脚本表示如下:
49
37
50
38
[source,groovy]
51
39
-------------------------
@@ -57,13 +45,10 @@ if (price < threshold) { <2>
57
45
}
58
46
return price * (1 - discount) * margin / target <2>
59
47
-------------------------
60
- <1> The `price` and `margin` variables are extracted from the `price` and
61
- `margin` fields in the document.
62
- <2> The `threshold`, `discount`, and `target` variables we will pass in as
63
- `params`.
48
+ <1> `price` 和 `margin` 变量可以分别从文档的 `price` 和 `margin` 字段提取。
49
+ <2> `threshold` 、 `discount` 和 `target` 是作为参数 `params` 传入的。
64
50
65
- Finally, we can add our `script_score` function to the list of other functions
66
- that we are already using:
51
+ 最终我们将 `script_score` 函数与其他函数一起使用:
67
52
68
53
[source,json]
69
54
-------------------------
@@ -80,7 +65,7 @@ GET /_search
80
65
"discount": 0.1,
81
66
"target": 10
82
67
},
83
- "script": "price = doc['price'].value; margin = doc['margin'].value;
68
+ "script": "price = doc['price'].value; margin = doc['margin'].value;
84
69
if (price < threshold) { return price * margin / target };
85
70
return price * (1 - discount) * margin / target;" <3>
86
71
}
@@ -89,35 +74,22 @@ GET /_search
89
74
}
90
75
}
91
76
-------------------------
92
- <1> The `location` and `price` clauses refer to the example explained in
93
- <<decay-functions>>.
94
- <2> By passing in these variables as `params`, we can change their values
95
- every time we run this query without having to recompile the script.
96
- <3> JSON cannot include embedded newline characters. Newline characters in
97
- the script should either be escaped as `\n` or replaced with semicolons.
77
+ <1> `location` 和 `price` 语句在 <<decay-functions,衰减函数>> 中解释过。
78
+ <2> 将这些变量作为参数 `params` 传递,我们可以查询时动态改变脚本无须重新编译。
79
+ <3> JSON 不能接受内嵌的换行符,脚本中的换行符可以用 `\n` 或 `;` 符号替代。
98
80
99
- This query would return the documents that best satisfy the user's
100
- requirements for location and price, while still factoring in our need to make
101
- a profit.
81
+ 这个查询根据用户对地点和价格的需求,返回用户最满意的文档,同时也考虑到我们对于盈利的要求。
102
82
103
83
[TIP]
104
84
========================================
105
85
106
- The `script_score` function provides enormous flexibility.((("scripts", "performance and"))) Within a script,
107
- you have access to the fields of the document, to the current `_score`, and
108
- even to the term frequencies, inverse document frequencies, and field length
109
- norms (see {ref}/modules-advanced-scripting.html[Text scoring in scripts]).
86
+ `script_score` 函数提供了巨大的灵活性,((("scripts", "performance and")))可以通过脚本访问文档里的所有字段、当前评分 `_score` 甚至词频、逆向文档频率和字段长度规范值这样的信息(参见 see {ref}/modules-advanced-scripting.html[脚本对文本评分])。
110
87
111
- That said, scripts can have a performance impact. If you do find that your
112
- scripts are not quite fast enough, you have three options:
88
+ 有人说使用脚本对性能会有影响,如果确实发现脚本执行较慢,可以有以下三种选择:
113
89
114
- * Try to precalculate as much information as possible and include it in each
115
- document.
116
- * Groovy is fast, but not quite as fast as Java.((("Java", "scripting in"))) You could reimplement your
117
- script as a native Java script. (See
118
- {ref}/modules-scripting.html#native-java-scripts[Native Java Scripts]).
119
- * Use the `rescore` functionality((("rescoring"))) described in <<rescore-api>> to apply
120
- your script to only the best-scoring documents.
90
+ * 尽可能多的提前计算各种信息并将结果存入每个文档中。
91
+ * Groovy 很快,但没 Java 快。((("Java", "scripting in")))可以将脚本用原生的 Java 脚本重新实现。(参见
92
+ {ref}/modules-scripting.html#native-java-scripts[原生 Java 脚本])。
93
+ * 仅对那些最佳评分的文档应用脚本,使用 <<rescore-api,重新评分>> 中提到的 `rescore` 功能。((("rescoring")))
121
94
122
95
========================================
123
-
0 commit comments