ElasticsearchのFunctionScoreQueryにはfieldの値をscoreに関連づけられるfield_value_factor functionがあります。
field_value_factorにはデフォルト(none) + 9種類のmodifierが指定でき、field_valueに特殊な計算をした値をscoreとすることができます。
modifier | score計算 |
---|---|
none | field_value |
log | log(field_value) |
log1p | log(1 + field_value) |
log2p | log(2 + field_value) |
ln | ln(field_value) |
ln1p | ln(1 + field_value) |
ln2p | ln(2 + field_value) |
square | (field_value)^2 |
sqrt | √(field_value) |
reciprocal | 1 / field_value |
いずれも簡単な計算ですが頭の中で考えるより目で見たほうがわかりやすいのでグラフにしました。
グラフには square を含めていません。指数関数なので他に比べ増加率が高すぎグラフが見づらくなるためです。
field値 1 〜 10の範囲の場合
field_value 1 〜 1000の範囲の場合
noneは除いています。log3種とln3種はそれぞれ一本にまとまって見えています。
log1pとln1pの使いどころ
公式ドキュメントに記述があり、field_value = 0に対して log, ln を指定すると例外を発生させるのでそれを回避するために使用するとのことです。
Keep in mind that taking the log() of 0, or the square root of a negative number
is an illegal operation, and an exception will be thrown. Be sure to limit the
values of the field with a range filter to avoid this, or uselog1p
and
ln1p
.
log2p, ln2pについては言及がありませんが -1か自然数の値をもつfieldに使用するんでしょうか。