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)

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_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.



88
89
90
91
92
93
94
95
96
97
# File 'lib/europeana/blacklight/search_builder.rb', line 88

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



146
147
148
149
150
151
152
# File 'lib/europeana/blacklight/search_builder.rb', line 146

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.



157
158
159
160
161
162
# File 'lib/europeana/blacklight/search_builder.rb', line 157

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



48
49
50
51
# File 'lib/europeana/blacklight/search_builder.rb', line 48

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



74
75
76
77
78
# File 'lib/europeana/blacklight/search_builder.rb', line 74

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/europeana/blacklight/search_builder.rb', line 113

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.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/europeana/blacklight/search_builder.rb', line 58

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



166
167
168
169
# File 'lib/europeana/blacklight/search_builder.rb', line 166

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



131
132
133
134
135
136
137
# File 'lib/europeana/blacklight/search_builder.rb', line 131

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.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/europeana/blacklight/search_builder.rb', line 32

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



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

def quote_facet_value(facet_field, value)
  return value if Europeana::API::Search::Fields::MEDIA.include?(facet_field)
  return value if value.include?('*')
  '"' + value.gsub('"', '\"') + '"'
end

#salient_facets_for_api_facet_qfObject



99
100
101
102
103
# File 'lib/europeana/blacklight/search_builder.rb', line 99

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



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/europeana/blacklight/search_builder.rb', line 173

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