Permalink
Browse files

Add Matthias's find_related_articles method to Page.

  • Loading branch information...
1 parent 20d1da1 commit c442d4c0a6ff568aa727421d4897a9e2e56db167 @gma committed Nov 12, 2010
Showing with 22 additions and 2 deletions.
  1. +22 −2 lib/nesta-search.rb
View
@@ -1,5 +1,25 @@
module Nesta
- module Search
- # Your code goes here...
+ class Page
+ def related_articles
+ if keywords
+ index = Ferret::Index::Index.new
+ Page.find_articles.each do |article|
+ index << {:heading => article.heading, :href => article.abspath, :summary => article.summary, :body => article.body}
+ end
+ results = {}
+ keywords.each do |keyword|
+ index.search_each(keyword) do |id, score|
+ results[id] ? results[id] = results[id] + score : results[id] = score
+ end
+ end
+ related_article_links = []
+ results.each do |key, value|
+ related_article_links << Page.find_by_path(index[key]['href']) unless abspath == index[key]['href']
+ end
+ related_article_links
+ else
+ []
+ end
+ end
end
end

0 comments on commit c442d4c

Please sign in to comment.