Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for mlt.interestingTerms
  • Loading branch information
nbraem committed Oct 7, 2010
1 parent 87a87f9 commit 03ed72b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions sunspot/lib/sunspot/dsl/more_like_this_query.rb
Expand Up @@ -51,6 +51,10 @@ def boost_by_relevance(should_boost)
@query.more_like_this.boost_by_relevance = should_boost
end
alias_method :boost, :boost_by_relevance

def interesting_terms(value)
@query.more_like_this.interesting_terms = value
end
end
end
end
4 changes: 4 additions & 0 deletions sunspot/lib/sunspot/query/more_like_this.rb
Expand Up @@ -42,6 +42,10 @@ def boost_by_relevance=(should_boost)
@params[:"mlt.boost"] = should_boost
end

def interesting_terms=(value)
@params[:"mlt.interestingTerms"] = value
end

def to_params
params = Sunspot::Util.deep_merge(
@params,
Expand Down
2 changes: 1 addition & 1 deletion sunspot/lib/sunspot/search.rb
@@ -1,5 +1,5 @@
%w(abstract_search standard_search more_like_this_search query_facet field_facet
date_facet facet_row hit highlight).each do |file|
date_facet facet_row hit highlight interesting_term).each do |file|
require File.join(File.dirname(__FILE__), 'search', file)
end

Expand Down
33 changes: 33 additions & 0 deletions sunspot/lib/sunspot/search/interesting_term.rb
@@ -0,0 +1,33 @@
module Sunspot
module Search
#
# InterestingTerm encapsulates the information returned by solr
# when specifying mlt.interestingTerms = list or details
#
class InterestingTerm
#
# Actual term that is extracted by the MoreLikeThisHandler in solr.
# Results returned by more_like_this are found by searching for this term.
#
attr_reader :term

#
# Field of this interesing term, or nil if interesting_terms is
# not :details
#
attr_reader :field

#
# Score of this interesting term, or nil if interesting_terms is
# not :details
#
attr_reader :score

def initialize(term, field = nil, score = nil) #:nodoc:
@term = term
@field = field
@score = score
end
end
end
end
16 changes: 16 additions & 0 deletions sunspot/lib/sunspot/search/more_like_this_search.rb
Expand Up @@ -19,6 +19,22 @@ def execute
def request_handler
super || :mlt
end

def interesting_terms
if @solr_result['interestingTerms']
if @solr_result['interestingTerms'].last.is_a? Float
# interestingTerms: ["body_mlt_textv:two", 1.0, "body_mlt_textv:three", 1.0]
@interesting_terms ||= @solr_result['interestingTerms'].each_slice(2).map do |interesting_term, score|
field, term = interesting_term.match(/(.*)_.+:(.*)/)[1..2]
InterestingTerm.new(term, field, score)
end
else
@interesting_terms ||= @solr_result['interestingTerms'].map do |term|
InterestingTerm.new(term)
end
end
end
end

private

Expand Down
2 changes: 2 additions & 0 deletions sunspot/spec/api/query/more_like_this_spec.rb
Expand Up @@ -62,13 +62,15 @@
maximum_word_length 4
maximum_query_terms 5
boost_by_relevance false
interesting_terms :details
end
connection.should have_last_search_with(:"mlt.mintf" => 1)
connection.should have_last_search_with(:"mlt.mindf" => 2)
connection.should have_last_search_with(:"mlt.minwl" => 3)
connection.should have_last_search_with(:"mlt.maxwl" => 4)
connection.should have_last_search_with(:"mlt.maxqt" => 5)
connection.should have_last_search_with(:"mlt.boost" => false)
connection.should have_last_search_with(:"mlt.interestingTerms" => :details)
end

it 'should accept short options' do
Expand Down
10 changes: 10 additions & 0 deletions sunspot/spec/integration/more_like_this_spec.rb
Expand Up @@ -40,4 +40,14 @@
@mlt.total.should == 0
end
end

it 'should return interesting terms' do
Sunspot.more_like_this(@posts.first) do
interesting_terms :list
end.interesting_terms.length.should == 4

Sunspot.more_like_this(@posts.first) do
interesting_terms :details
end.interesting_terms.length.should == 4
end
end

0 comments on commit 03ed72b

Please sign in to comment.