Class: Solr::Facet
Overview
A representation of a Solr facet
Solr facets arrive in a variety of formats, and thus have to be parsed in a variety of ways. This class attempts to handle all of that in the most generic and extensible way possible.
Instance Attribute Summary (collapse)
-
- (Symbol) field
The field that we are faceting on.
-
- (String) field_label
field in human-readable form.
-
- (Integer) hits
The number of hits for this facet.
-
- (String) label
value in human-readable form.
-
- (String) query
The Solr filter query for this facet.
-
- (String) value
The value for this facet.
Instance Method Summary (collapse)
-
- (Integer) <=>(other)
Compare facet objects appropriately given their field.
- - (Object) format_year_label private
-
- (Facet) initialize(options = {})
constructor
Create a new facet.
Constructor Details
- (Facet) initialize(options = {})
Create a new facet
We can either get a string-format query, or (from RSolr::Ext) a facet object and an item object. This handles dealing with all of that. We will get either :name, :value, and :hits (a facet parameter), or :query and :hits (a facet query).
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/solr/facet.rb', line 39 def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end |
Instance Attribute Details
- (Symbol) field
The field that we are faceting on
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solr/facet.rb', line 24 class Facet attr_accessor :query, :field, :value, :hits, :label, :field_label # Create a new facet # # We can either get a string-format query, or (from RSolr::Ext) a facet # object and an item object. This handles dealing with all of that. We # will get either +:name+, +:value+, and +:hits+ (a facet parameter), or # +:query+ and +:hits+ (a facet query). # # @param [Hash] options specification of the new facet # @option options [Symbol] :name The field being faceted on # @option options [String] :value The facet value # @option options [Integer] :hits Number of hits for this facet # @option options [String] :query Facet as a string query def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end include Comparable # Compare facet objects appropriately given their field # # In general, this sorts first by count and then by value. # # @param [Facet] other object for comparison # @return [Integer] -1, 0, or 1, appropriately def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end private def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end end |
- (String) field_label
field in human-readable form
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solr/facet.rb', line 24 class Facet attr_accessor :query, :field, :value, :hits, :label, :field_label # Create a new facet # # We can either get a string-format query, or (from RSolr::Ext) a facet # object and an item object. This handles dealing with all of that. We # will get either +:name+, +:value+, and +:hits+ (a facet parameter), or # +:query+ and +:hits+ (a facet query). # # @param [Hash] options specification of the new facet # @option options [Symbol] :name The field being faceted on # @option options [String] :value The facet value # @option options [Integer] :hits Number of hits for this facet # @option options [String] :query Facet as a string query def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end include Comparable # Compare facet objects appropriately given their field # # In general, this sorts first by count and then by value. # # @param [Facet] other object for comparison # @return [Integer] -1, 0, or 1, appropriately def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end private def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end end |
- (Integer) hits
The number of hits for this facet
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solr/facet.rb', line 24 class Facet attr_accessor :query, :field, :value, :hits, :label, :field_label # Create a new facet # # We can either get a string-format query, or (from RSolr::Ext) a facet # object and an item object. This handles dealing with all of that. We # will get either +:name+, +:value+, and +:hits+ (a facet parameter), or # +:query+ and +:hits+ (a facet query). # # @param [Hash] options specification of the new facet # @option options [Symbol] :name The field being faceted on # @option options [String] :value The facet value # @option options [Integer] :hits Number of hits for this facet # @option options [String] :query Facet as a string query def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end include Comparable # Compare facet objects appropriately given their field # # In general, this sorts first by count and then by value. # # @param [Facet] other object for comparison # @return [Integer] -1, 0, or 1, appropriately def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end private def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end end |
- (String) label
value in human-readable form
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solr/facet.rb', line 24 class Facet attr_accessor :query, :field, :value, :hits, :label, :field_label # Create a new facet # # We can either get a string-format query, or (from RSolr::Ext) a facet # object and an item object. This handles dealing with all of that. We # will get either +:name+, +:value+, and +:hits+ (a facet parameter), or # +:query+ and +:hits+ (a facet query). # # @param [Hash] options specification of the new facet # @option options [Symbol] :name The field being faceted on # @option options [String] :value The facet value # @option options [Integer] :hits Number of hits for this facet # @option options [String] :query Facet as a string query def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end include Comparable # Compare facet objects appropriately given their field # # In general, this sorts first by count and then by value. # # @param [Facet] other object for comparison # @return [Integer] -1, 0, or 1, appropriately def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end private def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end end |
- (String) query
The Solr filter query for this facet
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solr/facet.rb', line 24 class Facet attr_accessor :query, :field, :value, :hits, :label, :field_label # Create a new facet # # We can either get a string-format query, or (from RSolr::Ext) a facet # object and an item object. This handles dealing with all of that. We # will get either +:name+, +:value+, and +:hits+ (a facet parameter), or # +:query+ and +:hits+ (a facet query). # # @param [Hash] options specification of the new facet # @option options [Symbol] :name The field being faceted on # @option options [String] :value The facet value # @option options [Integer] :hits Number of hits for this facet # @option options [String] :query Facet as a string query def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end include Comparable # Compare facet objects appropriately given their field # # In general, this sorts first by count and then by value. # # @param [Facet] other object for comparison # @return [Integer] -1, 0, or 1, appropriately def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end private def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end end |
- (String) value
The value for this facet
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solr/facet.rb', line 24 class Facet attr_accessor :query, :field, :value, :hits, :label, :field_label # Create a new facet # # We can either get a string-format query, or (from RSolr::Ext) a facet # object and an item object. This handles dealing with all of that. We # will get either +:name+, +:value+, and +:hits+ (a facet parameter), or # +:query+ and +:hits+ (a facet query). # # @param [Hash] options specification of the new facet # @option options [Symbol] :name The field being faceted on # @option options [String] :value The facet value # @option options [Integer] :hits Number of hits for this facet # @option options [String] :query Facet as a string query def initialize( = {}) if [:query] # We already have a query here, so go ahead and save the query @query = [:query] raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Basic format: "field:QUERY" parts = @query.split(':') raise ArgumentError unless parts.count == 2 @field = parts[0].to_sym @value = parts[1] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' # Format the label according to the field type -- for now, the only # argument type is year, so raise an error otherwise raise ArgumentError unless @field == :year format_year_label return end # We need to have name, value, and hits raise ArgumentError unless [:name] @field = [:name].to_sym raise ArgumentError unless [:value] @value = [:value] # Strip quotes from the value if present @value = @value[1..-2] if @value[0] == '"' && @value[-1] == '"' raise ArgumentError unless [:hits] @hits = Integer([:hits]) # Construct the query @query = "#{field.to_s}:\"#{value}\"" # Format the label case @field when :authors_facet @label = @value @field_label = I18n.t('search.index.authors_facet_short') when :journal_facet @label = @value @field_label = I18n.t('search.index.journal_facet_short') else @label = @value # Bad fallback, but it will do @field_label = @field.to_s end end include Comparable # Compare facet objects appropriately given their field # # In general, this sorts first by count and then by value. # # @param [Facet] other object for comparison # @return [Integer] -1, 0, or 1, appropriately def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end private def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end end |
Instance Method Details
- (Integer) <=>(other)
Compare facet objects appropriately given their field
In general, this sorts first by count and then by value.
104 105 106 107 108 109 110 |
# File 'lib/solr/facet.rb', line 104 def <=>(other) return -(@hits <=> other.hits) if hits != other.hits # We want years to sort inverse, while we want others normal. return -(@value <=> other.value) if field == :year (@value <=> other.value) end |
- (Object) format_year_label (private)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/solr/facet.rb', line 114 def format_year_label # We need to parse the decade out of "[X TO Y]" value_without_brackets = @value[1..-2] parts = value_without_brackets.split raise ArgumentError unless parts.count == 3 decade = parts[0] if decade == '*' decade = '1790' end decade = Integer(decade) if decade == 1790 @label = I18n.t('search.index.year_before_1800') elsif decade == 2010 @label = I18n.t('search.index.year_after_2010') else @label = "#{decade}–#{decade + 9}" end @field_label = I18n.t('search.index.year_facet_short') end |