improves search order

This commit is contained in:
rgarcia
2015-11-18 20:43:01 +01:00
parent 51e9de115c
commit 1429728368
2 changed files with 13 additions and 14 deletions

View File

@@ -54,8 +54,8 @@ class Proposal < ActiveRecord::Base
tsearch: { dictionary: "spanish", tsvector_column: 'tsv' } tsearch: { dictionary: "spanish", tsvector_column: 'tsv' }
}, },
ignoring: :accents, ignoring: :accents,
ranked_by: '(:tsearch + proposals.cached_votes_up)', ranked_by: '(:tsearch)',
order_within_rank: "proposals.created_at DESC" order_within_rank: "proposals.cached_votes_up DESC"
} }
def searchable_values def searchable_values

View File

@@ -435,7 +435,7 @@ describe Proposal do
context "order" do context "order" do
it "orders by weight" do it "orders by weight" do
proposal_question = create(:proposal, question: 'stop corruption') proposal_question = create(:proposal, question: 'stop corruption')
proposal_title = create(:proposal, title: 'stop corruption') proposal_title = create(:proposal, title: 'stop corruption')
proposal_description = create(:proposal, description: 'stop corruption') proposal_description = create(:proposal, description: 'stop corruption')
proposal_summary = create(:proposal, summary: 'stop corruption') proposal_summary = create(:proposal, summary: 'stop corruption')
@@ -448,30 +448,29 @@ describe Proposal do
expect(results.fourth).to eq(proposal_description) expect(results.fourth).to eq(proposal_description)
end end
it "orders by weight and then votes" do it "orders by weight and then by votes" do
title_some_votes = create(:proposal, title: 'stop corruption', cached_votes_up: 5) title_some_votes = create(:proposal, title: 'stop corruption', cached_votes_up: 5)
title_least_voted = create(:proposal, title: 'stop corruption', cached_votes_up: 2) title_least_voted = create(:proposal, title: 'stop corruption', cached_votes_up: 2)
title_most_voted = create(:proposal, title: 'stop corruption', cached_votes_up: 10) title_most_voted = create(:proposal, title: 'stop corruption', cached_votes_up: 10)
summary_most_voted = create(:proposal, summary: 'stop corruption', cached_votes_up: 10) summary_most_voted = create(:proposal, summary: 'stop corruption', cached_votes_up: 10)
results = Proposal.search('stop corruption') results = Proposal.search('stop corruption')
expect(results.first).to eq(title_most_voted) expect(results.first).to eq(title_most_voted)
expect(results.second).to eq(summary_most_voted) expect(results.second).to eq(title_some_votes)
expect(results.third).to eq(title_some_votes) expect(results.third).to eq(title_least_voted)
expect(results.fourth).to eq(title_least_voted) expect(results.fourth).to eq(summary_most_voted)
end end
it "orders by weight and then votes and then created_at" do it "gives much more weight to word matches than votes" do
newest = create(:proposal, title: 'stop corruption', cached_votes_up: 5, created_at: Time.now) exact_title_few_votes = create(:proposal, title: 'stop corruption', cached_votes_up: 5)
oldest = create(:proposal, title: 'stop corruption', cached_votes_up: 5, created_at: 1.month.ago) similar_title_many_votes = create(:proposal, title: 'stop some of the corruption', cached_votes_up: 500)
old = create(:proposal, title: 'stop corruption', cached_votes_up: 5, created_at: 1.week.ago)
results = Proposal.search('stop corruption') results = Proposal.search('stop corruption')
expect(results.first).to eq(newest) expect(results.first).to eq(exact_title_few_votes)
expect(results.second).to eq(old) expect(results.second).to eq(similar_title_many_votes)
expect(results.third).to eq(oldest)
end end
end end