In views search index is imposssible to filter results on decimal values.

Created on 2 December 2023, 7 months ago
Updated 23 December 2023, 6 months ago

Hello, Drupal profs!

I have decimal field in search index

All nodes with these values ​​are indexed.

Some times it's possible to filter results on some values:

But most often (approximately always) it's doesn't work. Why?

đź“Ś Task
Status

Active

Version

1.31

Component

General code

Created by

🇺🇦Ukraine VasyOK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @VasyOK
  • 🇦🇹Austria drunken monkey Vienna, Austria

    That’s a general problem with the way computers represent non-integer numbers. See Wikipedia for details. In essence, numbers like 5.29 cannot be stored exactly in a computer, so they have to be rounded. Consequently, it depends on very low-level technical details of not only the software (search backend in our case), but also the specific hardware whether a comparison of floating point numbers returns “equal”. The general rule is therefore to never use an “equals” comparison with floating point numbers, because they are too unpredictable.

    If you do need exact matches, just indexing the field as a “string” instead should work a lot better (though it’s also not ideal, since, e.g., “5.5“, “5.50” and “05.5” would all be considered different).

    The real solution here could be to add a new operator to Views, “equals (rounded)” (or something like that), that converts the equality check to a very small range condition: e.g., price = 5.29 would become price BETWEEN 5.289999 AND 5.290001. (Just adapting the existing “equals” operator might also make sense, since it shouldn’t be used in its current form anyways. Would have to be discussed.)
    If you are interested, we can change this issue to a feature request. Would need someone to implement this, though, plus add tests. And I’d probably wait whether more people express support for this, to see whether this is even a common enough problem that it needs to be addressed. In my experience, for things like prices (probably the most common decimal field) you rarely want an equality check anyways, range checks make much more sense in most cases.

Production build 0.69.0 2024