Skip to content

Commit bb6d36a

Browse files
authored
Recommend use of ln1p/log1p to avoid negative scores (#41610)
As negative scores will now cause an error, and it is easy to accidentally produce negative scores with some of the built-in modifiers (especially `ln` and `log`), this adjusts the documentation to more strongly recommend the use of `ln1p` and `log1p` instead. Also corrects some awkward formatting on the note sections following the table.
1 parent e14aeb2 commit bb6d36a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

docs/reference/query-dsl/function-score-query.asciidoc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,14 @@ There are a number of options for the `field_value_factor` function:
305305
| Modifier | Meaning
306306

307307
| `none` | Do not apply any multiplier to the field value
308-
| `log` | Take the https://en.wikipedia.org/wiki/Common_logarithm[common logarithm] of the field value
308+
| `log` | Take the https://en.wikipedia.org/wiki/Common_logarithm[common logarithm] of the field value.
309+
Because this function will return a negative value and cause an error if used on values
310+
between 0 and 1, it is recommended to use `log1p` instead.
309311
| `log1p` | Add 1 to the field value and take the common logarithm
310312
| `log2p` | Add 2 to the field value and take the common logarithm
311-
| `ln` | Take the https://en.wikipedia.org/wiki/Natural_logarithm[natural logarithm] of the field value
313+
| `ln` | Take the https://en.wikipedia.org/wiki/Natural_logarithm[natural logarithm] of the field value.
314+
Because this function will return a negative value and cause an error if used on values
315+
between 0 and 1, it is recommended to use `ln1p` instead.
312316
| `ln1p` | Add 1 to the field value and take the natural logarithm
313317
| `ln2p` | Add 2 to the field value and take the natural logarithm
314318
| `square` | Square the field value (multiply it by itself)
@@ -321,14 +325,17 @@ There are a number of options for the `field_value_factor` function:
321325
Value used if the document doesn't have that field. The modifier
322326
and factor are still applied to it as though it were read from the document.
323327

328+
NOTE: Scores produced by the `field_value_score` function must be
329+
non-negative, otherwise an error will be thrown. The `log` and `ln` modifiers
330+
will produce negative values if used on values between 0 and 1. Be sure to limit
331+
the values of the field with a range filter to avoid this, or use `log1p` and
332+
`ln1p`.
324333

325-
Keep in mind that taking the log() of 0, or the square root of a negative number
326-
is an illegal operation, and an exception will be thrown. Be sure to limit the
327-
values of the field with a range filter to avoid this, or use `log1p` and
328-
`ln1p`.
334+
NOTE: Keep in mind that taking the log() of 0, or the square root of a
335+
negative number is an illegal operation, and an exception will be thrown. Be
336+
sure to limit the values of the field with a range filter to avoid this, or use
337+
`log1p` and `ln1p`.
329338

330-
NOTE: Scores produced by the `field_value_score` function must be non-negative,
331-
otherwise an error will be thrown.
332339

333340
[[function-decay]]
334341
==== Decay functions

0 commit comments

Comments
 (0)