elasticsearch-explain-response 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 781deba8c0a09a57fbf21df20918be1f577bfb7c
4
- data.tar.gz: 808cd8bd921f1a514cafd1a47457a0575bce6a74
3
+ metadata.gz: 8ea1238650fd2756ec6248ac4b08de91c29dd353
4
+ data.tar.gz: 413adb252c2a14172f19429ab32ec8813c1ec920
5
5
  SHA512:
6
- metadata.gz: 5af238a2068fa0aa82b168b830726674c44f0447c406220907c785f98752c23b11e68dfd6e4032859cf4bcac0683157dcdd2a8610db79f2c6d0764dd75898c8e
7
- data.tar.gz: df4a958713b444e87804f7a7d40a1bb09814e00f562d4a0f14f948bcd0bfe76fc432aa3b2e42a7263f64eb5ff50e5687a0235dcf7071a956c3daf2b628968be2
6
+ metadata.gz: 720dfd516b2d11b1f74121106a17f159e72018032dc19539651e3d83e829fd1aaf6870f4cd9766aa704672e4ecf6146fed96cd7bc017909b3b37bb41ace3cdb8
7
+ data.tar.gz: a4f2fcbf13b033977a2bd731700c658fb641baee8e2943807f6439e8ac25708f424ee5c3c62180dfd0bfd5125886cf0cb67b7a9ce9fc88b4a519eacc709d1029
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "elasticsearch-explain-response"
7
- spec.version = "0.2.0"
7
+ spec.version = "0.2.1"
8
8
  spec.authors = ["Tomoya Hirano"]
9
9
  spec.email = ["hiranotomoya@gmail.com"]
10
10
  spec.summary = %q{Parser for Elasticserach Explain response}
@@ -1,7 +1,10 @@
1
+ require "elasticsearch/api/response/renderable"
2
+
1
3
  module Elasticsearch
2
4
  module API
3
5
  module Response
4
6
  class ExplainNode
7
+ include Renderable
5
8
  extend Forwardable
6
9
 
7
10
  attr_reader :score, :description, :details, :level
@@ -22,8 +22,8 @@ module Elasticsearch
22
22
  # Show scoring as a simple math formula
23
23
  # @example
24
24
  # "1.0 = (1.0(termFreq=1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)"
25
- def render_in_line(result, options = {})
26
- new(result["explanation"], options).render_in_line
25
+ def render_in_line(result, options = {}, &block)
26
+ new(result["explanation"], options).render_in_line(&block)
27
27
  end
28
28
 
29
29
  # Show scoring with indents
@@ -33,8 +33,8 @@ module Elasticsearch
33
33
  # 3.35 = 0.2 + 0.93 + 1.29 + 0.93
34
34
  # 54.3 = 54.3 min 3.4028234999999995e+38(maxBoost)
35
35
  # 54.3 = 2.0 x 10.0 x 3.0 x 0.91
36
- def render(result, options = {})
37
- new(result["explanation"], options).render
36
+ def render(result, options = {}, &block)
37
+ new(result["explanation"], options).render(&block)
38
38
  end
39
39
 
40
40
  def result_as_hash(result, options = {})
@@ -53,16 +53,16 @@ module Elasticsearch
53
53
  parse_details
54
54
  end
55
55
 
56
- def render
57
- Renderers::StandardRenderer.new({ colorize: true }.merge(rendering_options)).render(@root)
56
+ def render(&block)
57
+ @root.render(rendering_options, &block)
58
58
  end
59
59
 
60
- def render_in_line
61
- Renderers::InlineRenderer.new({ colorize: true }.merge(rendering_options)).render(@root)
60
+ def render_in_line(&block)
61
+ @root.render_in_line(rendering_options, &block)
62
62
  end
63
63
 
64
- def render_as_hash
65
- Renderers::HashRenderer.new.render(@root)
64
+ def render_as_hash(&block)
65
+ @root.render_as_hash(rendering_options, &block)
66
66
  end
