Class: Searchkick::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Query.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/searchkick/query.rb', line 10

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"

  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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/searchkick/query.rb', line 52

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
      status_code = e.message[1..3].to_i
      if status_code == 404
        raise MissingIndexError, "Index missing - run #{searchkick_klass.name}.reindex"
      elsif status_code == 500 && (
          e.message.include?("IllegalArgumentException[minimumSimilarity >= 1]") ||
          e.message.include?("No query registered for [multi_match]") ||
          e.message.include?("[match] query does not support [cutoff_frequency]]") ||
          e.message.include?("No query registered for [function_score]]")
        )

        raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 1.0 or greater"
      elsif status_code == 400
        if e.message.include?("[multi_match] analyzer [searchkick_search] not found")
          raise InvalidQueryError, "Bad mapping - run #{searchkick_klass.name}.reindex"
        else
          raise InvalidQueryError, e.message
        end
      else
        raise e
      end
    end

    # 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 || []
    }
    Searchkick::Results.new(searchkick_klass, response, opts)
  end
end

#execute_search_with_instrumentationObject



5
6
7
8
9
10
11
12
13
# File 'lib/searchkick/logging.rb', line 5

def execute_search_with_instrumentation
  event = {
    name: "#{searchkick_klass.name} Search",
    query: params
  }
  ActiveSupport::Notifications.instrument("search.searchkick", event) do
    execute_search_without_instrumentation
  end
end

#paramsObject



42
43
44
45
46
47
48
49
50
# File 'lib/searchkick/query.rb', line 42

def params
  params = {
    index: options[:index_name] || searchkick_index.name,
    body: body
  }
  params.merge!(type: @type) if @type
  params.merge!(routing: @routing) if @routing
  params
end

#searchkick_indexObject



30
31
32
# File 'lib/searchkick/query.rb', line 30

def searchkick_index
  klass.searchkick_index
end

#searchkick_klassObject



38
39
40
# File 'lib/searchkick/query.rb', line 38

def searchkick_klass
  klass.searchkick_klass
end

#searchkick_optionsObject



34
35
36
# File 'lib/searchkick/query.rb', line 34

def searchkick_options
  klass.searchkick_options
end

#to_curlObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/searchkick/query.rb', line 106

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