I have the following task: Query SOLR and return a weighted list based on multiple conditions.
Example:
I have documents with the following fields, they mostly represent movies:
name, genre, actors, director
I want to return 20 documents sorted on the following condition
- The document shares 1 actor and is from the same director (5 points)
- The document shares 2 or more actors (3 points)
- The document shares the director (3 points)
- The document is of the same genre and shares an actor (2 points)
- The document is of the same genre (1 point)
Then take these 4 movies:
Id: 1
Name: Harry Potter and the Philosopher’s Stone
Genre: Adventure
Director: Chris Columbus
Actors: Daniel Radcliffe, Rupert Grint, Emma Watson
Id: 2
Name: My Week with Marilyn
Genre: Drama
Director: Simon Curtis
Actors: Michelle Williams, Eddie Redmayne, Emma Watson
Id: 3
Name: Percy Jackson & the Olympians: The Lightning Thief
Genre: Adventure
Directory: Chris Columbus
Actors: Logan Lerman, Brandon T. Jackson, Alexandra Daddario
Id: 4
Name: Harry Potter and the Chamber of Secrets
Genre: Adventure
Director: Chris Columbus
Actors: Daniel Radcliffe, Rupert Grint, Emma Watson
I want to query the SOLR as such: Return me a list of relevant movies based on movie id==4
The returned result should be:
- Id: 1, points: 14 (matches all 5 conditions)
- Id: 3, points: 4 (matches condition 3 and 5)
- Id: 2, points: 0 (matches 0 conditions)
Is there anyway to do this directly within SOLR?
As always thanks in advance 🙂
You can return weighted results with the DisMax Query Parser, it’s called boosting. You can give varying weights to the columns in your document by using a Query Filter like in the following example. You’ll have to modify it to come up with your own formula, but you should be able to get close. Start with tweaking the numbers in the boost, but you might end up doing some more advanced Function Queries
From your example where you want to find documents that match #4
?q=Genre:’Adventure’ Director:’Chris Columnbus’ Actors:(‘Daniel Radcliffe’ ‘Rupert Grint’ ‘Emma Watson’)&qf=Director^2.0+Actor^1.5+Genre^1.0&fl=*,score