Skip to content

Instantly share code, notes, and snippets.

@mattheworiordan
Last active August 29, 2015 14:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mattheworiordan/2a0e78adb2396abd864f to your computer and use it in GitHub Desktop.
Facets for Econsultancy based on our user interface design intentions
MODELS = [
'BlogPost',
'Event',
'PressRelease',
'Report',
'CaseStudy',
'TrainingCourse'
]
# Example search for just "test" with no filters, note, topics are ommitted from this example because too many results to be readable
results = Sunspot.search(MODELS.map(&:constantize)) do
fulltext "test"
facet :model_name
facet :sectors
end
results.total # => 7147
puts results.facet(:model_name).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# => {"BlogPost"=>5538, "PressRelease"=>867, "Report"=>632, "CaseStudy"=>67, "TrainingCourse"=>23, "Event"=>20}
puts results.facet(:sectors).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# => {"Retail"=>796, "Technology, Media & Telecoms (TMT)"=>535, "Travel & Leisure"=>198, "Financial Services"=>155, "Consumer Goods"=>144, "Automotive"=>100, "Professional Services"=>93, "Gaming & Gambling"=>71, "Energy & Utilities"=>56, "Healthcare & Pharmaceutical"=>54, "Charities, Government & Non Profit"=>51, "Transport"=>47, "Real Estate & Property"=>45, "Manufacturing"=>35, "Education"=>30, "Agriculture"=>21}
# Example search for just "test" filtered by Content Type Report
results = Sunspot.search(MODELS.map(&:constantize)) do
fulltext "test"
model_filter = with(:model_name, 'Report')
facet :model_name, exclude: model_filter
facet :sectors
end
results.total # => 632
puts results.facet(:model_name).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# With the exclude, we now get the correct values for the content filter facets ignoring the Report filter applied
# => {"BlogPost"=>5538, "PressRelease"=>867, "Report"=>632, "CaseStudy"=>67, "TrainingCourse"=>23, "Event"=>20}
# However the sectors facets now only show sector facets that exist in Reports
puts results.facet(:sectors).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# => {"Retail"=>54, "Travel & Leisure"=>42, "Financial Services"=>41, "Automotive"=>28, "Consumer Goods"=>27, "Healthcare & Pharmaceutical"=>25, "Gaming & Gambling"=>22, "Technology, Media & Telecoms (TMT)"=>22, "Energy & Utilities"=>21, "Real Estate & Property"=>21, "Professional Services"=>18, "Manufacturing"=>12, "Transport"=>10, "Charities, Government & Non Profit"=>9, "Education"=>8, "Agriculture"=>6}
# Example search for just "test" filtered by Content Type Report and Sector Automotive
results = Sunspot.search(MODELS.map(&:constantize)) do
fulltext "test"
model_filter = with(:model_name, 'Report')
facet :model_name, exclude: model_filter
sector_filter = with(:sectors, 'Automotive')
facet :sectors, exclude: sector_filter, name: 'sector_filter_with_exclusion'
facet :sectors, name: 'sector_filter_without_exclusion'
end
results.total # => 28
puts results.facet(:model_name).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# With the exclude, we now get the correct values for the content filter facets ignoring the Report filter applied, but with the sector filter applied
# => {"BlogPost"=>63, "Report"=>28, "CaseStudy"=>9}
# The sectors facets shoudl remain unchanged from previous example as we exclude the filter applied to the drop down intentionally
puts results.facet(:sector_filter_with_exclusion).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# => {"Retail"=>54, "Travel & Leisure"=>42, "Financial Services"=>41, "Automotive"=>28, "Consumer Goods"=>27, "Healthcare & Pharmaceutical"=>25, "Gaming & Gambling"=>22, "Technology, Media & Telecoms (TMT)"=>22, "Energy & Utilities"=>21, "Real Estate & Property"=>21, "Professional Services"=>18, "Manufacturing"=>12, "Transport"=>10, "Charities, Government & Non Profit"=>9, "Education"=>8, "Agriculture"=>6}
# This example shows you can you build an extra set of facets using the same field
# This could be used to build multiple facets on EditorialMetadata with difference exclusions very easily
puts results.facet(:sector_filter_without_exclusion).rows.inject({}) { |hash, facet| hash[facet.value] = facet.count; hash }
# => {"Automotive"=>28, "Financial Services"=>26, "Travel & Leisure"=>24, "Retail"=>23, "Healthcare & Pharmaceutical"=>21, "Energy & Utilities"=>20, "Consumer Goods"=>19, "Real Estate & Property"=>19, "Gaming & Gambling"=>16, "Professional Services"=>14, "Technology, Media & Telecoms (TMT)"=>14, "Manufacturing"=>11, "Transport"=>10, "Charities, Government & Non Profit"=>9, "Education"=>8, "Agriculture"=>6}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment