elasticsearch-explain-response 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df103068b5f7a7ecbb4aedc891170f4694de2306
4
- data.tar.gz: a12e87ba1be8f777d975797f0e7a48719696f555
3
+ metadata.gz: 207cf81ab8713811ce0491a545acd8a0784212b2
4
+ data.tar.gz: c940afb7c7e8a8e3345bd7b8b5dfa3de9eae1159
5
5
  SHA512:
6
- metadata.gz: 02147c8d3dfbcf889dc90daac3c2820e343dcf53421a095f2ff46e2eecc1b1040fb420f6d7b0df7b9643621c0c0d32569073d511766d27df6910224183daf143
7
- data.tar.gz: c77fb47e745a90012425c93c03abb1014926705c85ae954f4aa0bb8eb1b32517d991e68fa8c318175e550589647b21b3c746f1ac8f6ac37eaace8e9752425c7c
6
+ metadata.gz: 2d1f222854366f2583042dd7321b9384ef5d271845e8e802ba5eabba91d63327dbeac5bcc319b42729567b83ccf77cb325bd8415ffc5fe4193478a6ee3c0a3b5
7
+ data.tar.gz: 536938ce0c1df8bc814e7dae4814a218619cb00c884a0834ca52b0574c9ec5b2634d625c5ed4ed644e2ab19279210e81703e72017acd7af656d41559332f9986
@@ -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.3"
7
+ spec.version = "0.2.4"
8
8
  spec.authors = ["Tomoya Hirano"]
9
9
  spec.email = ["hiranotomoya@gmail.com"]
10
10
  spec.summary = %q{Parser for Elasticserach Explain response}
@@ -35,6 +35,10 @@ module Elasticsearch
35
35
  score == 1.0
36
36
  end
37
37
 
38
+ def score?
39
+ type == "score"
40
+ end
41
+
38
42
  def min?
39
43
  type == "min"
40
44
  end
@@ -43,6 +47,14 @@ module Elasticsearch
43
47
  type == "func"
44
48
  end
45
49
 
50
+ def product?
51
+ type == "product"
52
+ end
53
+
54
+ def constant?
55
+ type == "constant"
56
+ end
57
+
46
58
  def match?
47
59
  type == "match"
48
60
  end
@@ -92,6 +92,9 @@ module Elasticsearch
92
92
  when /\AConstantScore\(.+\), product of\:\z/
93
93
  type = "constant"
94
94
  operation = "constant"
95
+ when /\Aconstant score/
96
+ type = "constant"
97
+ operation = "constant"
95
98
  when "static boost factor", "boostFactor"
96
99
  type = "boost"
97
100
  operation = "boost"
@@ -110,6 +113,9 @@ module Elasticsearch
110
113
  operator = "+"
111
114
  when "maxBoost"
112
115
  type = "maxBoost"
116
+ when /_score\:\s*/
117
+ type = "score"
118
+ operation = "score"
113
119
  else
114
120
  type = description
115
121
  operation = description
@@ -13,6 +13,12 @@ module Elasticsearch
13
13
 
14
14
  def trim_node(node)
15
15
  case
16
+ when node.product?
17
+ trim_product_node(node)
18
+ when node.score?
19
+ trim_score_node(node)
20
+ when node.func?, node.boost?
21
+ trim_children_node(node)
16
22
  when node.func_score?
17
23
  trim_func_score_node(node)
18
24
  when node.min?
@@ -22,15 +28,51 @@ module Elasticsearch
22
28
  end
23
29
  end
24
30
 
31
+ def trim_score_node(node)
32
+ case node.children.size
33
+ when 1
34
+ return trim_node(node.children.first)
35
+ else
36
+ trim_default_node(node)
37
+ end
38
+ end
39
+
40
+ def trim_children_node(node)
41
+ case node.children.size
42
+ when 1
43
+ node.children = []
44
+ trim_node(node)
45
+ else
46
+ trim_default_node(node)
47
+ end
48
+ end
49
+
50
+ def trim_product_node(node)
51
+ case node.children.size
52
+ when 2
53
+ constant = node.children.find { |n| n.constant? }
54
+ if constant
55
+ other = (node.children - [constant])[0]
56
+ if constant.score_one? && other.score == node.score
57
+ return trim_node(other)
58
+ end
59
+ end
60
+ end
61
+
62
+ trim_default_node(node)
63
+ end
64
+
25
65
  def trim_func_score_node(node)
26
66
  case node.children.size
27
67
  when 2
28
- match = node.children.find(&:match?)
29
- if match
30
- other = (node.children - [match])[0]
31
- if match.score_one? && other.score == node.score
32
- entity = match.field == "*" ? other : match
33
- return merge_function_score_node(node, entity)
68
+ boost = node.children.find { |n| n.match? || n.query_boost? }
69
+ if boost
70
+ other = (node.children - [boost])[0]
71
+ if boost.score_one? && other.score == node.score
72
+ other = trim_node(other) if other.has_children?
73
+ new_node = merge_function_score_node(node, boost, other)
74
+ new_node.children = other.children
75
+ return trim_node(new_node)
34
76
  end
35
77
  end
36
78
  end
@@ -51,7 +93,9 @@ module Elasticsearch
51
93
  trim_node(child)
52
94
  end
53
95
 
