Class: Searchkick::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/searchkick/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, term, options = {}) ⇒ Query

Returns a new instance of Query


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/searchkick/query.rb', line 15

def initialize(klass, term, options = {})
  if term.is_a?(Hash)
    options = term
    term = "*"
  else
    term = term.to_s
  end

  if options[:emoji]
    term = EmojiParser.parse_unicode(term) { |e| " #{e.name} " }.strip
  end

  @klass = klass
  @term = term
  @options = options
  @match_suffix = options[:match] || searchkick_options[:match] || "analyzed"

  # prevent Ruby warnings
  @type = nil
  @routing = nil
  @misspellings_below = nil
  @highlighted_fields = nil

  prepare
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body


6
7
8
# File 'lib/searchkick/query.rb', line 6

def body
  @body
end

#klassObject (readonly)

Returns the value of attribute klass


5
6
7
# File 'lib/searchkick/query.rb', line 5

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options


5
6
7
# File 'lib/searchkick/query.rb', line 5

def options
  @options
end

#termObject (readonly)

Returns the value of attribute term


5
6
7
# File 'lib/searchkick/query.rb', line 5

def term
  @term
end

Instance Method Details

#executeObject


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/searchkick/query.rb', line 73

def execute
  @execute ||= begin
    begin
      response = execute_search
      if @misspellings_below && response["hits"]["total"] < @misspellings_below
        prepare
        response = execute_search
      end
    rescue => e # TODO rescue type
      handle_error(e)
    end
    handle_response(response)
  end
end

#handle_response(response) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/searchkick/query.rb', line 99

def handle_response(response)
  # apply facet limit in client due to
  # https://github.com/elasticsearch/elasticsearch/issues/1305
  @facet_limits.each do |field, limit|
    field = field.to_s
    facet = response["facets"][field]
    response["facets"][field]["terms"] = facet["terms"].first(limit)
    response["facets"][field]["other"] = facet["total"] - facet["terms"].sum { |term| term["count"] }
  end

  opts = {
    page: @page,
    per_page: @per_page,
    padding: @padding,
    load: @load,
    includes: options[:include] || options[:includes],
    json: !options[:json].nil?,
    match_suffix: @match_suffix,
    highlighted_fields: @highlighted_fields || []
  }

  # set execute for multi search
  @execute = Searchkick::Results.new(searchkick_klass, response, opts)
end

#paramsObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/searchkick/query.rb', line 53

def params
  index =
    if options[:index_name]
      Array(options[:index_name]).map { |v| v.respond_to?(:searchkick_index) ? v.searchkick_index.name : v }.join(",")
    elsif searchkick_index
      searchkick_index.name
    else
      "_all"
    end

  params = {
    index: index,
    body: body
  }
  params[:type] = @type if @type
  params[:routing] = @routing if @routing
  params.merge!(options[:request_params]) if options[:request_params]
  params
end

#searchkick_indexObject


41
42
43
# File 'lib/searchkick/query.rb', line 41

def searchkick_index
  klass ? klass.searchkick_index : nil
end

#searchkick_klassObject


49
50
51
# File 'lib/searchkick/query.rb', line 49

def searchkick_klass
  klass ? klass.searchkick_klass : nil
end

#searchkick_optionsObject


45
46
47
# File 'lib/searchkick/query.rb', line 45

def searchkick_options
  klass ? klass.searchkick_options : {}
end

#to_curlObject


88
89
90
91
92
93
94
95
96
97
# File 'lib/searchkick/query.rb', line 88

def to_curl
  query = params
  type = query[:type]
  index = query[:index].is_a?(Array) ? query[:index].join(",") : query[:index]

  # no easy way to tell which host the client will use
  host = Searchkick.client.transport.hosts.first
  credentials = host[:user] || host[:password] ? "#{host[:user]}:#{host[:password]}@" : nil
  "curl #{host[:protocol]}://#{credentials}#{host[:host]}:#{host[:port]}/#{CGI.escape(index)}#{type ? "/#{type.map { |t| CGI.escape(t) }.join(',')}" : ''}/_search?pretty -d '#{query[:body].to_json}'"
end