I'm interested in possible ways to model the cosine similarity algorithm using Solr. I have items which are assigned a vector, for example:
items = [
{ id: 1, vector: [0,0,0,2,3,0,0] },
{ id: 2, vector: [0,1,0,1,5,0,0] },
{ id: 3, vector: [2,3,0,0,0,1,0] },
{ id: 4, vector: [1,2,4,6,5,0,0] }
]
And a search vector to which the others need to be ranked.
Currently, I'm modeling this in ruby by running over all the items and assigning them a rank against the input vector. Here's the implementation of cosine similarity I'm using:
module SimilarityCalculator
def self.get_similarity(vector1, vector2)
dp = dot_product(vector1, vector2)
nm = normalize(vector1) * normalize(vector2)
dp / nm
end
private
def self.dot_product(vector1, vector2)
sum = 0.0
vector1.each_with_index { |val, i| sum += val * vector2[i] }
sum
end
def self.normalize(vector)
Math.sqrt(vector.inject(0.0) { |m,o| m += o**2 })
end
end
Then, to get a ranked list I would do something like the following:
ranked = []
search_vector = [1,0,0,3,5,0,0]
items.each do |item|
rank = SimilarityCalculator.get_similarity(search_vector, item.vector)
{ id: item.id, rank: rank }
end
I don't know enough about Solr to know how this would be modeled or even if it can, but I thought I'd throw it out there.