Class: SupplejackApi::ConceptSearch

Inherits:
Search
  • Object
show all
Defined in:
app/models/supplejack_api/concept_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConceptSearch

Returns a new instance of ConceptSearch.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/supplejack_api/concept_search.rb', line 11

def initialize(options = {})
  @options = options.dup
  @options.reverse_merge!(
    and: {},
    or: {},
    without: {},
    page: 1,
    per_page: 20,
    sort: nil,
    direction: 'desc',
    fields: ConceptSchema.model_fields.keys.join(','),
    debug: nil
  )
end

Instance Method Details

#field_listObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/supplejack_api/concept_search.rb', line 66

def field_list
  return @field_list if @field_list
  model_fields = ConceptSchema.model_fields.dup
  valid_fields = model_fields.keep_if { |_key, field| field.try(:store).nil? }

  @field_list = options[:fields].split(',').map { |f| f.strip.tr(':', '_').to_sym }
  @field_list.keep_if do |f|
    valid_fields.include?(f)
  end

  @field_list
end

#group_listObject



79
80
81
82
83
84
# File 'app/models/supplejack_api/concept_search.rb', line 79

def group_list
  return @group_list if @group_list
  @group_list = options[:fields].split(',').map { |f| f.strip.to_sym }
  @group_list.keep_if { |f| ConceptSchema.groups.keys.include?(f) }
  @group_list
end

#query_fieldsObject

rubocop:enable Metrics/MethodLength



61
62
63
64
# File 'app/models/supplejack_api/concept_search.rb', line 61

def query_fields
  query_fields_list = super
  query_fields_list += [:name, :label] if (query_fields_list && [:name, :label]).present?
end

#search_builderObject

FIXME: make me smaller!



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
# File 'app/models/supplejack_api/concept_search.rb', line 27

def search_builder
  @search_builder ||= Sunspot.new_search(SupplejackApi::Concept) do
    spellcheck collate: true, only_more_popular: true if options[:suggest]

    options[:without].each do |name, values|
      values = values.split(',')
      values.each do |value|
        without(name, to_proper_value(name, value))
      end
    end

    if options[:geo_bbox]
      coords = options[:geo_bbox].split(',').map(&:to_f)
      with(:lat_lng).in_bounding_box([coords[2], coords[1]], [coords[0], coords[3]])
    end

    adjust_solr_params do |params|
      if options[:solr_query].present?
        params[:q] ||= ''
        params['q.alt'] = options[:solr_query]
        params[:defType] = 'dismax'
      end
    end

    order_by(sort, direction) if options[:sort].present?

    paginate page: page, per_page: per_page
  end

  @search_builder.build(&build_conditions)
  @search_builder
end