Class: Elasticsearch::API::Response::ExplainResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/api/response/explain_response.rb

Overview

Parse Elasticsearch Explain API response json and display them in a neat way

Examples:

require 'elasticsearch'
client = Elasticsearch::Client.new
result = client.explain index: "megacorp", type: "employee", id: "1", q: "last_name:Smith"
Elasticsearch::API::Response::ExplainResponse.new(result["explanation"]).render_in_line
  #=> "1.0 = (1.0(termFreq=1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(explain, options = {}) ⇒ ExplainResponse

Returns a new instance of ExplainResponse.



47
48
49
50
51
52
53
54
# File 'lib/elasticsearch/api/response/explain_response.rb', line 47

def initialize(explain, options = {})
  @explain = explain || {}
  @indent = 0
  @trim = options.has_key?(:trim) ? options.delete(:trim) : true
  @rendering_options = options

  parse_details
end

Instance Attribute Details

#explainObject (readonly)

Returns the value of attribute explain.



45
46
47
# File 'lib/elasticsearch/api/response/explain_response.rb', line 45

def explain
  @explain
end

#rendering_optionsObject (readonly)

Returns the value of attribute rendering_options.



45
46
47
# File 'lib/elasticsearch/api/response/explain_response.rb', line 45

def rendering_options
  @rendering_options
end

#trimObject (readonly)

Returns the value of attribute trim.



45
46
47
# File 'lib/elasticsearch/api/response/explain_response.rb', line 45

def trim
  @trim
end

Class Method Details

.render(result, options = {}, &block) ⇒ Object

Show scoring with indents

Examples:

60.62 = 1.12 x 54.3 x 1.0(queryBoost)
  1.12 = 3.35 x 0.33(coord(4/12))
    3.35 = 0.2 + 0.93 + 1.29 + 0.93
  54.3 = 54.3 min 3.4028234999999995e+38(maxBoost)
    54.3 = 2.0 x 10.0 x 3.0 x 0.91


36
37
38
# File 'lib/elasticsearch/api/response/explain_response.rb', line 36

def render(result, options = {}, &block)
  new(result["explanation"], options).render(&block)
end

.render_in_line(result, options = {}, &block) ⇒ Object

Show scoring as a simple math formula

Examples:

"1.0 = (1.0(termFreq=1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)"


25
26
27
# File 'lib/elasticsearch/api/response/explain_response.rb', line 25

def render_in_line(result, options = {}, &block)
  new(result["explanation"], options).render_in_line(&block)
end

.result_as_hash(result, options = {}) ⇒ Object



40
41
42
# File 'lib/elasticsearch/api/response/explain_response.rb', line 40

def result_as_hash(result, options = {})
  new(result["explanation"], options).render_as_hash
end

Instance Method Details

#render(&block) ⇒ Object



56
57
58
# File 'lib/elasticsearch/api/response/explain_response.rb', line 56

def render(&block)
  @root.render(rendering_options, &block)
end

#render_as_hash(&block) ⇒ Object



64
65
66
# File 'lib/elasticsearch/api/response/explain_response.rb', line 64

def render_as_hash(&block)
  @root.render_as_hash(rendering_options, &block)
end

#render_in_line(&block) ⇒ Object



60
61
62
# File 'lib/elasticsearch/api/response/explain_response.rb', line 60

def render_in_line(&block)
  @root.render_in_line(rendering_options, &block)
end