module SearchSolrTools::Helpers::SolrFormat
Methods for generating formatted values that can be indexed by SOLR
Constants
- DAILY_INDEX
- DATA_CENTER_NAMES
- DATE
- HOURLY_INDEX
- HTTP_URL_FORMAT
- MAX_DATE
- MIN_DATE
- MONTHLY_INDEX
- MULTIYEARLY_INDEX
- NOT_SPECIFIED
- REDUCE_TEMPORAL_DURATION
- SPATIAL_0_500_INDEX
- SPATIAL_16_30_INDEX
- SPATIAL_2_5_INDEX
- SPATIAL_501_1_INDEX
- SPATIAL_6_15_INDEX
- SPATIAL_GREATER_30_INDEX
- SPATIAL_RESOLUTION_FACET_VALUES
- SUBDAILY_INDEX
- SUBHOURLY_INDEX
- SUBMONTHLY_INDEX
- SUBYEARLY_INDEX
- TEMPORAL_RESOLUTION_FACET_VALUES
- WEEKLY_INDEX
- YEARLY_INDEX
Public Class Methods
bin(mappings, term)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 161 def self.bin(mappings, term) mappings.each do |mapping| term.match(mapping['pattern']) do return mapping['mapping'].upcase end end nil end
date?(date)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 235 def self.date?(date) return false unless date.is_a? String d = DateTime.parse(date.strip) rescue false DateTime.valid_date?(d.year, d.mon, d.day) unless d.eql?(false) end
date_str(date)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 149 def self.date_str(date) d = if date.is_a? String DateTime.parse(date.strip) rescue nil else date end "#{d.iso8601[0..-7]}Z" unless d.nil? end
facet_binning(type, format_string)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 88 def self.facet_binning(type, format_string) binned_facet = bin(FacetConfiguration.get_facet_bin(type), format_string) if binned_facet.nil? format_string elsif binned_facet.match?(/\Aexclude\z/i) nil else binned_facet end end
find_index_for_single_spatial_resolution_value(string_duration)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 190 def self.find_index_for_single_spatial_resolution_value(string_duration) value, units = string_duration.split if units == 'deg' spatial_resolution_index_degrees(value) elsif units == 'm' spatial_resolution_index_meters(value) end end
find_index_for_single_temporal_resolution_value(string_duration)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 170 def self.find_index_for_single_temporal_resolution_value(string_duration) iso8601_duration = ISO8601::Duration.new(string_duration) dur_sec = iso8601_duration.to_seconds case dur_sec when 0..3_599 then SUBHOURLY_INDEX when 3600 then HOURLY_INDEX when 3601..86_399 then SUBDAILY_INDEX when 86_400..172_800 then DAILY_INDEX when 172_801..691_200 then WEEKLY_INDEX when 691_201..1_728_000 then SUBMONTHLY_INDEX when 1_728_001..2_678_400 then MONTHLY_INDEX when 2_678_400..31_535_999 then SUBYEARLY_INDEX when 31_536_000 then YEARLY_INDEX else MULTIYEARLY_INDEX end end
format_date_for_index(date_str, default)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 242 def self.format_date_for_index(date_str, default) date_str = default unless date? date_str DateTime.parse(date_str).strftime('%C.%y%m%d') end
get_spatial_scope_facet_with_bounding_box(bbox)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 135 def self.get_spatial_scope_facet_with_bounding_box(bbox) if bbox.nil? || BoundingBoxUtil.box_invalid?(bbox) return nil elsif BoundingBoxUtil.box_global?(bbox) facet = 'Coverage from over 85 degrees North to -85 degrees South | Global' elsif BoundingBoxUtil.box_local?(bbox) facet = 'Less than 1 degree of latitude change | Local' else facet = 'Between 1 and 170 degrees of latitude change | Regional' end facet end
get_temporal_duration(start_time, end_time)
click to toggle source
returns the temporal duration in days; returns -1 if there is not a valid start date
# File lib/search_solr_tools/helpers/solr_format.rb, line 55 def self.get_temporal_duration(start_time, end_time) if start_time.to_s.empty? duration = nil else end_time = Time.now if end_time.to_s.empty? # datasets that cover just one day would have end_date - start_date = 0, # so we need to add 1 to make sure the duration is the actual number of # days; if the end date and start date are flipped in the metadata, a # negative duration doesn't make sense so use the absolute value duration = Integer((end_time - start_time).abs / 86_400) + 1 end duration end
get_temporal_duration_facet(duration)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 69 def self.get_temporal_duration_facet(duration) return NOT_SPECIFIED if duration.nil? years = duration.to_i / 365 temporal_duration_range(years) end
parameter_binning(parameter_string)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 99 def self.parameter_binning(parameter_string) binned_parameter = bin(FacetConfiguration.get_facet_bin('parameter'), parameter_string) return binned_parameter unless binned_parameter.nil? # if no mapping exists, use variable_level_1. # Force it to all upper case for consistency. This is a hacky workaround to # deal with deprecated GCMD keywords still in use by some datasets that result # in duplicate, case-sensitive entries in the search interface facet list. parts = parameter_string.split '>' return parts[3].strip.upcase if parts.length >= 4 nil end
reduce_temporal_duration(values)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 84 def self.reduce_temporal_duration(values) values.map { |v| Integer(v) rescue nil }.compact.max end
resolution_not_specified?(resolution)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 128 def self.resolution_not_specified?(resolution) return true if resolution.to_s.empty? return true unless %w[single range].include? resolution['type'] return true if resolution['type'] == 'single' && resolution['resolution'].to_s.empty? return true if resolution['type'] == 'range' && resolution['min_resolution'].to_s.empty? end
resolution_value(resolution, find_index_method, resolution_values)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 113 def self.resolution_value(resolution, find_index_method, resolution_values) return NOT_SPECIFIED if resolution_not_specified? resolution if resolution['type'] == 'single' i = send(find_index_method, resolution['resolution']) return resolution_values[i] end if resolution['type'] == 'range' i = send(find_index_method, resolution['min_resolution']) j = send(find_index_method, resolution['max_resolution']) return resolution_values[i..j] end raise "Invalid resolution #{resolution['type']}" end
spatial_resolution_index_degrees(degrees)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 200 def self.spatial_resolution_index_degrees(degrees) if degrees.to_f <= 0.05 SPATIAL_2_5_INDEX elsif degrees.to_f < 0.5 SPATIAL_16_30_INDEX else SPATIAL_GREATER_30_INDEX end end
spatial_resolution_index_meters(meters)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 210 def self.spatial_resolution_index_meters(meters) case meters.to_f when 0..500 then SPATIAL_0_500_INDEX when 500..1_000 then SPATIAL_501_1_INDEX when 1_000..5_000 then SPATIAL_2_5_INDEX when 5_000..15_000 then SPATIAL_6_15_INDEX when 15_000..30_000 then SPATIAL_16_30_INDEX else SPATIAL_GREATER_30_INDEX end end
temporal_display_str(date_range)
click to toggle source
# File lib/search_solr_tools/helpers/solr_format.rb, line 47 def self.temporal_display_str(date_range) temporal_str = (date_range[:start]).to_s temporal_str += ",#{date_range[:end]}" unless date_range[:end].nil? temporal_str end
temporal_duration_range(years)
click to toggle source
takes a temporal_duration in years, returns a string representing the range for faceting
# File lib/search_solr_tools/helpers/solr_format.rb, line 224 def self.temporal_duration_range(years) range = [] range.push '< 1 year' if years >= 0 && years < 1 range.push '1+ years' if years >= 1 range.push '5+ years' if years >= 5 range.push '10+ years' if years >= 10 range end
temporal_index_str(date_range)
click to toggle source
We are indexing date ranges a spatial coordinates. This means we have to convert dates into the format YY.YYMMDD which can be stored in the standard lat/long space For example: 2013-01-01T00:00:00Z to 2013-01-31T00:00:00Z will be converted to 20.130101, 20.130131. See wiki.apache.org/solr/SpatialForTimeDurations
# File lib/search_solr_tools/helpers/solr_format.rb, line 80 def self.temporal_index_str(date_range) "#{format_date_for_index date_range[:start], MIN_DATE} #{format_date_for_index(date_range[:end], MAX_DATE)}" end