Class: Sunspot::Query::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/query/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ Query

Returns a new instance of Query.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sunspot/query/query.rb', line 7

def initialize(types)
  @scope = Scope.new
  @field_facets = []
  @query_facets = {}
  @sort = SortComposite.new
  if types.length == 1
    @scope.add_restriction(TypeField.instance, Restriction::EqualTo, types.first)
  else
    @scope.add_restriction(TypeField.instance, Restriction::AnyOf, types)
  end
end

Instance Attribute Details

#fulltextObject

Returns the value of attribute fulltext.



5
6
7
# File 'lib/sunspot/query/query.rb', line 5

def fulltext
  @fulltext
end

#scopeObject

Returns the value of attribute scope.



4
5
6
# File 'lib/sunspot/query/query.rb', line 4

def scope
  @scope
end

Instance Method Details

#add_field_facet(field, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/sunspot/query/query.rb', line 27

def add_field_facet(field, options = {})
  facet = FieldFacet.build(field, options)
  if facet.is_a?(QueryFacet)
    @query_facets[field.name.to_sym] = facet
  else
    @field_facets << facet
  end
end

#add_location_restriction(coordinates, radius) ⇒ Object



23
24
25
# File 'lib/sunspot/query/query.rb', line 23

def add_location_restriction(coordinates, radius)
  @local = Local.new(coordinates, radius)
end

#add_query_facet(name, options = {}) ⇒ Object



36
37
38
# File 'lib/sunspot/query/query.rb', line 36

def add_query_facet(name, options = {})
  @query_facets[name.to_sym] = QueryFacet.new(name, options)
end

#add_sort(sort) ⇒ Object



40
41
42
# File 'lib/sunspot/query/query.rb', line 40

def add_sort(sort)
  @sort << sort
end

#pageObject



80
81
82
# File 'lib/sunspot/query/query.rb', line 80

def page
  @pagination.page if @pagination
end

#paginate(page, per_page) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/sunspot/query/query.rb', line 44

def paginate(page, per_page)
  if @pagination
    @pagination.page = page
    @pagination.per_page = per_page
  else
    @pagination = Pagination.new(page, per_page)
  end
end

#per_pageObject



84
85
86
# File 'lib/sunspot/query/query.rb', line 84

def per_page
  @pagination.per_page if @pagination
end

#query_facet(name) ⇒ Object



88
89
90
# File 'lib/sunspot/query/query.rb', line 88

def query_facet(name)
  @query_facets[name] if @query_facets
end

#set_fulltext(keywords) ⇒ Object



19
20
21
# File 'lib/sunspot/query/query.rb', line 19

def set_fulltext(keywords)
  @fulltext = Dismax.new(keywords)
end

#to_paramsObject



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
# File 'lib/sunspot/query/query.rb', line 53

def to_params
  params = 
    if @local
      if @fulltext
        raise(
          IllegalSearchError,
          "Can't perform search with both fulltext and geographical components due to LocalSolr limitations"
        )
      end
      { :q => @scope.to_boolean_phrase }
    else
      @scope.to_params
    end
  Sunspot::Util.deep_merge!(params, @fulltext.to_params) if @fulltext
  @field_facets.each do |facet|
    Sunspot::Util.deep_merge!(params, facet.to_params)
  end
  @query_facets.values.each do |facet|
    Sunspot::Util.deep_merge!(params, facet.to_params)
  end
  Sunspot::Util.deep_merge!(params, @sort.to_params)
  Sunspot::Util.deep_merge!(params, @pagination.to_params) if @pagination
  Sunspot::Util.deep_merge!(params, @local.to_params) if @local
  params[:q] ||= '*:*'
  params
end