user preferenced sorting


for some new features on our search we are wondering if it is possible to have the sorting in Redisearch based on predefined user preferences.

Lets say if user is fan of classical music and he has this in his preferences, can we give priority on the Redis documents that are tagged as classical (or contain classical in the value). Can this be done through sorting or scoring?

We must be able to handle it dynamically for each user at query time.

Does an ~optional query on a tag index not do what you want? Something like

FT.SEARCH idx ‘@name:mark ~@likes:{classical}’

Mark Nunberg | Senior Software Engineer
Redis Labs - home of Redis


Great, Mark! I didn’t even know there was a optional query.

Optional terms or clauses: foo ~bar means bar is optional but documents with bar in them will rank higher

Can you explain a bit “will rank higher”? How does this happen in the background? Will the results with the optional query be guaranteed at the top?

Results are sorted (by default) using a numeric score. The score is comprised of the “uniqueness” of the term, coupled by the WEIGHT of the field, and a bunch of other factors. In a normal query “foo bar”, both foo and bar must be present in the document. When they are found, the score of foo is combined with the score of bar to give us a solid result. In an OR query such as “foo|bar”, there is no way to express the priority of terms (for example, “foo|bar” would match documents that contained either, meaning that foo might not be present). "foo ~bar” allows the query to match all documents containing “foo”, and place priority on those that also contain “bar”, adding the weight of “bar”, if present, to the score.

Mark Nunberg | Senior Software Engineer
Redis Labs - home of Redis


We are experimenting with this, but are not getting the results as desired.
The idea is that users can have multiple preference types and even multiple preferences within the same type. Each preference can also have it’s own weight.

The query that we have now seems something like this:
FT.SEARCH idxCars “@BodyType:break ~@CleanMake:Volkswagen => { $weight: 10; } ~@CleanMake:Peugeot => { $weight: 5; } ~@Color:Black => { $weight: 50; }”

This query should return all “break” models, with the VW’s listed before the Peugeot’s.

However, when supplying two optional parameters for the same field, the sorting is not how we would expect.

Also the sorting seems to act different when the optional parameters are put in front or in back of the real query.

Are we using the optional parameters and weight boosting correct?

There were some more recent (a few weeks ago) fixes for optional queries which might fix the issues you’re seeing, or at least similar issues. In master; specifically 37bbea3fdd06d4ce702ccf1864d2f5d29be01dbd

Mark Nunberg | Senior Software Engineer
Redis Labs - home of Redis


I can confirm that the issue still remains using the latest version of the master (Git=v1.4.2-268-g83e6790c)

I’ve created a bug for it and will update there: