lib/searchkick/query.rb in searchkick-hooopo-2.3.3 vs lib/searchkick/query.rb in searchkick-hooopo-2.3.4
- old
+ new
@@ -14,11 +14,11 @@
:offset_value, :offset, :previous_page, :prev_page, :next_page, :first_page?, :last_page?,
:out_of_range?, :hits, :response, :to_a, :first
def initialize(klass, term = "*", **options)
unknown_keywords = options.keys - [:aggs, :body, :body_options, :boost,
- :boost_by, :boost_by_distance, :boost_where, :conversions, :debug, :emoji, :exclude, :execute, :explain,
+ :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain,
:fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load,
:match, :misspellings, :offset, :operator, :order, :padding, :page, :per_page, :profile,
:request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where]
raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any?
@@ -77,11 +77,11 @@
def execute
@execute ||= begin
begin
response = execute_search
- if @misspellings_below && response["hits"]["total"] < @misspellings_below
+ if retry_misspellings?(response)
prepare
response = execute_search
end
rescue => e # TODO rescue type
handle_error(e)
@@ -157,10 +157,14 @@
# set execute for multi search
@execute = Searchkick::Results.new(searchkick_klass, response, opts)
end
+ def retry_misspellings?(response)
+ @misspellings_below && response["hits"]["total"] < @misspellings_below
+ end
+
private
def handle_error(e)
status_code = e.message[1..3].to_i
if status_code == 404
@@ -379,11 +383,11 @@
query: {
function_score: {
boost_mode: "replace",
query: {
match: {
- "#{conversions_field}.query" => term
+ "#{conversions_field}.query" => options[:conversions_term] || term
}
}
}.merge(script_score)
}
}
@@ -445,11 +449,11 @@
# aggregations
set_aggregations(payload) if options[:aggs]
# suggestions
- set_suggestions(payload) if options[:suggest]
+ set_suggestions(payload, options[:suggest]) if options[:suggest]
# highlight
set_highlights(payload, fields) if options[:highlight]
# timeout shortly after client times out
@@ -487,27 +491,28 @@
@load = load
end
def set_fields
boost_fields = {}
- fields = options[:fields] || searchkick_options[:searchable]
+ fields = options[:fields] || searchkick_options[:default_fields] || searchkick_options[:searchable]
+ all = searchkick_options.key?(:_all) ? searchkick_options[:_all] : below60?
default_match = options[:match] || searchkick_options[:match] || :word
fields =
if fields
fields.map do |value|
k, v = value.is_a?(Hash) ? value.to_a.first : [value, default_match]
k2, boost = k.to_s.split("^", 2)
field = "#{k2}.#{v == :word ? 'analyzed' : v}"
boost_fields[field] = boost.to_f if boost
field
end
- elsif default_match == :word
+ elsif all && default_match == :word
["_all"]
- elsif default_match == :phrase
+ elsif all && default_match == :phrase
["_all.phrase"]
else
- raise ArgumentError, "Must specify fields"
+ raise ArgumentError, "Must specify fields to search"
end
[boost_fields, fields]
end
def set_boost_by_distance(custom_filters)
@@ -573,16 +578,22 @@
end
payload[:indices_boost] = indices_boost
end
- def set_suggestions(payload)
- suggest_fields = (searchkick_options[:suggest] || []).map(&:to_s)
+ def set_suggestions(payload, suggest)
+ suggest_fields = nil
- # intersection
- if options[:fields]
- suggest_fields &= options[:fields].map { |v| (v.is_a?(Hash) ? v.keys.first : v).to_s.split("^", 2).first }
+ if suggest.is_a?(Array)
+ suggest_fields = suggest
+ else
+ suggest_fields = (searchkick_options[:suggest] || []).map(&:to_s)
+
+ # intersection
+ if options[:fields]
+ suggest_fields &= options[:fields].map { |v| (v.is_a?(Hash) ? v.keys.first : v).to_s.split("^", 2).first }
+ end
end
if suggest_fields.any?
payload[:suggest] = {text: term}
suggest_fields.each do |field|
@@ -590,10 +601,12 @@
phrase: {
field: "#{field}.suggest"
}
}
end
+ else
+ raise ArgumentError, "Must pass fields to suggest option"
end
end
def set_highlights(payload, fields)
payload[:highlight] = {
@@ -820,11 +833,11 @@
def term_filters(field, value)
if value.is_a?(Array) # in query
if value.any?(&:nil?)
{bool: {should: [term_filters(field, nil), term_filters(field, value.compact)]}}
else
- {in: {field => value}}
+ {terms: {field => value}}
end
elsif value.nil?
{bool: {must_not: {exists: {field: field}}}}
elsif value.is_a?(Regexp)
{regexp: {field => {value: value.source}}}
@@ -894,8 +907,12 @@
end
end
def below50?
Searchkick.server_below?("5.0.0-alpha1")
+ end
+
+ def below60?
+ Searchkick.server_below?("6.0.0-alpha1")
end
end
end