This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 533
/
facets_test.rb
232 lines (184 loc) · 7.45 KB
/
facets_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'test_helper'
require 'date'
module Tire
class FacetsIntegrationTest < Test::Unit::TestCase
include Test::Integration
context "Facets" do
should "return results scoped to current query" do
q = 'tags:ruby'
s = Tire.search('articles-test') do
query { string q }
facet 'tags' do
terms :tags
end
end
facets = s.results.facets['tags']['terms']
assert_equal 2, facets.count
assert_equal 'ruby', facets.first['term']
assert_equal 2, facets.first['count']
end
should "allow to specify global facets and query-scoped facets" do
q = 'tags:ruby'
s = Tire.search('articles-test') do
query { string q }
facet('scoped-tags') { terms :tags }
facet('global-tags', :global => true) { terms :tags }
end
scoped_facets = s.results.facets['scoped-tags']['terms']
global_facets = s.results.facets['global-tags']['terms']
assert_equal 2, scoped_facets.count
assert_equal 5, global_facets.count
end
should "allow to define multiple facets" do
s = Tire.search('articles-test') do
facet('tags') { terms :tags }
facet('date') { date :published_on }
end
assert_equal 2, s.results.facets.size
end
should "allow to restrict facets with filters" do
s = Tire.search('articles-test') do
query { string 'tags:ruby' }
facet('tags', :facet_filter => { :range => { :published_on => { :from => '2011-01-01', :to => '2011-01-01' } } }) do
terms :tags
end
end
assert_equal 1, s.results.facets.size
assert_equal 'ruby', s.results.facets['tags']['terms'].first['term']
assert_equal 1, s.results.facets['tags']['terms'].first['count'].to_i
end
context "date histogram" do
should "return aggregated values for all results" do
s = Tire.search('articles-test') do
query { all }
facet 'published_on' do
date :published_on
end
end
facets = s.results.facets['published_on']['entries']
assert_equal 4, facets.size, facets.inspect
assert_equal 2, facets.entries[1]["count"], facets.inspect
end
end
context "date ranges" do
should "return aggregated values for all results" do
s = Tire.search('articles-test') do
query { all }
facet 'published_on' do
range :published_on, [{:to => '2010-12-31'}, {:from => '2011-01-01', :to => '2011-01-05'}]
end
end
facets = s.results.facets['published_on']['ranges']
assert_equal 2, facets.size, facets.inspect
assert_equal 0, facets.entries[0]["count"], facets.inspect
assert_equal 5, facets.entries[1]["count"], facets.inspect
end
end
context "histogram" do
should "return aggregated values for all results" do
s = Tire.search('articles-test') do
query { all }
facet 'words_histogram' do
histogram :words, :interval => 100
end
end
facets = s.results.facets['words_histogram']['entries']
assert_equal 3, facets.size, facets.inspect
assert_equal({"key" => 100, "count" => 2}, facets.entries[0], facets.inspect)
assert_equal({"key" => 200, "count" => 2}, facets.entries[1], facets.inspect)
assert_equal({"key" => 300, "count" => 1}, facets.entries[2], facets.inspect)
end
end
context "statistical" do
should "return computed statistical data on a numeric field" do
s = Tire.search('articles-test') do
query { all }
facet 'word_stats' do
statistical :words
end
end
facets = s.results.facets["word_stats"]
assert_equal 5, facets["count"], facets.inspect
assert_equal 1125.0, facets["total"], facets.inspect
assert_equal 125.0, facets["min"], facets.inspect
assert_equal 375.0, facets["max"], facets.inspect
assert_equal 225.0, facets["mean"], facets.inspect
end
should "return computed statistical data by given script" do
s = Tire.search('articles-test') do
query { all }
facet 'word_stats' do
statistical :statistical => { :script => "doc['words'].value * factor",
:params => { :factor => 2 } }
end
end
facets = s.results.facets["word_stats"]
assert_equal 2250.0, facets["total"], facets.inspect
end
end
context "terms_stats" do
should "return computed stats computed on a field, per term value driven by another field" do
s = Tire.search('articles-test') do
query { all }
facet 'words_per_tag_stats' do
terms_stats :tags, :words
end
end
facets = s.results.facets['words_per_tag_stats']['terms']
assert_equal({"term" => "ruby", "count" => 2, "total_count"=> 2, "min"=> 125.0, "max"=> 250.0, "total"=> 375.0, "mean"=> 187.5}, facets[0], facets.inspect)
assert_equal({"term" => "java", "count" => 2, "total_count"=> 2, "min"=> 125.0, "max"=> 375.0, "total"=> 500.0, "mean"=> 250.0}, facets[1], facets.inspect)
assert_equal({"term" => "python", "count" => 1, "total_count"=> 1, "min"=> 250.0, "max"=> 250.0, "total"=> 250.0, "mean"=> 250.0}, facets[2], facets.inspect)
end
end
context "query facets" do
should "return aggregated values for a string query" do
s = Tire.search('articles-test') do
facet 'tees' do
query { string 'T*' }
end
end
count = s.results.facets['tees']['count']
assert_equal 2, count, s.results.facets['tees'].inspect
end
should "return aggregated values for _exists_ string query" do
s = Tire.search('articles-test') do
facet 'drafts' do
query { string '_exists_:draft' }
end
end
count = s.results.facets['drafts']['count']
assert_equal 1, count, s.results.facets['drafts'].inspect
end
should "return aggregated values for a terms query" do
s = Tire.search('articles-test') do
facet 'friends' do
query { terms :tags, ['ruby', 'python'] }
end
end
count = s.results.facets['friends']['count']
assert_equal 2, count, s.results.facets['friends'].inspect
s = Tire.search('articles-test') do
facet 'friends' do
query { terms :tags, ['ruby', 'python'], :minimum_match => 2 }
end
end
count = s.results.facets['friends']['count']
assert_equal 1, count, s.results.facets['friends'].inspect
end
end
context "filter" do
should "return a filtered facet" do
s = Tire.search('articles-test') do
query { all }
facet 'filtered' do
filter :tags, 'ruby'
end
end
assert_equal 5, s.results.size, s.results.inspect
facets = s.results.facets["filtered"]
assert_equal 2, facets["count"], facets.inspect
end
end
end
end
end