Question

I'm creating an index with Redis using a sorted set. I want the following order for the matching results (smaller string first):

a
ar
art
artu
artur
arturo

From the Redis documentation I using a score to store the data:

The elements are considered to be ordered from the highest to the lowest score.

The code that I write to achieve this has a magic number 1000 but this an arbitrary number, is enough that it's larger than the largest string that I have to store (also I don't know what's the exact maximum string length, but I'm sure that is less than 1000)

Hash[
  'elem'  => string
  'score' => 1000 - string.size
]

How do you delete this magic number with sense?

Était-ce utile?

La solution

Does the score have to be positive? If not:

'score' => -string.size

Essentially, this is replacing 1000 with 0. 0 is one of the numbers that you're allowed to use without it being considered magic!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top