Class: Searchkick::Query
- Inherits:
-
Object
- Object
- Searchkick::Query
- Extended by:
- Forwardable
- Defined in:
- lib/searchkick/query.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(klass, term, options = {}) ⇒ Query
constructor
A new instance of Query.
- #params ⇒ Object
- #searchkick_index ⇒ Object
- #searchkick_klass ⇒ Object
- #searchkick_options ⇒ Object
- #to_curl ⇒ Object
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, = {}) if term.is_a?(Hash) = term term = "*" else term = term.to_s end if [:emoji] term = EmojiParser.parse_unicode(term) { |e| " #{e.name} " }.strip end @klass = klass @term = term @options = @match_suffix = [:match] || [:match] || "analyzed" prepare end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
6 7 8 |
# File 'lib/searchkick/query.rb', line 6 def body @body end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/searchkick/query.rb', line 5 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/searchkick/query.rb', line 5 def @options end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
5 6 7 |
# File 'lib/searchkick/query.rb', line 5 def term @term end |
Instance Method Details
#execute ⇒ Object
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 105 106 107 108 109 110 111 112 113 |
# File 'lib/searchkick/query.rb', line 61 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.[1..3].to_i if status_code == 404 raise MissingIndexError, "Index missing - run #{reindex_command}" elsif status_code == 500 && ( e..include?("IllegalArgumentException[minimumSimilarity >= 1]") || e..include?("No query registered for [multi_match]") || e..include?("[match] query does not support [cutoff_frequency]]") || e..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..include?("[multi_match] analyzer [searchkick_search] not found") raise InvalidQueryError, "Bad mapping - run #{reindex_command}" else raise InvalidQueryError, e. 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: [:include] || [:includes], json: ![:json].nil?, match_suffix: @match_suffix, highlighted_fields: @highlighted_fields || [] } Searchkick::Results.new(searchkick_klass, response, opts) end end |
#params ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/searchkick/query.rb', line 42 def params index = if [:index_name] Array([: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.merge!(type: @type) if @type params.merge!(routing: @routing) if @routing params end |
#searchkick_index ⇒ Object
30 31 32 |
# File 'lib/searchkick/query.rb', line 30 def searchkick_index klass ? klass.searchkick_index : nil end |
#searchkick_klass ⇒ Object
38 39 40 |
# File 'lib/searchkick/query.rb', line 38 def searchkick_klass klass ? klass.searchkick_klass : nil end |
#searchkick_options ⇒ Object
34 35 36 |
# File 'lib/searchkick/query.rb', line 34 def klass ? klass. : {} end |
#to_curl ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/searchkick/query.rb', line 115 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 |