Reputation: 1038
I've added a "popularity" field to Solr as an external field.
"./solr/configsets/sunspot/conf/schema.xml"
<schema name="sunspot" version="1.0">
<!-- ... -->
<types>
<fieldType name="ext_popularity_field" keyField="id" defVal="1.0" class="solr.ExternalFileField"/>
</types>
<fields>
<field name="popularity" type="ext_popularity_field" />
</fields>
<!-- ... -->
<listener event="newSearcher" class="org.apache.solr.schema.ExternalFileFieldReloader"/>
<listener event="firstSearcher" class="org.apache.solr.schema.ExternalFileFieldReloader"/>
</schema>
I've generated the ./solr/development/data/external_popularity.txt file and I can see those scores with this query on the solr dashboard:
http://localhost:8983/solr/development/select?q={!func}popularity&fl=id,score
<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">21</int>
</lst>
<result name="response" numFound="7626" start="0" maxScore="21.75">
<doc>
<str name="id">Item 9788770781701</str>
<float name="score">21.75</float>
</doc>
<doc>
<str name="id">Item 9781449661373</str>
<float name="score">19.0</float>
</doc>
<!-- ... -->
</result>
</response>
So now the question is, how do I actually reference this in the Rails project? I have an Item class with a searchable block:
class Item < ActiveRecord::Base
searchable include: [:authors, :publisher, :order_temporary_stock] do
text :ean
text :full_title
text :author_names
text :publisher_name
# ...
end
end
...and a typical search:
Item.solr_search do
fulltext self.q do
phrase_fields full_title: 16.0 # pf: full_title_text^16.0
phrase_slop 1
boost_fields ean: 8.0 # qf: ean_text^8.0
boost_fields author_names: 2.0 # qf: author_names_text^2.0
boost_fields publisher_name: 2.0 # qf: publisher_name_text^2.0
# boost(popularity)
end
end
Upvotes: 1
Views: 453
Reputation: 1038
Solved the above by adding :boost to the search params as follows:
Item.solr_search do
fulltext self.q do
phrase_fields full_title: 16.0 # pf: full_title_text^16.0
phrase_slop 1
boost_fields ean: 8.0 # qf: ean_text^8.0
boost_fields author_names: 2.0 # qf: author_names_text^2.0
boost_fields publisher_name: 2.0 # qf: publisher_name_text^2.0
end
adjust_solr_params do |params|
params[:boost] = "popularity_score"
end
end
end
Upvotes: 1