refactors proposals search
This commit is contained in:
@@ -13,15 +13,18 @@ module SearchCache
|
|||||||
private
|
private
|
||||||
|
|
||||||
def searchable_values_sql
|
def searchable_values_sql
|
||||||
searchable_values.collect { |value, weight| set_tsvector(value, weight) }.join(" || ")
|
searchable_values
|
||||||
|
.select{ |k,_| k.present? }
|
||||||
|
.collect{ |value, weight| set_tsvector(value, weight) }
|
||||||
|
.join(" || ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_tsvector(value, weight)
|
def set_tsvector(value, weight)
|
||||||
"setweight(to_tsvector('spanish', coalesce(#{quote(value)}, '')), #{quote(weight)})"
|
"setweight(to_tsvector('spanish', unaccent(coalesce(#{quote(value)}, ''))), #{quote(weight)})"
|
||||||
end
|
end
|
||||||
|
|
||||||
def quote(value)
|
def quote(value)
|
||||||
ActiveRecord::Base.connection.quote(value)
|
ActiveRecord::Base.connection.quote(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -45,17 +45,9 @@ class Proposal < ActiveRecord::Base
|
|||||||
scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
|
scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
|
||||||
|
|
||||||
pg_search_scope :pg_search, {
|
pg_search_scope :pg_search, {
|
||||||
against: {
|
against: :ignored, # not used since the using: option has a tsvector_column
|
||||||
title: 'A',
|
|
||||||
question: 'B',
|
|
||||||
summary: 'C',
|
|
||||||
description: 'D'
|
|
||||||
},
|
|
||||||
associated_against: {
|
|
||||||
tags: :name
|
|
||||||
},
|
|
||||||
using: {
|
using: {
|
||||||
tsearch: { dictionary: "spanish", tsvector_column: 'tsv' }
|
tsearch: { dictionary: "spanish", tsvector_column: 'tsv', prefix: true }
|
||||||
},
|
},
|
||||||
ignoring: :accents,
|
ignoring: :accents,
|
||||||
ranked_by: '(:tsearch)',
|
ranked_by: '(:tsearch)',
|
||||||
@@ -63,22 +55,24 @@ class Proposal < ActiveRecord::Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
def searchable_values
|
def searchable_values
|
||||||
values = {
|
{ title => 'A',
|
||||||
title => 'A',
|
question => 'B',
|
||||||
question => 'B',
|
author.username => 'B',
|
||||||
summary => 'C',
|
tag_list.join(' ') => 'B',
|
||||||
description => 'D'
|
geozone.try(:name) => 'B',
|
||||||
|
summary => 'C',
|
||||||
|
description_text => 'D'
|
||||||
}
|
}
|
||||||
tag_list.each{ |tag| values[tag] = 'D' }
|
|
||||||
values[author.username] = 'D'
|
|
||||||
values[geozone.name] = 'D' if geozone.present?
|
|
||||||
values
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search(terms)
|
def self.search(terms)
|
||||||
self.pg_search(terms)
|
self.pg_search(terms)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def description_text
|
||||||
|
ActionController::Base.helpers.sanitize(description, tags: [])
|
||||||
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
super.try :html_safe
|
super.try :html_safe
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -414,22 +414,21 @@ describe Proposal do
|
|||||||
context "stemming" do
|
context "stemming" do
|
||||||
|
|
||||||
it "searches word stems" do
|
it "searches word stems" do
|
||||||
proposal = create(:proposal, summary: 'biblioteca')
|
proposal = create(:proposal, summary: 'Economía')
|
||||||
|
|
||||||
results = Proposal.search('bibliotecas')
|
results = Proposal.search('economía')
|
||||||
expect(results).to eq([proposal])
|
expect(results).to eq([proposal])
|
||||||
|
|
||||||
results = Proposal.search('bibliotec')
|
results = Proposal.search('econo')
|
||||||
expect(results).to eq([proposal])
|
expect(results).to eq([proposal])
|
||||||
|
|
||||||
results = Proposal.search('biblioteco')
|
results = Proposal.search('eco')
|
||||||
expect(results).to eq([proposal])
|
expect(results).to eq([proposal])
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "accents" do
|
context "accents" do
|
||||||
|
|
||||||
it "searches with accents" do
|
it "searches with accents" do
|
||||||
proposal = create(:proposal, summary: 'difusión')
|
proposal = create(:proposal, summary: 'difusión')
|
||||||
|
|
||||||
@@ -439,12 +438,14 @@ describe Proposal do
|
|||||||
proposal2 = create(:proposal, summary: 'estadisticas')
|
proposal2 = create(:proposal, summary: 'estadisticas')
|
||||||
results = Proposal.search('estadísticas')
|
results = Proposal.search('estadísticas')
|
||||||
expect(results).to eq([proposal2])
|
expect(results).to eq([proposal2])
|
||||||
end
|
|
||||||
|
|
||||||
|
proposal3 = create(:proposal, summary: 'público')
|
||||||
|
results = Proposal.search('publico')
|
||||||
|
expect(results).to eq([proposal3])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "case" do
|
context "case" do
|
||||||
|
|
||||||
it "searches case insensite" do
|
it "searches case insensite" do
|
||||||
proposal = create(:proposal, title: 'SHOUT')
|
proposal = create(:proposal, title: 'SHOUT')
|
||||||
|
|
||||||
@@ -455,24 +456,24 @@ describe Proposal do
|
|||||||
results = Proposal.search("SCREAM")
|
results = Proposal.search("SCREAM")
|
||||||
expect(results).to eq([proposal2])
|
expect(results).to eq([proposal2])
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "tags" do
|
context "tags" do
|
||||||
|
|
||||||
it "searches by tags" do
|
it "searches by tags" do
|
||||||
proposal = create(:proposal, tag_list: 'Latina')
|
proposal = create(:proposal, tag_list: 'Latina')
|
||||||
|
|
||||||
results = Proposal.search('Latina')
|
results = Proposal.search('Latina')
|
||||||
expect(results.first).to eq(proposal)
|
expect(results.first).to eq(proposal)
|
||||||
end
|
|
||||||
|
|
||||||
|
results = Proposal.search('Latin')
|
||||||
|
expect(results.first).to eq(proposal)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
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')
|
||||||
|
|||||||
Reference in New Issue
Block a user