Class: Europeana::Blacklight::SearchBuilder

Inherits:
Blacklight::SearchBuilder
  • Object
show all
Includes:
FacetPagination, MoreLikeThis, OverlayParams, Ranges
Defined in:
lib/europeana/blacklight/search_builder.rb,
lib/europeana/blacklight/search_builder/ranges.rb,
lib/europeana/blacklight/search_builder/more_like_this.rb,
lib/europeana/blacklight/search_builder/overlay_params.rb,
lib/europeana/blacklight/search_builder/facet_pagination.rb

Overview

Core search builder for ApiRepository

Defined Under Namespace

Modules: FacetPagination, MoreLikeThis, OverlayParams, Ranges

Constant Summary collapse

STANDALONE_FACETS =
%w(COLOURPALETTE MEDIA REUSABILITY THUMBNAIL).freeze
MEDIA_FACETS =
%w(COLOURPALETTE IMAGE_ASPECTRATIO IMAGE_COLOR IMAGE_COLOUR
IMAGE_GRAYSCALE IMAGE_GREYSCALE IMAGE_SIZE MEDIA MIME_TYPE
SOUND_DURATION SOUND_HQ TEXT_FULLTEXT VIDEO_DURATION
VIDEO_HD).freeze

Instance Method Summary collapse

Methods included from OverlayParams

#add_overlay_params_to_api, #with_overlay_params

Methods included from Ranges

#add_range_qf_to_api

Methods included from MoreLikeThis

#add_mlt_to_api

Methods included from FacetPagination

#add_facet_paging_to_api

Instance Method Details

#add_api_url_to_api(api_parameters) ⇒ Object



176
177
178
179
# File 'lib/europeana/blacklight/search_builder.rb', line 176

def add_api_url_to_api(api_parameters)
  return unless blacklight_params[:api_url]
  api_parameters[:api_url] = blacklight_params[:api_url]
end

#add_facet_qf_to_api(api_parameters) ⇒ Object

TODO:

Handle different types of value, like Blacklight::Solr::SearchBuilder#facet_value_to_fq_string does

Facet filtering of results

Maps Blacklight’s :f param to API’s :qf param.



93
94
95
96
97
98
99
100
101
102
# File 'lib/europeana/blacklight/search_builder.rb', line 93

def add_facet_qf_to_api(api_parameters)
  return unless blacklight_params[:f]

  salient_facets_for_api_facet_qf.each_pair do |facet_field, values|
    [values].flatten.compact.each do |value|
      api_parameters[:qf] ||= []
      api_parameters[:qf] << "#{facet_field}:" + quote_facet_value(facet_field, value)
    end
  end
end

#add_facetting_to_api(api_parameters) ⇒ Object

TODO:

Handle facet settings like query, sort, pivot, etc, like Blacklight::Solr::SearchBuilder#add_facetting_to_solr does

Request facet data in results, respecting configured limits



151
152
153
154
155
156
157
# File 'lib/europeana/blacklight/search_builder.rb', line 151

def add_facetting_to_api(api_parameters)
  api_parameters[:facet] = api_request_facet_fields.keys.uniq.join(',')

  api_request_facet_fields.each do |field_name, facet|
    api_parameters[:"f.#{facet.field}.facet.limit"] = facet_limit_for(field_name) if facet_limit_for(field_name)
  end
end

#add_paging_to_api(api_parameters) ⇒ Object

copy paging params from BL app over to API, changing app level per_page and page to API rows and start.



162
163
164
165
166
167
# File 'lib/europeana/blacklight/search_builder.rb', line 162

def add_paging_to_api(api_parameters)
  rows(api_parameters[:rows] || 10) if rows.nil?
  api_parameters[:rows] = rows

  api_parameters[:start] = start unless start == 0
end

#add_profile_to_api(api_parameters) ⇒ Object

Set the profile type



53
54
55
56
# File 'lib/europeana/blacklight/search_builder.rb', line 53

def add_profile_to_api(api_parameters)
  api_parameters[:profile] = 'params rich'
  api_parameters[:profile] << ' facets' if blacklight_config.facet_fields
end

#add_qf_to_api(api_parameters) ⇒ Object

Add the user’s query filter terms



