Class: Elasticord::Client::SearchBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/elasticord/client/search_builder.rb

Constant Summary collapse

PAGE_DEFAULT =
1
PER_PAGE_DEFAULT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elastic_search_client, entity, index, type) ⇒ SearchBuilder

Returns a new instance of SearchBuilder.



11
12
13
14
# File 'lib/elasticord/client/search_builder.rb', line 11

def initialize(elastic_search_client, entity, index, type)
  @elastic_search_client, @entity, @index, @type =
    elastic_search_client, entity, index, type
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



16
17
18
# File 'lib/elasticord/client/search_builder.rb', line 16

def index
  @index
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/elasticord/client/search_builder.rb', line 16

def type
  @type
end

Instance Method Details

#body_paramsObject



77
78
79
# File 'lib/elasticord/client/search_builder.rb', line 77

def body_params
  @body_params ||= { size: size, from: from, query: { bool: {} } }
end

#load(body = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elasticord/client/search_builder.rb', line 37

def load(body = {})
  begin
    elastic_results = execute_search(body)

    results.total_results = elastic_results['total']

    elastic_results['hits'].map do |result|
      results.push entity.new(result['_source'])
    end
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
    nil
  end

  results
end

#page(new_page) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/elasticord/client/search_builder.rb', line 20

def page(new_page)
  results.current_page = new_page

  body_params[:from] = results.from

  self
end

#per(new_per_page) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/elasticord/client/search_builder.rb', line 28

def per(new_per_page)
  results.per_page = new_per_page

  body_params[:size] = results.size
  body_params[:from] = results.from

  self
end

#ransack(ransack_filters = {}) ⇒ Object

def ft(full_text_term)

return self unless full_text_term

params = (body_params[:query][:bool][:must] ||= [])

# params[:must] << { match: { title: full_text_term } }

end



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elasticord/client/search_builder.rb', line 61

def ransack(ransack_filters = {})
  return self unless ransack_filters

  params = body_params[:query][:bool]

  ransack_filters.each do |key, value|
    if key['_eq_any']
      add_should_clause(params, key, value)
    elsif key['_eq']
      add_must_clause(params, key, value)
    end
  end

  self
end