search visualizations by tag

pull/14798/head
Gonzalo Riestra 6 years ago
parent d0fe9b7268
commit 2fa5969563

@ -25,7 +25,8 @@ class Carto::VisualizationQuerySearcher
def tsvector
%{
setweight(to_tsvector('english', coalesce("visualizations"."name",'')), 'A') ||
setweight(to_tsvector('english', coalesce("visualizations"."description",'')), 'B')
setweight(to_tsvector('english', coalesce(array_to_string(visualizations.tags, ', '),'')), 'B') ||
setweight(to_tsvector('english', coalesce("visualizations"."description",'')), 'C')
}
end
@ -41,7 +42,8 @@ class Carto::VisualizationQuerySearcher
def partial_match_sql
%{
#{tsvector} @@ plainto_tsquery('english', ?)
OR CONCAT("visualizations"."name", ' ', "visualizations"."description") ILIKE ?
OR CONCAT("visualizations"."name", array_to_string(visualizations.tags, ''), "visualizations"."description")
ILIKE ?
}.squish
end

@ -38,6 +38,15 @@ describe Carto::DashboardPreviewSearcher do
result.total_count.should eql 1
end
it 'finds maps by tag' do
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, types: ["derived"], pattern: "Spain", limit: 5)
result = searcher.search
result.tags.should eql []
result.visualizations.should eql [@map_spain]
result.total_count.should eql 1
end
it 'finds datasets by name' do
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, types: ["table"], pattern: "lution", limit: 5)
result = searcher.search
@ -56,6 +65,15 @@ describe Carto::DashboardPreviewSearcher do
result.total_count.should eql 1
end
it 'finds datasets by tag' do
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, types: ["table"], pattern: "States", limit: 5)
result = searcher.search
result.tags.should eql []
result.visualizations.should eql [@table_us]
result.total_count.should eql 1
end
it 'finds tags' do
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, types: ["tag"], pattern: "united", limit: 5)
result = searcher.search
@ -66,12 +84,21 @@ describe Carto::DashboardPreviewSearcher do
end
it 'includes results from tags, maps and datasets' do
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, pattern: "on", limit: 5)
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, pattern: "contamin", limit: 5)
result = searcher.search
result.tags.should eql ["contamination"]
result.visualizations.should =~ [@table_us]
result.total_count.should eql 2
result.visualizations.should =~ [@table_us, @map_spain]
result.total_count.should eql 3
end
it 'limits results by type' do
searcher = Carto::DashboardPreviewSearcher.new(user: @carto_user1, pattern: "contamin", limit: 1)
result = searcher.search
result.tags.should eql ["contamination"]
result.visualizations.should =~ [@map_spain]
result.total_count.should eql 3
end
end

@ -8,7 +8,8 @@ describe Carto::VisualizationQuerySearcher do
FactoryGirl.create(:derived_visualization, user_id: @user.id, name: 'New York polution',
description: 'Polution by traffic and industry')
Delorean.jump(1.day)
FactoryGirl.create(:derived_visualization, user_id: @user.id, name: 'New industries in York')
FactoryGirl.create(:derived_visualization, user_id: @user.id, name: 'New industries in York',
tags: ["traffic"])
Delorean.jump(1.day)
FactoryGirl.create(:derived_visualization, user_id: @user.id, name: 'Madrid traffic and polution')
Delorean.back_to_the_present
@ -29,10 +30,10 @@ describe Carto::VisualizationQuerySearcher do
expect(result.size).to eql 0
end
it 'finds words in title and description' do
it 'finds words in title, description and tag' do
result = @searcher.search('traffic')
expect(result.size).to eql 2
expect(result.size).to eql 3
end
it 'finds singular and plural words with a singular one' do
@ -50,7 +51,7 @@ describe Carto::VisualizationQuerySearcher do
it 'allows to search with several words not consecutive' do
result = @searcher.search('New York traffic')
expect(result.size).to eql 1
expect(result.size).to eql 2
end
end
@ -69,12 +70,13 @@ describe Carto::VisualizationQuerySearcher do
end
context 'ordering' do
it 'ranks better matches in title than description' do
it 'ranks matches by type: title > tag > description' do
result = @searcher.search('traffic')
expect(result.size).to eql 2
expect(result.size).to eql 3
expect(result.first.name).to eql 'Madrid traffic and polution'
expect(result.second.name).to eql 'New York polution'
expect(result.second.name).to eql 'New industries in York'
expect(result.third.name).to eql 'New York polution'
end
it 'ranks better matches with word repetition' do

@ -16,10 +16,11 @@ describe Carto::Api::TagsController do
before(:each) do
@mapaza = FactoryGirl.create(:carto_visualization, type: Carto::Visualization::TYPE_DERIVED,
user: @carto_user1, name: "mapaza",
tags: ["owned-tag", "map"])
tags: ["owned-tag"])
table = create_random_table(@user2)
shared_visualization = table.table_visualization
shared_visualization.name = "shared_table"
shared_visualization.tags = ["shared-tag"]
shared_visualization.save
shared_entity = CartoDB::SharedEntity.new(
@ -69,24 +70,40 @@ describe Carto::Api::TagsController do
]
login_as(@user1, scope: @user1.username)
get_json search_preview_url(q: "mapaza", types: "derived"), @headers do |response|
get_json search_preview_url(q: "mapaz", types: "derived"), @headers do |response|
expect(response.status).to eq(200)
expect(response.body[:result]).to eq expected_result
expect(response.body[:total_count]).to eq 1
end
end
it 'returns a 200 response with matching maps and tags' do
it 'returns a 200 response with matching maps, tables and tags' do
expected_result = [
{ type: "tag", name: "map", url: "http://#{@base_url}:53716/dashboard/search/tag/map" },
{ type: "tag", name: "owned-tag", url: "http://#{@base_url}:53716/dashboard/search/tag/owned-tag" },
{ type: "tag", name: "shared-tag", url: "http://#{@base_url}:53716/dashboard/search/tag/shared-tag" },
{ type: "table", name: "shared_table", url: "http://#{@base_url}:53716/tables/shared_table" },
{ type: "derived", name: "mapaza", url: "http://#{@base_url}:53716/viz/#{@mapaza.id}/map" }
]
login_as(@user1, scope: @user1.username)
get_json search_preview_url(q: "map", types: "derived,tag"), @headers do |response|
get_json search_preview_url(q: "tag", types: "derived,table,tag"), @headers do |response|
expect(response.status).to eq(200)
expect(response.body[:result]).to eq expected_result
expect(response.body[:total_count]).to eq 2
expect(response.body[:total_count]).to eq 4
end
end
it 'allows to limit total results' do
expected_result = [
{ type: "tag", name: "owned-tag", url: "http://#{@base_url}:53716/dashboard/search/tag/owned-tag" },
{ type: "tag", name: "shared-tag", url: "http://#{@base_url}:53716/dashboard/search/tag/shared-tag" }
]
login_as(@user1, scope: @user1.username)
get_json search_preview_url(q: "tag", types: "derived,table,tag", limit: 2), @headers do |response|
expect(response.status).to eq(200)
expect(response.body[:result]).to eq expected_result
expect(response.body[:total_count]).to eq 4
end
end

Loading…
Cancel
Save