Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for mlt.interestingTerms

  • Loading branch information...
commit 03ed72bcceede8be661daa0a320c6857355884ba 1 parent 87a87f9
@nbraem authored
View
4 sunspot/lib/sunspot/dsl/more_like_this_query.rb
@@ -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
View
4 sunspot/lib/sunspot/query/more_like_this.rb
@@ -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,
View
2  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
View
33 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
View
16 sunspot/lib/sunspot/search/more_like_this_search.rb
@@ -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
View
2  sunspot/spec/api/query/more_like_this_spec.rb
@@ -62,6 +62,7 @@
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)
@@ -69,6 +70,7 @@
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
View
10 sunspot/spec/integration/more_like_this_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.