79
80
81
82
83
# File 'lib/europeana/blacklight/search_builder.rb', line 79

def add_qf_to_api(api_parameters)
  return unless blacklight_params[:qf]
  api_parameters[:qf] ||= []
  api_parameters[:qf] = api_parameters[:qf] + blacklight_params[:qf]
end

#add_query_facet_to_api(_api_parameters) ⇒ Object

Filter results by a query facet



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/europeana/blacklight/search_builder.rb', line 118

def add_query_facet_to_api(_api_parameters)
  return unless blacklight_params[:f]

  salient_facets = blacklight_params[:f].select do |k, _v|
    facet = blacklight_config.facet_fields[k]
    facet.present? && facet.query && (facet.include_in_request || (facet.include_in_request.nil? && blacklight_config.add_facet_fields_to_solr_request))
  end

  salient_facets.each_pair do |facet_field, value_list|
    Array(value_list).reject(&:blank?).each do |value|
      with_overlay_params(blacklight_config.facet_fields[facet_field].query[value][:fq])
    end
  end
end

#add_query_to_api(api_parameters) ⇒ Object

Take the user-entered query, and put it in the API params, including config’s “search field” params for current search field.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/europeana/blacklight/search_builder.rb', line 63

def add_query_to_api(api_parameters)
  if [blacklight_params[:q]].flatten.reject(&:blank?).blank?
    query = '*:*'
  elsif search_field && search_field.field.present?
    query = "#{search_field.field}:#{blacklight_params[:q]}"
  elsif blacklight_params[:q].is_a?(Hash)
    # @todo when would it be a Hash?
    query = nil
  elsif blacklight_params[:q]
    query = blacklight_params[:q]
  end
  append_to_query_param(api_parameters, query)
end

#add_sorting_to_api(_api_parameters) ⇒ Object



171
172
173
174
# File 'lib/europeana/blacklight/search_builder.rb', line 171

def add_sorting_to_api(_api_parameters)
  return if sort.blank?
  Europeana::API.logger.warn('Europeana REST API does not support sorting')
end

#add_standalone_facets_to_api(api_parameters) ⇒ Object

Some facets need to be filtered as distinct API params, even though they are returned with the facets in a search response



136
137
138
139
140
141
142
# File 'lib/europeana/blacklight/search_builder.rb', line 136

def add_standalone_facets_to_api(api_parameters)
  STANDALONE_FACETS.each do |field|
    if blacklight_params[:f] && blacklight_params[:f][field]
      api_parameters[field.downcase.to_sym] = blacklight_params[:f][field].join(',')
    end
  end
end

#default_api_parameters(api_parameters) ⇒ Object

TODO:

Rename default_solr_params to default_params upstream

Start with general defaults from BL config. Need to use custom merge to dup values, to avoid later mutating the original by mistake.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/europeana/blacklight/search_builder.rb', line 37

def default_api_parameters(api_parameters)
  blacklight_config.default_solr_params.each do |key, value|
    if value.respond_to?(:deep_dup)
      api_parameters[key] = value.deep_dup
    elsif value.respond_to?(:dup) && value.duplicable?
      api_parameters[key] = value.dup
    else
      api_parameters[key] = value
    end
  end
end

#quote_facet_value(facet_field, value) ⇒ Object



110
111
112
113
114
# File 'lib/europeana/blacklight/search_builder.rb', line 110

def quote_facet_value(facet_field, value)
  return value if MEDIA_FACETS.include?(facet_field)
  return value if value.include?('*')
  '"' + value.gsub('"', '\"') + '"'
end

#salient_facets_for_api_facet_qfObject



104
105
106
107
108
# File 'lib/europeana/blacklight/search_builder.rb', line 104

def salient_facets_for_api_facet_qf
  blacklight_params[:f].select do |k, _v|
    !STANDALONE_FACETS.include?(k) && api_request_facet_fields.keys.include?(k)
  end
end

#start(start = nil) ⇒ Object

Europeana API start param counts from 1



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/europeana/blacklight/search_builder.rb', line 183

def start(start = nil)
  if start
    params_will_change!
    @start = start.to_i
    self
  else
    @start ||= (page - 1) * (rows || 10) + 1

    val = @start || 1
    val = 1 if @start < 1
    val
  end
end