54
- def merge_function_score_node(current, entity)
96
+ def merge_function_score_node(current, boost, target)
97
+ entity = boost.field == "*" || boost.field.nil? ? target : boost
98
+
55
99
  ExplainNode.new(
56
100
  score: current.score,
57
101
  level: current.level,
@@ -13,7 +13,7 @@ module Elasticsearch
13
13
  private
14
14
 
15
15
  def recursive_render_details(node)
16
- details = node.children.map do |child|
16
+ details = node.children.map { |child|
17
17
  if child.children.any? && child.level <= @max
18
18
  recursive_render_details(child)
19
19
  else
@@ -21,7 +21,7 @@ module Elasticsearch
21
21
  render_node(child)
22
22
  end
23
23
  end
24
- end.compact
24
+ }.compact
25
25
 
26
26
  if details.size > 1
27
27
  wrap_paren(details.join(" #{node.operator} "))
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require "pry"
3
2
 
4
3
  describe Elasticsearch::API::Response::ExplainResponse do
5
4
  let(:fake_response) do
@@ -66,7 +65,7 @@ describe Elasticsearch::API::Response::ExplainResponse do
66
65
  end
67
66
 
68
67
  it "returns summary of explain in line" do
69
- expect(subject).to eq("887.19 = (10.0(match(name:hawaii)) x 10.0(match(name:guam)) x 0.7(match(name:\"new caledonia\", new, nueva, caledonia)) x 3.0(match(with_beach:T)) x 0.99(func(updated_at)) x 3.0(match(region_id:[3 TO 3]))) x 1.0(queryBoost)")
68
+ expect(subject).to eq("887.19 = (10.0(match(name:hawaii)) x 10.0(match(name:guam)) x 0.7(match(name:\"new caledonia\", new, nueva, caledonia)) x 3.0(match(with_beach:T)) x 0.99(func(updated_at)) x 3.0(match(region_id:[3 TO 3])))")
70
69
  end
71
70
  end
72
71
  end
@@ -86,7 +85,7 @@ describe Elasticsearch::API::Response::ExplainResponse do
86
85
  end
87
86
 
88
87
  it "returns summary of explain in line" do
89
- expect(subject).to eq("887.19 = (10.0(match(name)) x 10.0(match(name)) x 0.7(match(name)) x 3.0(match(with_beach)) x 0.99(func(updated_at)) x 3.0(match(region_id))) x 1.0(queryBoost)")
88
+ expect(subject).to eq("887.19 = (10.0(match(name)) x 10.0(match(name)) x 0.7(match(name)) x 3.0(match(with_beach)) x 0.99(func(updated_at)) x 3.0(match(region_id)))")
90
89
  end
91
90
  end
92
91
  end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe Elasticsearch::API::Response::ExplainTrimmer do
4
+ describe "#trim" do
5
+ context "with function score node" do
6
+ let(:json) do
7
+ {"value"=>100.0,
8
+ "description"=>"function score, product of:",
9
+ "details"=>
10
+ [{"value"=>1.0,
11
+ "description"=> "match filter: QueryWrapperFilter(title:paris)"},
12
+ {"value"=>100.0,
13
+ "description"=>"product of:",
14
+ "details"=>[
15
+ {"value"=>1.0, "description"=>"constant score 1.0 - no function provided"},
16
+ {"value"=>100.0, "description"=>"weight"}
17
+ ]}
18
+ ]
19
+ }
20
+ end
21
+
22
+ it "trims function score node" do
23
+ tree = Elasticsearch::API::Response::ExplainParser.new.parse(json)
24
+ result = Elasticsearch::API::Response::ExplainTrimmer.new.trim(tree)
25
+ expect(result.render_as_hash).to eq(
26
+ score: 100.0,
27
+ type: "match",
28
+ operation: "match",
29
+ field: "title",
30
+ value: "paris"
31
+ )
32
+ end
33
+ end
34
+
35
+ context "with boost node" do
36
+ let(:json) do
37
+ { "value" => 10,
38
+ "description"=>"function score, product of:",
39
+ "details"=> [
40
+ { "value"=>1, "description"=>"match filter: QueryWrapperFilter(name.raw:smith)"},
41
+ { "value"=>10, "description"=>"static boost factor",
42
+ "details"=>[
43
+ {"value"=>10, "description"=>"boostFactor"}
44
+ ]
45
+ }
46
+ ]
47
+ }
48
+ end
49
+
50
+ it "trims boost node" do
51
+ tree = Elasticsearch::API::Response::ExplainParser.new.parse(json)
52
+ result = Elasticsearch::API::Response::ExplainTrimmer.new.trim(tree)
53
+ expect(result.render_as_hash).to eq(
54
+ score: 10,
55
+ type: "match",
56
+ operation: "match",
57
+ field: "name.raw",
58
+ value: "smith"
59
+ )
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require "elasticsearch-explain-response"
3
+ require "pry"
3
4
 
4
5
  Dir[ File.expand_path('../support/**/*.rb', __FILE__) ].each { |f| require f }
5
6
 
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.3
4
+ version: 0.2.4
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-18 00:00:00.000000000 Z
11
+ date: 2016-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,7 @@ files:
122
122
  - lib/elasticsearch/api/response/renderers/inline_renderer.rb
123
123
  - lib/elasticsearch/api/response/renderers/standard_renderer.rb
124
124
  - spec/elasticsearch/api/response/explain_response_spec.rb
125
+ - spec/elasticsearch/api/response/explain_trimmer_spec.rb
125
126
  - spec/fixtures/response1.yml
126
127
  - spec/fixtures/response2.yml
127
128
  - spec/spec_helper.rb
@@ -152,7 +153,9 @@ specification_version: 4
152
153
  summary: Parser for Elasticserach Explain response
153
154
  test_files:
154
155
  - spec/elasticsearch/api/response/explain_response_spec.rb
156
+ - spec/elasticsearch/api/response/explain_trimmer_spec.rb
155
157
  - spec/fixtures/response1.yml
156
158
  - spec/fixtures/response2.yml
157
159
  - spec/spec_helper.rb
158
160
  - spec/support/fixture_helper.rb
161
+ has_rdoc: