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, max: nil, colorize: true) ⇒ ExplainResponse

Returns a new instance of ExplainResponse.



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

def initialize(explain, max: nil, colorize: true)
  @explain = explain
  @indent = 0
  @renderer = ExplainRenderer.new(max: max, colorize: colorize)
end

Instance Attribute Details

#explainObject (readonly)

Returns the value of attribute explain.



38
39
40
# File 'lib/elasticsearch/api/response/explain_response.rb', line 38

def explain
  @explain
end

Class Method Details

.render(result, max: nil) ⇒ 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


33
34
35
# File 'lib/elasticsearch/api/response/explain_response.rb', line 33

def render(result, max: nil)
  new(result["explanation"], max: max).render
end

.render_in_line(result, max: nil) ⇒ 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)"


22
23
24
# File 'lib/elasticsearch/api/response/explain_response.rb', line 22

def render_in_line(result, max: nil)
  new(result["explanation"], max: max).render_in_line
end

Instance Method Details

#renderObject



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

def render
  parse_details
  @renderer.render(@root)
end

#render_in_lineObject



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

def render_in_line
  parse_details
  @renderer.render_in_line(@root)
end