Skip to content

Commit f72cca3

Browse files
authored
Merge pull request #2275 from broadinstitute/jb-search-filter-order
Better sorting/uniquifying of facet filters (SCP-6023)
2 parents 4a33df0 + d3a3b74 commit f72cca3

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

app/models/search_facet.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,14 @@ def get_unique_filter_values(public_only: false)
400400
filter_map += filters_from_metadatum(metadatum)
401401
end
402402
end
403-
filter_map.uniq
403+
return filter_map if is_numeric?
404+
405+
filter_map.uniq { |filter| [filter[:id]&.downcase, filter[:name]&.downcase] }.reject do |filter|
406+
filter[:id].blank? || filter[:name].blank?
407+
end
404408
end
405409

406-
# find all matching CellMetadum objects for a given facet
410+
# find all matching CellMetadatum objects for a given facet
407411
# can scope query to matching values array if needed or by a sub-query if provided
408412
def associated_metadata(values:, query: {})
409413
annotation_type = is_numeric? ? 'numeric' : 'group'
@@ -452,15 +456,15 @@ def update_filter_values!(external_facet = nil)
452456
merged_values = values.dup
453457
if external_facet[:filters]
454458
Rails.logger.info "Merging #{external_facet[:filters]} into '#{name}' facet filters"
455-
external_facet[:filters].each do |filter|
459+
external_facet[:filters].uniq(&:downcase).each do |filter|
456460
merged_values << { id: filter, name: filter } unless filters_include?(filter)
457461
end
458462
end
459463
return false if values.empty? # found no results, meaning an error occurred
460464

461-
values.sort_by! { |f| f[:name] }
462-
merged_values.sort_by! { |f| f[:name] }
463-
public_values = get_unique_filter_values(public_only: true)
465+
values.sort_by! { |f| f[:name].downcase }
466+
merged_values.sort_by! { |f| f[:name].downcase }
467+
public_values = get_unique_filter_values(public_only: true).sort_by { |f| f[:name].downcase }
464468
update(filters: values, public_filters: public_values, filters_with_external: merged_values)
465469
end
466470
end

test/models/search_facet_test.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ class SearchFacetTest < ActiveSupport::TestCase
4343
{ id: 'NCBITaxon_9606', name: 'Homo sapiens' },
4444
{ id: 'NCBITaxon_10090', name: 'Mus musculus' }
4545
]
46+
47+
SearchFacet.create(name: 'Cell type', identifier: 'cell_type', is_mongo_based: true,
48+
ontology_urls: [
49+
{
50+
name: 'Cell Ontology',
51+
url: 'https://www.ebi.ac.uk/ols/api/ontologies/cl',
52+
browser_url: 'https://www.ebi.ac.uk/ols/ontologies/cl'
53+
}
54+
],
55+
data_type: 'string', is_ontology_based: true, is_array_based: false,
56+
big_query_id_column: 'cell_type',
57+
big_query_name_column: 'cell_type__ontology_label',
58+
convention_name: 'Alexandria Metadata Convention', convention_version: '2.2.0')
4659
end
4760

4861
after(:all) do
@@ -158,6 +171,7 @@ class SearchFacetTest < ActiveSupport::TestCase
158171
test 'should find all filter matches' do
159172
azul_diseases = AzulSearchService.get_all_facet_filters['disease']
160173
disease_keyword = 'cancer'
174+
skip 'Azul search service not available' unless azul_diseases
161175
cancers = azul_diseases[:filters].select { |d| d.match?(disease_keyword) }
162176
disease_facet = SearchFacet.find_by(identifier: 'disease')
163177
disease_facet.update_filter_values!(azul_diseases)
@@ -215,18 +229,7 @@ class SearchFacetTest < ActiveSupport::TestCase
215229
]
216230
}
217231
])
218-
facet = SearchFacet.create(name: 'Cell type', identifier: 'cell_type', is_mongo_based: true,
219-
ontology_urls: [
220-
{
221-
name: 'Cell Ontology',
222-
url: 'https://www.ebi.ac.uk/ols/api/ontologies/cl',
223-
browser_url: 'https://www.ebi.ac.uk/ols/ontologies/cl'
224-
}
225-
],
226-
data_type: 'string', is_ontology_based: true, is_array_based: false,
227-
big_query_id_column: 'cell_type',
228-
big_query_name_column: 'cell_type__ontology_label',
229-
convention_name: 'Alexandria Metadata Convention', convention_version: '2.2.0')
232+
facet = SearchFacet.find_by(identifier: 'cell_type')
230233
expected_filters = [
231234
{ id: 'CL_0000236', name: 'B cell' },
232235
{ id: 'CL_0000561', name: 'amacrine cell' },
@@ -255,4 +258,16 @@ class SearchFacetTest < ActiveSupport::TestCase
255258
facet.reload
256259
assert_equal expected_filter, facet.filter_for_presence
257260
end
261+
262+
test 'should sort and uniquify filters properly' do
263+
facet = SearchFacet.find_by(identifier: 'cell_type')
264+
facet.update_filter_values!
265+
assert facet.filters.first[:name] == 'amacrine cell'
266+
external_filters = { filters: ['b cell', 'B cell', 'amacrine cell', 'Amacrine Cell', 'T cell'] }
267+
facet.update_filter_values!(external_filters)
268+
facet.reload
269+
assert_equal 1, facet.filters_with_external.select { |f| f[:name] == 'amacrine cell' }.count
270+
assert_equal 1, facet.filters_with_external.select { |f| f[:name] == 'T cell' }.count
271+
assert_equal 'T cell', facet.filters_with_external.last[:name]
272+
end
258273
end

0 commit comments

Comments
 (0)