elasticsearch-explain-response 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2299eb0a20900ac58c72b1bd5dd60bb2b0c3861
4
+ data.tar.gz: 0f969d270256226f68544794baa05dbce0c2c805
5
+ SHA512:
6
+ metadata.gz: b0ee27611287865e4013b8d2ee43b2056325377aa2af26f1b2884a84522af89ba9b6a8bfd04289109d1155216fc22bf008f4869cd33f503cc3193b9af1c552c2
7
+ data.tar.gz: 6852990a83ecf7235c985c767a4d36f83bde12915b1de05a21827a5a73b73c74965950575300228c9d6df26783ccfe0611519d7bb068b004c590704d9d0b8f03
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elasticsearch-explain-viewer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tomoya Hirano
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Elasticsearch-explain-response
2
+
3
+ Parse Elasticsearch Explain Response and summarize it in a simple way.
4
+ This gem is intended for developers working with Elasticsearch to debug search scores
5
+ in a easier way.
6
+ The current feature is very simple, but any sugguestions or feature requests are welcomed.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'elasticsearch-explain-response'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install elasticsearch-explain-response
23
+
24
+ ## Usage
25
+
26
+ ### Summarize the explanation in one line
27
+
28
+ ```
29
+ require 'elasticsearch'
30
+ client = Elasticsearch::Client.new
31
+ result = client.explain index: "megacorp", type: "employee", id: "1", q: "last_name:Smith"
32
+ puts Elasticsearch::API::Response::ExplainResponse.new(result["explanation"]).render_in_line
33
+ #=>
34
+ 1.0 = (1.0(termFreq=1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)
35
+ ```
36
+
37
+ ### Summarize the explanation in lines
38
+
39
+ ```
40
+ require 'elasticsearch'
41
+ client = Elasticsearch::Client.new
42
+ result = client.explain index: "megacorp", type: "employee", id: "1", q: "last_name:Smith"
43
+ puts Elasticsearch::API::Response::ExplainResponse.new(result["explanation"]).render
44
+ #=>
45
+ 1.0 = 1.0(fieldWeight)
46
+ 1.0 = 1.0(tf(1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)
47
+ 1.0 = 1.0(termFreq=1.0)
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/[my-github-username]/elasticsearch-explain-response/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "elasticsearch-explain-response"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Tomoya Hirano"]
9
+ spec.email = ["hiranotomoya@gmail.com"]
10
+ spec.summary = %q{Parser for Elasticserach Explain response}
11
+ spec.description = %q{Parser for Elasticserach Explain response}
12
+ spec.homepage = "http://github.com/tomoya55/elasticsearch-explain-response"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "elasticsearch", "~> 1.0.0"
24
+ spec.add_development_dependency "ansi", "~> 1.5.0"
25
+ end
@@ -0,0 +1 @@
1
+ require "elasticsearch/api/response/explain_response"
@@ -0,0 +1,34 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Response
4
+ module ColorHelper
5
+ def colorized?
6
+ unless @ansi_loaded
7
+ @colorized = load_ansi
8
+ else
9
+ !!@colorized
10
+ end
11
+ end
12
+
13
+ def diable_colorization
14
+ @ansi_loaded = true
15
+ @colorized = false
16
+ end
17
+
18
+ def load_ansi
19
+ require "ansi/core"
20
+ rescue LoadError
21
+ false
22
+ end
23
+
24
+ def ansi(str, *codes)
25
+ if colorized?
26
+ str.to_s.ansi(*codes)
27
+ else
28
+ str.to_s
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,179 @@
1
+ require "elasticsearch/api/response/color_helper"
2
+
3
+ module Elasticsearch
4
+ module API
5
+ module Response
6
+ # Parse Elasticsearch Explain API response json and display them in a neat way
7
+ #
8
+ # @example
9
+ # require 'elasticsearch'
10
+ # client = Elasticsearch::Client.new
11
+ # result = client.explain index: "megacorp", type: "employee", id: "1", q: "last_name:Smith"
12
+ # Elasticsearch::API::Response::ExplainResponse.new(result["explanation"]).render_in_line
13
+ # #=> "1.0 = (1.0(termFreq=1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)"
14
+ class ExplainResponse
15
+ include ColorHelper
16
+
17
+ class << self
18
+ # Show scoring as a simple math formula
19
+ # @example
20
+ # "1.0 = (1.0(termFreq=1.0)) x 1.0(idf(2/3)) x 1.0(fieldNorm)"
21
+ def render_in_line(result, max: nil)
22
+ new(result["explanation"], max: max).render_in_line
23
+ end
24
+
25
+ # Show scoring with indents
26
+ # @example
27
+ # 60.62 = 1.12 x 54.3 x 1.0(queryBoost)
28
+ # 1.12 = 3.35 x 0.33(coord(4/12))
29
+ # 3.35 = 0.2 + 0.93 + 1.29 + 0.93
30
+ # 54.3 = 54.3 min 3.4028234999999995e+38(maxBoost)
31
+ # 54.3 = 2.0 x 10.0 x 3.0 x 0.91
32
+ def render(result, max: nil)
33
+ parser = new(result["explanation"], max: max)
34
+ parser.parse
35
+ parser.render
36
+ end
37
+ end
38
+
39
+ attr_reader :explain
40
+
41
+ def initialize(explain, max: nil)
42
+ @explain = explain
43
+ @indent = 0
44
+ @max = max || 3
45
+ end
46
+
47
+ def render
48
+ @buffer = []
49
+ parse_explain(explain, indent: @indent, max: @max)
50
+ @buffer.map do |buf|
51
+ render_result(buf[:score], render_details(buf[:details], indent: @max + 1), indent: buf[:indent])
52
+ end
53
+ end
54
+
55
+ def render_in_line
56
+ score = explain["value"]
57
+ details = parse_for_oneline(explain)
58
+ render_result(score, render_details(details, indent: 0))
59
+ end
60
+
61
+ private
62
+
63
+ def get_score_type(description)
64
+ case
65
+ when description.include?("product of")
66
+ "x"
67
+ when description.include?("[multiply]")
68
+ "x"
69
+ when description.include?("sum of")
70
+ "+"
71
+ when description.include?("Math.min of")
72
+ "min"
73
+ else
74
+ " "
75
+ end
76
+ end
77
+
78
+ def extract_description(description)
79
+ case description
80
+ when /\Aweight\((\w+)\:(\w+)\s+in\s+\d+\)\s+\[\w+\]\,\s+result\s+of\:\z/
81
+ [field($1), value($2)].join(':')
82
+ when /\Aidf\(docFreq\=(\d+)\,\s+maxDocs\=(\d+)\)\z/
83
+ "idf(#{$1}/#{$2})"
84
+ when /\Atf\(freq\=([\d.]+)\)\,\swith\sfreq\sof\:\z/
85
+ "tf(#{$1})"
86
+ when /\Ascore\(doc\=\d+\,freq=[\d\.]+\)\,\sproduct\sof\:\z/
87
+ "score"
88
+ when /\Amatch\sfilter\:\s(?:cache\()?(?:\w+\()?([\w\*]+)\:([\w\*]+)\)*\z/
89
+ "match(#{field($1)}:#{value($2)})"
90
+ when /\AFunction\sfor\sfield\s([\w\_]+)\:\z/
91
+ "func(#{field($1)})"
92
+ when /\A(queryWeight|fieldWeight|fieldNorm)/
93
+ $1
94
+ when /\Afunction\sscore/
95
+ nil
96
+ when "static boost factor"
97
+ "boost"
98
+ when "Math.min of", "sum of:", "product of:"
99
+ nil
100
+ else
101
+ description
102
+ end
103
+ end
104
+
105
+ def field(str)
106
+ ansi(str, :blue ,:bright)
107
+ end
108
+
109
+ def value(str)
110
+ ansi(str, :green)
111
+ end
112
+
113
+ def render_details(details, indent:)
114
+ details[:details].map do |de|
115
+ case de
116
+ when Hash
117
+ if indent < @max
118
+ detail = render_details(de, indent: indent.succ)
119
+ wrap_paren(detail)
120
+ else
121
+ de[:description]
122
+ end
123
+ else
124
+ de
125
+ end
126
+ end.join(" #{details[:symbol]} ")
127
+ end
128
+
129
+ def parse_for_oneline(explain)
130
+ symbol = get_score_type(explain["description"])
131
+ description = parse_description(explain)
132
+ details = explain["details"].map do |de|
133
+ if de["details"]
134
+ parse_for_oneline(de)
135
+ else
136
+ parse_description(de)
137
+ end
138
+ end
139
+ {details: details, symbol: symbol, description: description}
140
+ end
141
+
142
+ def parse_description(detail)
143
+ text = render_score(detail["value"])
144
+ description = extract_description(detail["description"])
145
+ text += "(#{description})" if description
146
+ text
147
+ end
148
+
149
+ def parse_explain(explain, indent:, max: )
150
+ return if indent > max
151
+ score = explain["value"]
152
+ symbol = get_score_type(explain["description"])
153
+ details = explain["details"].map(&method(:parse_description))
154
+ @buffer << {score: score, details: {details: details, symbol: symbol}, indent: indent}
155
+
156
+ explain["details"].each do |de|
157
+ parse_explain(de, indent: indent.succ, max: max) if de["details"]
158
+ end
159
+ end
160
+
161
+ def render_score(score)
162
+ ansi(score.round(2).to_s, :magenta, :bright)
163
+ end
164
+
165
+ def render_result(score, details, indent: 0)
166
+ " " * indent * 2 + [render_score(score), "=", details].flatten.join(" ")
167
+ end
168
+
169
+ def wrap_paren(string)
170
+ if string.start_with?("(") && string.end_with?(")")
171
+ string
172
+ else
173
+ "(" + string + ")"
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Elasticsearch::API::Response::ExplainResponse do
4
+ let(:fake_response) do
5
+ fixture_load(:response1)
6
+ end
7
+
8
+ describe "#render_in_line" do
9
+ let(:response) do
10
+ described_class.new(fake_response["explanation"])
11
+ end
12
+
13
+ subject do
14
+ response.render_in_line
15
+ end
16
+
17
+ before do
18
+ response.diable_colorization
19
+ end
20
+
21
+ it "returns summary of explain in line" do
22
+ expect(subject).to eq("0.05 = (0.43(queryWeight) x 0.25(fieldWeight)) x 0.5(coord(1/2))")
23
+ end
24
+ end
25
+
26
+ describe "#render" do
27
+ let(:response) do
28
+ described_class.new(fake_response["explanation"])
29
+ end
30
+
31
+ subject do
32
+ response.render
33
+ end
34
+
35
+ before do
36
+ response.diable_colorization
37
+ end
38
+
39
+ it "returns summary of explain in lines" do
40
+ expect(subject).to eq [
41
+ "0.05 = 0.11 x 0.5(coord(1/2))",
42
+ " 0.11 = 0.11(_all:smith)",
43
+ " 0.11 = 0.11(score)",
44
+ " 0.11 = 0.43(queryWeight) x 0.25(fieldWeight)"
45
+ ]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,39 @@
1
+ ---
2
+ _index: "megacorp"
3
+ _type: "employee"
4
+ _id: "1"
5
+ matched: true
6
+ explanation:
7
+ value: 0.053770658
8
+ description: "product of:"
9
+ details:
10
+ - value: 0.107541315
11
+ description: "sum of:"
12
+ details:
13
+ - value: 0.107541315
14
+ description: "weight(_all:smith in 0) [PerFieldSimilarity], result of:"
15
+ details:
16
+ - value: 0.107541315
17
+ description: "score(doc=0,freq=1.0), product of:"
18
+ details:
19
+ - value: 0.43016526
20
+ description: "queryWeight, product of:"
21
+ details:
22
+ - value: 1.0
23
+ description: "idf(docFreq=2, maxDocs=3)"
24
+ - value: 0.43016526
25
+ description: "queryNorm"
26
+ - value: 0.25
27
+ description: "fieldWeight in 0, product of:"
28
+ details:
29
+ - value: 1.0
30
+ description: "tf(freq=1.0), with freq of:"
31
+ details:
32
+ - value: 1.0
33
+ description: "termFreq=1.0"
34
+ - value: 1.0
35
+ description: "idf(docFreq=2, maxDocs=3)"
36
+ - value: 0.25
37
+ description: "fieldNorm(doc=0)"
38
+ - value: 0.5
39
+ description: "coord(1/2)"
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require "elasticsearch-explain-response"
3
+
4
+ Dir[ File.expand_path('../support/**/*.rb', __FILE__) ].each { |f| require f }
5
+
6
+ RSpec.configure do |config|
7
+ config.include FixtureHelper
8
+ end
@@ -0,0 +1,11 @@
1
+ require "yaml"
2
+
3
+ module FixtureHelper
4
+ def fixture_load(name)
5
+ YAML.load_file(fixture_file_path(name))
6
+ end
7
+
8
+ def fixture_file_path(name)
9
+ File.expand_path("../../fixtures/#{name}.yml", __FILE__)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticsearch-explain-response
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomoya Hirano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: elasticsearch
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: ansi
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.5.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.5.0
83
+ description: Parser for Elasticserach Explain response
84
+ email:
85
+ - hiranotomoya@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - elasticsearch-explain-response.gemspec
97
+ - lib/elasticsearch-explain-response.rb
98
+ - lib/elasticsearch/api/response/color_helper.rb
99
+ - lib/elasticsearch/api/response/explain_response.rb
100
+ - spec/elasticsearch/api/response/explain_response_spec.rb
101
+ - spec/fixtures/response1.yml
102
+ - spec/spec_helper.rb
103
+ - spec/support/fixture_helper.rb
104
+ homepage: http://github.com/tomoya55/elasticsearch-explain-response
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.5
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Parser for Elasticserach Explain response
128
+ test_files:
129
+ - spec/elasticsearch/api/response/explain_response_spec.rb
130
+ - spec/fixtures/response1.yml
131
+ - spec/spec_helper.rb
132
+ - spec/support/fixture_helper.rb