Class: CustomAttributes::SearchQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_attributes/search_query.rb

Overview

Builds a query in elastic search DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#customizableObject

Returns the value of attribute customizable.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def customizable
  @customizable
end

#defaultsObject

Returns the value of attribute defaults.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def defaults
  @defaults
end

#field_listObject

Returns the value of attribute field_list.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def field_list
  @field_list
end

#filter_byObject

Returns the value of attribute filter_by.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def filter_by
  @filter_by
end

#filter_by_delimiterObject

Returns the value of attribute filter_by_delimiter.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def filter_by_delimiter
  @filter_by_delimiter
end

#filter_orObject

Returns the value of attribute filter_or.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def filter_or
  @filter_or
end

#filter_or_delimiterObject

Returns the value of attribute filter_or_delimiter.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def filter_or_delimiter
  @filter_or_delimiter
end

#match_anyObject

Returns the value of attribute match_any.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def match_any
  @match_any
end

#pageObject

Returns the value of attribute page.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def page
  @page
end

#per_pageObject

Returns the value of attribute per_page.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def per_page
  @per_page
end

#queryObject

fulltext search query, by default applied to all fields



22
23
24
# File 'lib/custom_attributes/search_query.rb', line 22

def query
  @query
end

#sort_byObject

Returns the value of attribute sort_by.



6
7
8
# File 'lib/custom_attributes/search_query.rb', line 6

def sort_by
  @sort_by
end

Instance Method Details

#buildObject



8
9
10
11
12
13
14
15
# File 'lib/custom_attributes/search_query.rb', line 8

def build
  {
    query: subquery,
    from: from,
    size: per_page,
    sort: sort_hash_to_array
  }
end

#default_fieldsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/custom_attributes/search_query.rb', line 69

def default_fields
  default_fields = {
    custom_fields: Hash[customizable.available_custom_fields
                                     .select { |cf| cf.searchable == true }
                                     .map { |cf| [cf.slug.to_sym, {}] }]
  }

  if customizable.present?
    default_fields[:fields] = Hash[
      customizable.class.default_fields.map { |f| [f.to_sym, {}] }
    ]
  end

  default_fields
end

#fromObject



17
18
19
# File 'lib/custom_attributes/search_query.rb', line 17

def from
  (page - 1) * per_page
end

#match_any?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/custom_attributes/search_query.rb', line 65

def match_any?
  @match_any
end