Levenshtein function returning different result based on limit value The Levenshtein function is not working as I would have expect. Is there anything that I minsunderstood?
Here is the query:
SELECT c0.id, c0.engine_type, c0.mpg, c0.kwh, c0.price, c0.make, c0.model, c0.vin, c0.inserted_at, c0.updated_at 
FROM cars AS c0 ORDER BY LEAST(levenshtein(c0.model, 'Camry'), levenshtein(c0.make, 'Toyota')) 
LIMIT 5

Executing this query will return me the following data:
5   &quot;electric&quot;      257 32288   &quot;Toyota&quot;    &quot;Camry&quot; &quot;SW081452D50423138&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
20  &quot;gasoline&quot;  83      68851   &quot;Toyota&quot;    &quot;Camry&quot; &quot;643VN327D4ZH04928&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
4   &quot;gasoline&quot;  74      74482   &quot;Toyota&quot;    &quot;Corolla&quot;   &quot;1K48R780410S27945&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
10  &quot;gasoline&quot;  73      87040   &quot;Dodge&quot; &quot;Ram&quot;   &quot;J22782VG240639409&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
3   &quot;electric&quot;      116 66560   &quot;Audi&quot;  &quot;A5&quot;    &quot;94V5772ZB4BJ23179&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;

As you can see above, the first two matches are the Toyota Camrys; which I would expect from the query.
However, when I change the LIMIT attribute to 10 for instance,
SELECT c0.id, c0.engine_type, c0.mpg, c0.kwh, c0.price, c0.make, c0.model, c0.vin, c0.inserted_at, c0.updated_at 
FROM cars AS c0 ORDER BY LEAST(levenshtein(c0.model, 'Camry'), levenshtein(c0.make, 'Toyota')) 
LIMIT 10

I get a different result:
4   &quot;gasoline&quot;  74      74482   &quot;Toyota&quot;    &quot;Corolla&quot;   &quot;1K48R780410S27945&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
5   &quot;electric&quot;      257 32288   &quot;Toyota&quot;    &quot;Camry&quot; &quot;SW081452D50423138&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
20  &quot;gasoline&quot;  83      68851   &quot;Toyota&quot;    &quot;Camry&quot; &quot;643VN327D4ZH04928&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
10  &quot;gasoline&quot;  73      87040   &quot;Dodge&quot; &quot;Ram&quot;   &quot;J22782VG240639409&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
7   &quot;electric&quot;      274 41661   &quot;Dodge&quot; &quot;Charger&quot;   &quot;FDND794KFW0179068&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
8   &quot;gasoline&quot;  57      42369   &quot;BMW&quot;   &quot;M3&quot;    &quot;NS7V3N1VW5J508253&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
9   &quot;electric&quot;      214 15710   &quot;BMW&quot;   &quot;X5&quot;    &quot;3VUFCG07ATW125829&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
11  &quot;electric&quot;      417 63167   &quot;Nissan&quot;    &quot;Juke&quot;  &quot;6800ULHC7H0857158&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
12  &quot;gasoline&quot;  78      21059   &quot;Lincoln&quot;   &quot;MKX&quot;   &quot;AFUCF3SUG6W287040&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;
13  &quot;electric&quot;      348 93954   &quot;Lincoln&quot;   &quot;MKS&quot;   &quot;2L64A6Z18XR348145&quot; &quot;2020-10-06 14:48:27&quot;   &quot;2020-10-06 14:48:27&quot;

Here above, for a reason beyond my understanding, the Toyota Corolla comes before the two Camrys. How come the Levenshtein distance is smaller for &quot;Toyota Corolla&quot; than &quot;Toyota Camry&quot; when I'm explictely searching for &quot;Toyota Camry&quot; and I limit the rows returned by the query to 10?
Any idea why?
The least() function returns the lesser of it's two arguments; both sets of rows have a perfect match for 'Toyota', which will have a distance of 0. So the ultimate evaluated ORDER BY value for each of the 3 rows is 0.
Adding additional ORDER BY expressions of each of the arguments to least() should do what you want: ORDER BY LEAST( levenshtein(c0.model, 'Camry'), levenshtein(c0.make, 'Toyota')), levenshtein(c0.model, 'Camry'),  levenshtein(c0.model, 'Toyota') will return all Camrys first, then any non-Camry Toyotas, and then rows whose least() expression evaluated to something higher than 0.