class Elasticord::Client::SearchBuilder
Constants
- PAGE_DEFAULT
- PER_PAGE_DEFAULT
Attributes
elastic_search_client[R]
entity[R]
index[R]
type[R]
Public Class Methods
new(elastic_search_client, entity, index, type)
click to toggle source
# 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
Public Instance Methods
body_params()
click to toggle source
# File lib/elasticord/client/search_builder.rb, line 77 def body_params @body_params ||= { size: size, from: from, query: { bool: {} } } end
load(body = {})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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 = {})
click to toggle source
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
# 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
Protected Instance Methods
add_must_clause(params, key, value)
click to toggle source
# File lib/elasticord/client/search_builder.rb, line 99 def add_must_clause(params, key, value) params[:must] ||= [] key = key.to_s.split('_eq').first.to_sym add_term(params[:must], key, value) end
add_should_clause(params, key, values)
click to toggle source
# File lib/elasticord/client/search_builder.rb, line 107 def add_should_clause(params, key, values) params[:boost] = 1.0 params[:should] ||= [] params[:minimum_should_match] = 1 key = key.to_s.split('_eq_any').first.to_sym [*values].each { |value| add_term(params[:should], key, value) } end
execute_search(body)
click to toggle source
# File lib/elasticord/client/search_builder.rb, line 90 def execute_search(body) elastic_results = elastic_search_client.search \ index: index, type: type, body: body_params.merge(body) hits = elastic_results['hits'] (hits && hits['total'].to_i > 0) ? hits : { 'hits' => [], 'total' => 0 } end
results()
click to toggle source
# File lib/elasticord/client/search_builder.rb, line 85 def results @results ||= ArrayWithMetaData.new \ PAGE_DEFAULT, PER_PAGE_DEFAULT end
Private Instance Methods
add_term(params, key, value)
click to toggle source
# File lib/elasticord/client/search_builder.rb, line 119 def add_term(params, key, value) value = value.downcase if value.is_a? String params.push({ term: { key => value } }) end