67
67
 
68
68
  private
@@ -0,0 +1,22 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Response
4
+ module Renderable
5
+ def render(rendering_options = {})
6
+ tree = block_given? ? yield(self) : self
7
+ Renderers::StandardRenderer.new({ colorize: true }.merge(rendering_options)).render(tree)
8
+ end
9
+
10
+ def render_in_line(rendering_options = {})
11
+ tree = block_given? ? yield(self) : self
12
+ Renderers::InlineRenderer.new({ colorize: true }.merge(rendering_options)).render(tree)
13
+ end
14
+
15
+ def render_as_hash(rendering_options = {})
16
+ tree = block_given? ? yield(self) : self
17
+ Renderers::HashRenderer.new.render(tree)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -14,6 +14,19 @@ describe Elasticsearch::API::Response::ExplainResponse do
14
14
  it "returns summary" do
15
15
  expect(subject).not_to be_empty
16
16
  end
17
+
18
+ context "with block" do
19
+ subject do
20
+ described_class.render_in_line(fake_response, colorize: false) do |tree|
21
+ tree.children = tree.children[0..1]
22
+ tree
23
+ end
24
+ end
25
+
26
+ it "changes the tree before rendering" do
27
+ expect(subject).to eq("0.05 = (0.11(score) x 0.5(coord(1/2)))")
28
+ end
29
+ end
17
30
  end
18
31
 
19
32
  describe '.render' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-explain-response
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomoya Hirano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-17 00:00:00.000000000 Z
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -112,11 +112,11 @@ files:
112
112
  - lib/elasticsearch/api/response/description.rb
113
113
  - lib/elasticsearch/api/response/explain_node.rb
114
114
  - lib/elasticsearch/api/response/explain_parser.rb
115
- - lib/elasticsearch/api/response/explain_renderer.rb
116
115
  - lib/elasticsearch/api/response/explain_response.rb
117
116
  - lib/elasticsearch/api/response/explain_trimmer.rb
118
117
  - lib/elasticsearch/api/response/helpers/color_helper.rb
119
118
  - lib/elasticsearch/api/response/helpers/string_helper.rb
119
+ - lib/elasticsearch/api/response/renderable.rb
120
120
  - lib/elasticsearch/api/response/renderers/base_renderer.rb
121
121
  - lib/elasticsearch/api/response/renderers/hash_renderer.rb
122
122
  - lib/elasticsearch/api/response/renderers/inline_renderer.rb
@@ -1,50 +0,0 @@
1
- require "elasticsearch/api/response/color_helper"
2
-
3
- module Elasticsearch
4
- module API
5
- module Response
6
- class ExplainRenderer
7
- include ColorHelper
8
-
9
- def initialize(options = {})
10
- disable_colorization if options[:colorize] == false
11
- @max = options[:max] || 3
12
- @plain_score = options[:plain_score] == true
13
- @show_values = options[:show_values] == true
14
- end
15
-
16
- def render(tree)
17
- @buffer = []
18
- recursive_render(tree)
19
- @buffer.join("\n")
20
- end
21
-
22
- def render_in_line(tree)
23
- [render_score(tree.score), "=", recursive_render_details(tree)].flatten.join(" ")
24
- end
25
-
26
- def recursive_render(node)
27
- return if node.level > @max
28
- render_result(node) if node.details.any?
29
- node.children.each do |child|
30
- recursive_render(child)
31
- end
32
- end
33
-
34
- private
35
-
36
- def render_result(node)
37
- @buffer << " " * node.level * 2 + [render_score(node.score), "=", render_details(node)].flatten.join(" ")
38
- end
39
-
40
- def render_details(node)
41
- if node.has_children?
42
- node.children.map(&method(:render_node)).compact.join(" #{node.operator} ")
43
- else
44
- render_node(node)
45
- end
46
- end
47
- end
48
- end
49
- end
50
- end