class ElasticSearch::QueryBuilder
Constants
- METHODS
- VERSION
Attributes
client[RW]
function_score[RW]
mopts[RW]
options[RW]
opts[RW]
Public Class Methods
new(opts: {}, mopts: [], client: nil, function_score: false, options: {})
click to toggle source
# File lib/elastic_search/query_builder.rb, line 36 def initialize(opts: {}, mopts: [], client: nil, function_score: false, options: {}) @opts = opts @mopts = mopts @function_score = function_score @client = client @options = options end
Public Instance Methods
add_to_multisearch(index: {})
click to toggle source
# File lib/elastic_search/query_builder.rb, line 60 def add_to_multisearch(index: {}) mopts << index mopts << opts.compact @opts = {} end
multisearch_results()
click to toggle source
# File lib/elastic_search/query_builder.rb, line 54 def multisearch_results raise 'client: should be set in order to fetch multisearch_results' unless client client&.msearch(body: mopts)&.dig('responses') end
results()
click to toggle source
# File lib/elastic_search/query_builder.rb, line 48 def results raise 'client: should be set in order to fetch results' unless client client&.search(opts.compact)&.results end
to_json(*_args)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 44 def to_json(*_args) @opts.to_h end
Private Instance Methods
add_clause(path, body)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 106 def add_clause(path, body) return if added?(path, body) body = body.compact if body.respond_to?(:compact) if !root_path?(path) && body.is_a?(Array) existing_content = opts.dig(*path) || [] opts.dig(*path.first(path.size - 1)).store(path.last, body + existing_content) elsif root_path?(path) opts[path.first] = body else path_minus_one = path.first(path.size - 1) opts.dig(*path_minus_one).store(path.last, body) end end
added?(path, body)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 99 def added?(path, body) return if root_path?(path) || !exclusive_path?(path) clause = opts.dig(*path) clause.any? { |item| item == body.first } if clause.is_a?(Array) end
exclude_opposite(path, body)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 89 def exclude_opposite(path, body) return if root_path?(path) || !exclusive_path?(path) context = path.last opposite = context == :must ? :must_not : :must path_minus_one = path.first(path.size - 1) opposite_array = opts.dig(*(path_minus_one + [opposite])) opts.dig(*path_minus_one).store(opposite, opposite_array.reject { |item| item == body.first }) if opposite_array end
exclusive_path?(path)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 85 def exclusive_path?(path) %i[must must_not].any? { |item| path.last == item } end
init_path(path)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 70 def init_path(path) return if path.size == 1 || initialized?(path) path_minus_one = path.first(path.size - 1) @opts = opts.merge((path_minus_one + [{}]).reverse.reduce { |a, b| { b => a } }) end
initialized?(path)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 77 def initialized?(path) opts.dig(*path.first(path.size - 1)).present? end
root_path?(path)
click to toggle source
# File lib/elastic_search/query_builder.rb, line 81 def root_path?(path) path.size == 1 end