Skip to content

Commit 7431804

Browse files
committed
Merge pull request #124 from richardwei2008/p2_chapter17/65_Script_score
chapter17_part13: /170_Relevance/65_Script_score.asciidoc
2 parents 41d7ad6 + 45dcc5d commit 7431804

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed
Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
[[script-score]]
2-
=== Scoring with Scripts
2+
=== 脚本评分
33

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")))
75

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+
举个例子,想将利润空间作为因子加入到相关度评分计算,在业务中,利润空间和以下三点相关:
117

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` 。
1711

18-
The algorithm that we will use to calculate the profit for each home is as
19-
follows:
12+
计算每个度假屋利润的算法如下:
2013

2114
[source,groovy]
2215
-------------------------
@@ -27,11 +20,8 @@ if (price < threshold) {
2720
}
2821
-------------------------
2922

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` ):
3525

3626
[source,groovy]
3727
-------------------------
@@ -43,9 +33,7 @@ if (price < threshold) {
4333
return profit / target
4434
-------------------------
4535

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 脚本表示如下:
4937

5038
[source,groovy]
5139
-------------------------
@@ -57,13 +45,10 @@ if (price < threshold) { <2>
5745
}
5846
return price * (1 - discount) * margin / target <2>
5947
-------------------------
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` 传入的。
6450

65-
Finally, we can add our `script_score` function to the list of other functions
66-
that we are already using:
51+
最终我们将 `script_score` 函数与其他函数一起使用:
6752

6853
[source,json]
6954
-------------------------
@@ -80,7 +65,7 @@ GET /_search
8065
"discount": 0.1,
8166
"target": 10
8267
},
83-
"script": "price = doc['price'].value; margin = doc['margin'].value;
68+
"script": "price = doc['price'].value; margin = doc['margin'].value;
8469
if (price < threshold) { return price * margin / target };
8570
return price * (1 - discount) * margin / target;" <3>
8671
}
@@ -89,35 +74,22 @@ GET /_search
8974
}
9075
}
9176
-------------------------
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` 或 `;` 符号替代。
9880

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+
这个查询根据用户对地点和价格的需求,返回用户最满意的文档,同时也考虑到我们对于盈利的要求。
10282

10383
[TIP]
10484
========================================
10585
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[脚本对文本评分])。
11087
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+
有人说使用脚本对性能会有影响,如果确实发现脚本执行较慢,可以有以下三种选择:
11389
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")))
12194
12295
========================================
123-

0 commit comments

Comments
 (0)