nxa-sunspot 0.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/History.txt +153 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +191 -0
  4. data/Rakefile +9 -0
  5. data/TODO +14 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +40 -0
  8. data/bin/sunspot-solr +95 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot.rb +488 -0
  11. data/lib/sunspot/adapters.rb +265 -0
  12. data/lib/sunspot/composite_setup.rb +188 -0
  13. data/lib/sunspot/configuration.rb +56 -0
  14. data/lib/sunspot/data_extractor.rb +50 -0
  15. data/lib/sunspot/dsl.rb +4 -0
  16. data/lib/sunspot/dsl/field_query.rb +150 -0
  17. data/lib/sunspot/dsl/fields.rb +100 -0
  18. data/lib/sunspot/dsl/fulltext.rb +228 -0
  19. data/lib/sunspot/dsl/query.rb +162 -0
  20. data/lib/sunspot/dsl/query_facet.rb +36 -0
  21. data/lib/sunspot/dsl/restriction.rb +25 -0
  22. data/lib/sunspot/dsl/scope.rb +225 -0
  23. data/lib/sunspot/dsl/search.rb +30 -0
  24. data/lib/sunspot/field.rb +181 -0
  25. data/lib/sunspot/field_factory.rb +144 -0
  26. data/lib/sunspot/indexer.rb +133 -0
  27. data/lib/sunspot/query.rb +9 -0
  28. data/lib/sunspot/query/abstract_field_facet.rb +43 -0
  29. data/lib/sunspot/query/boost_query.rb +20 -0
  30. data/lib/sunspot/query/connective.rb +189 -0
  31. data/lib/sunspot/query/date_field_facet.rb +14 -0
  32. data/lib/sunspot/query/dismax.rb +88 -0
  33. data/lib/sunspot/query/field_facet.rb +9 -0
  34. data/lib/sunspot/query/highlighting.rb +55 -0
  35. data/lib/sunspot/query/local.rb +27 -0
  36. data/lib/sunspot/query/pagination.rb +38 -0
  37. data/lib/sunspot/query/query.rb +86 -0
  38. data/lib/sunspot/query/query_facet.rb +16 -0
  39. data/lib/sunspot/query/restriction.rb +254 -0
  40. data/lib/sunspot/query/scope.rb +9 -0
  41. data/lib/sunspot/query/sort.rb +105 -0
  42. data/lib/sunspot/query/sort_composite.rb +33 -0
  43. data/lib/sunspot/query/text_field_boost.rb +15 -0
  44. data/lib/sunspot/schema.rb +147 -0
  45. data/lib/sunspot/search.rb +216 -0
  46. data/lib/sunspot/search/date_facet.rb +35 -0
  47. data/lib/sunspot/search/facet_row.rb +27 -0
  48. data/lib/sunspot/search/field_facet.rb +44 -0
  49. data/lib/sunspot/search/highlight.rb +38 -0
  50. data/lib/sunspot/search/hit.rb +117 -0
  51. data/lib/sunspot/search/query_facet.rb +62 -0
  52. data/lib/sunspot/session.rb +236 -0
  53. data/lib/sunspot/setup.rb +323 -0
  54. data/lib/sunspot/text_field_setup.rb +29 -0
  55. data/lib/sunspot/type.rb +204 -0
  56. data/lib/sunspot/util.rb +210 -0
  57. data/solr/etc/jetty.xml +212 -0
  58. data/solr/etc/webdefault.xml +379 -0
  59. data/solr/lib/jetty-6.1.3.jar +0 -0
  60. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  61. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  62. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  63. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  64. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  65. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  66. data/solr/solr/conf/elevate.xml +36 -0
  67. data/solr/solr/conf/protwords.txt +21 -0
  68. data/solr/solr/conf/schema.xml +64 -0
  69. data/solr/solr/conf/solrconfig.xml +725 -0
  70. data/solr/solr/conf/stopwords.txt +57 -0
  71. data/solr/solr/conf/synonyms.txt +31 -0
  72. data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
  73. data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
  74. data/solr/solr/lib/jsr108-0.01.jar +0 -0
  75. data/solr/solr/lib/locallucene.jar +0 -0
  76. data/solr/solr/lib/localsolr.jar +0 -0
  77. data/solr/start.jar +0 -0
  78. data/solr/webapps/solr.war +0 -0
  79. data/spec/api/adapters_spec.rb +33 -0
  80. data/spec/api/indexer/attributes_spec.rb +110 -0
  81. data/spec/api/indexer/batch_spec.rb +46 -0
  82. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  83. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  84. data/spec/api/indexer/fulltext_spec.rb +43 -0
  85. data/spec/api/indexer/removal_spec.rb +46 -0
  86. data/spec/api/indexer/spec_helper.rb +1 -0
  87. data/spec/api/indexer_spec.rb +4 -0
  88. data/spec/api/query/adjust_params_spec.rb +37 -0
  89. data/spec/api/query/connectives_spec.rb +176 -0
  90. data/spec/api/query/dsl_spec.rb +12 -0
  91. data/spec/api/query/dynamic_fields_spec.rb +149 -0
  92. data/spec/api/query/faceting_spec.rb +296 -0
  93. data/spec/api/query/fulltext_spec.rb +281 -0
  94. data/spec/api/query/highlighting_spec.rb +225 -0
  95. data/spec/api/query/local_spec.rb +62 -0
  96. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  97. data/spec/api/query/scope_spec.rb +266 -0
  98. data/spec/api/query/spec_helper.rb +1 -0
  99. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  100. data/spec/api/query/types_spec.rb +20 -0
  101. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  102. data/spec/api/search/faceting_spec.rb +242 -0
  103. data/spec/api/search/highlighting_spec.rb +65 -0
  104. data/spec/api/search/hits_spec.rb +67 -0
  105. data/spec/api/search/results_spec.rb +52 -0
  106. data/spec/api/search/search_spec.rb +23 -0
  107. data/spec/api/search/spec_helper.rb +1 -0
  108. data/spec/api/session_spec.rb +198 -0
  109. data/spec/api/spec_helper.rb +1 -0
  110. data/spec/api/sunspot_spec.rb +18 -0
  111. data/spec/helpers/indexer_helper.rb +29 -0
  112. data/spec/helpers/query_helper.rb +13 -0
  113. data/spec/helpers/search_helper.rb +78 -0
  114. data/spec/integration/dynamic_fields_spec.rb +55 -0
  115. data/spec/integration/faceting_spec.rb +188 -0
  116. data/spec/integration/highlighting_spec.rb +22 -0
  117. data/spec/integration/indexing_spec.rb +7 -0
  118. data/spec/integration/keyword_search_spec.rb +245 -0
  119. data/spec/integration/local_search_spec.rb +56 -0
  120. data/spec/integration/scoped_search_spec.rb +303 -0
  121. data/spec/integration/spec_helper.rb +7 -0
  122. data/spec/integration/stored_fields_spec.rb +10 -0
  123. data/spec/integration/test_pagination.rb +32 -0
  124. data/spec/mocks/adapters.rb +32 -0
  125. data/spec/mocks/blog.rb +3 -0
  126. data/spec/mocks/comment.rb +19 -0
  127. data/spec/mocks/connection.rb +106 -0
  128. data/spec/mocks/mock_adapter.rb +30 -0
  129. data/spec/mocks/mock_record.rb +48 -0
  130. data/spec/mocks/photo.rb +11 -0
  131. data/spec/mocks/post.rb +75 -0
  132. data/spec/mocks/super_class.rb +2 -0
  133. data/spec/mocks/user.rb +8 -0
  134. data/spec/spec_helper.rb +68 -0
  135. data/tasks/gemspec.rake +42 -0
  136. data/tasks/rcov.rake +28 -0
  137. data/tasks/rdoc.rake +22 -0
  138. data/tasks/schema.rake +19 -0
  139. data/tasks/spec.rake +24 -0
  140. data/tasks/todo.rake +4 -0
  141. data/templates/schema.xml.erb +36 -0
  142. metadata +319 -0
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'indexer', :type => :indexer do
4
+ end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'typed query' do
4
+ it "should send query to solr with adjusted parameters (keyword example)" do
5
+ session.search Post do
6
+ keywords 'keyword search'
7
+ adjust_solr_params do |params|
8
+ params[:q] = 'new search'
9
+ params[:some] = 'param'
10
+ end
11
+ end
12
+ connection.should have_last_search_with(:q => 'new search')
13
+ connection.should have_last_search_with(:some => 'param')
14
+ end
15
+
16
+ it "should work, even without another dsl command" do
17
+ session.search Post do
18
+ adjust_solr_params do |params|
19
+ params[:q] = 'napoleon dynamite'
20
+ params[:qt] = 'complicated'
21
+ end
22
+ end
23
+ connection.should have_last_search_with(:q => 'napoleon dynamite')
24
+ connection.should have_last_search_with(:qt => 'complicated')
25
+ end
26
+
27
+ it "should be able to extend parameters" do
28
+ session.search Post do
29
+ keywords 'keyword search'
30
+ adjust_solr_params do |params|
31
+ params[:q] += ' AND something_s:more'
32
+ end
33
+ end
34
+ connection.should have_last_search_with(:q => 'keyword search AND something_s:more')
35
+ end
36
+
37
+ end
@@ -0,0 +1,176 @@
1
+ describe 'connective in scope', :type => :query do
2
+ it 'creates a disjunction between two restrictions' do
3
+ session.search Post do
4
+ any_of do
5
+ with :category_ids, 1
6
+ with :blog_id, 2
7
+ end
8
+ end
9
+ connection.should have_last_search_including(
10
+ :fq, '(category_ids_im:1 OR blog_id_i:2)'
11
+ )
12
+ end
13
+
14
+ it 'creates a conjunction inside of a disjunction' do
15
+ session.search Post do
16
+ any_of do
17
+ with :blog_id, 2
18
+ all_of do
19
+ with :category_ids, 1
20
+ with(:average_rating).greater_than(3.0)
21
+ end
22
+ end
23
+ end
24
+ connection.should have_last_search_including(
25
+ :fq,
26
+ '(blog_id_i:2 OR (category_ids_im:1 AND average_rating_f:[3\.0 TO *]))'
27
+ )
28
+ end
29
+
30
+ it 'creates a disjunction with nested conjunction with negated restrictions' do
31
+ session.search Post do
32
+ any_of do
33
+ with :category_ids, 1
34
+ all_of do
35
+ without(:average_rating).greater_than(3.0)
36
+ with(:blog_id, 1)
37
+ end
38
+ end
39
+ end
40
+ connection.should have_last_search_including(
41
+ :fq, '(category_ids_im:1 OR (-average_rating_f:[3\.0 TO *] AND blog_id_i:1))'
42
+ )
43
+ end
44
+
45
+ it 'does nothing special if #all_of called from the top level' do
46
+ session.search Post do
47
+ all_of do
48
+ with :blog_id, 2
49
+ with :category_ids, 1
50
+ end
51
+ end
52
+ connection.should have_last_search_including(
53
+ :fq, 'blog_id_i:2', 'category_ids_im:1'
54
+ )
55
+ end
56
+
57
+ it 'creates a disjunction with negated restrictions' do
58
+ session.search Post do
59
+ any_of do
60
+ with :category_ids, 1
61
+ without(:average_rating).greater_than(3.0)
62
+ end
63
+ end
64
+ connection.should have_last_search_including(
65
+ :fq, '-(-category_ids_im:1 AND average_rating_f:[3\.0 TO *])'
66
+ )
67
+ end
68
+
69
+ it 'creates a disjunction with a negated restriction and a nested disjunction in a conjunction with a negated restriction' do
70
+ session.search(Post) do
71
+ any_of do
72
+ without(:title, 'Yes')
73
+ all_of do
74
+ with(:blog_id, 1)
75
+ any_of do
76
+ with(:category_ids, 4)
77
+ without(:average_rating, 2.0)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ connection.should have_last_search_including(
83
+ :fq, '-(title_ss:Yes AND -(blog_id_i:1 AND -(-category_ids_im:4 AND average_rating_f:2\.0)))'
84
+ )
85
+ end
86
+ it 'creates a disjunction with nested conjunction with nested disjunction with negated restriction' do
87
+ session.search(Post) do
88
+ any_of do
89
+ with(:title, 'Yes')
90
+ all_of do
91
+ with(:blog_id, 1)
92
+ any_of do
93
+ with(:category_ids, 4)
94
+ without(:average_rating, 2.0)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ connection.should have_last_search_including(
100
+ :fq, '(title_ss:Yes OR (blog_id_i:1 AND -(-category_ids_im:4 AND average_rating_f:2\.0)))'
101
+ )
102
+ end
103
+
104
+ #
105
+ # This is important because if a disjunction could be nested in another
106
+ # disjunction, then the inner disjunction could denormalize (and thus
107
+ # become negated) after the outer disjunction denormalized (checking to
108
+ # see if the inner one is negated). Since conjunctions never need to
109
+ # denormalize, if a disjunction can only contain conjunctions or restrictions,
110
+ # we can guarantee that the negation state of a disjunction's components will
111
+ # not change when #to_params is called on them.
112
+ #
113
+ # Since disjunction is associative, this behavior has no effect on the actual
114
+ # logical semantics of the disjunction.
115
+ #
116
+ it 'creates a single disjunction when disjunctions nested' do
117
+ session.search(Post) do
118
+ any_of do
119
+ with(:title, 'Yes')
120
+ any_of do
121
+ with(:blog_id, 1)
122
+ with(:category_ids, 4)
123
+ end
124
+ end
125
+ end
126
+ connection.should have_last_search_including(
127
+ :fq, '(title_ss:Yes OR blog_id_i:1 OR category_ids_im:4)'
128
+ )
129
+ end
130
+
131
+ it 'creates a disjunction with instance exclusion' do
132
+ post = Post.new
133
+ session.search Post do
134
+ any_of do
135
+ without(post)
136
+ with(:category_ids, 1)
137
+ end
138
+ end
139
+ connection.should have_last_search_including(
140
+ :fq, "-(id:Post\\ #{post.id} AND -category_ids_im:1)"
141
+ )
142
+ end
143
+
144
+ it 'creates a disjunction with empty restriction' do
145
+ session.search Post do
146
+ any_of do
147
+ with(:average_rating, nil)
148
+ with(:average_rating).greater_than(3.0)
149
+ end
150
+ end
151
+ connection.should have_last_search_including(
152
+ :fq, '-(average_rating_f:[* TO *] AND -average_rating_f:[3\.0 TO *])'
153
+ )
154
+ end
155
+
156
+ it 'creates a disjunction with some text field components' do
157
+ session.search Post do
158
+ any_of do
159
+ text_fields do
160
+ with(:title).starting_with('test')
161
+ end
162
+ with(:blog_id, 1)
163
+ end
164
+ end
165
+ connection.should have_last_search_including(
166
+ :fq, '(title_text:test* OR blog_id_i:1)'
167
+ )
168
+ end
169
+
170
+ it 'should ignore empty connectives' do
171
+ session.search Post do
172
+ any_of {}
173
+ end
174
+ connection.should_not have_last_search_including(:fq, '')
175
+ end
176
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'query DSL', :type => :query do
4
+ it 'should allow building search using block argument rather than instance_eval' do
5
+ @blog_id = 1
6
+ session.search Post do |query|
7
+ query.with(:blog_id, @blog_id)
8
+ end
9
+ connection.should have_last_search_including(:fq, 'blog_id_i:1')
10
+ end
11
+ end
12
+
@@ -0,0 +1,149 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'dynamic field query', :type => :query do
4
+ it 'restricts by dynamic string field with equality restriction' do
5
+ session.search Post do
6
+ dynamic :custom_string do
7
+ with :test, 'string'
8
+ end
9
+ end
10
+ connection.should have_last_search_including(:fq, 'custom_string\:test_s:string')
11
+ end
12
+
13
+ it 'restricts by dynamic integer field with less than restriction' do
14
+ session.search Post do
15
+ dynamic :custom_integer do
16
+ with(:test).less_than(1)
17
+ end
18
+ end
19
+ connection.should have_last_search_including(:fq, 'custom_integer\:test_i:[* TO 1]')
20
+ end
21
+
22
+ it 'restricts by dynamic float field with between restriction' do
23
+ session.search Post do
24
+ dynamic :custom_float do
25
+ with(:test).between(2.2..3.3)
26
+ end
27
+ end
28
+ connection.should have_last_search_including(:fq, 'custom_float\:test_fm:[2\.2 TO 3\.3]')
29
+ end
30
+
31
+ it 'restricts by dynamic time field with any of restriction' do
32
+ session.search Post do
33
+ dynamic :custom_time do
34
+ with(:test).any_of([Time.parse('2009-02-10 14:00:00 UTC'),
35
+ Time.parse('2009-02-13 18:00:00 UTC')])
36
+ end
37
+ end
38
+ connection.should have_last_search_including(:fq, 'custom_time\:test_d:(2009\-02\-10T14\:00\:00Z OR 2009\-02\-13T18\:00\:00Z)')
39
+ end
40
+
41
+ it 'restricts by dynamic boolean field with equality restriction' do
42
+ session.search Post do
43
+ dynamic :custom_boolean do
44
+ with :test, false
45
+ end
46
+ end
47
+ connection.should have_last_search_including(:fq, 'custom_boolean\:test_b:false')
48
+ end
49
+
50
+ it 'negates a dynamic field restriction' do
51
+ session.search Post do
52
+ dynamic :custom_string do
53
+ without :test, 'foo'
54
+ end
55
+ end
56
+ connection.should have_last_search_including(:fq, '-custom_string\:test_s:foo')
57
+ end
58
+
59
+ it 'scopes by a dynamic field inside a disjunction' do
60
+ session.search Post do
61
+ any_of do
62
+ dynamic :custom_string do
63
+ with :test, 'foo'
64
+ end
65
+ with :title, 'bar'
66
+ end
67
+ end
68
+ connection.should have_last_search_including(
69
+ :fq, '(custom_string\:test_s:foo OR title_ss:bar)'
70
+ )
71
+ end
72
+
73
+ it 'orders by a dynamic field' do
74
+ session.search Post do
75
+ dynamic :custom_integer do
76
+ order_by :test, :desc
77
+ end
78
+ end
79
+ connection.should have_last_search_with(:sort => 'custom_integer:test_i desc')
80
+ end
81
+
82
+ it 'orders by a dynamic field and static field, with given precedence' do
83
+ session.search Post do
84
+ dynamic :custom_integer do
85
+ order_by :test, :desc
86
+ end
87
+ order_by :sort_title, :asc
88
+ end
89
+ connection.should have_last_search_with(:sort => 'custom_integer:test_i desc, sort_title_s asc')
90
+ end
91
+
92
+ it 'raises an UnrecognizedFieldError if an unknown dynamic field is searched by' do
93
+ lambda do
94
+ session.search Post do
95
+ dynamic(:bogus) { with :some, 'value' }
96
+ end
97
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
98
+ end
99
+
100
+ it 'raises a NoMethodError if pagination is attempted in a dynamic query' do
101
+ lambda do
102
+ session.search Post do
103
+ dynamic :custom_string do
104
+ paginate 3, 10
105
+ end
106
+ end
107
+ end.should raise_error(NoMethodError)
108
+ end
109
+
110
+ it 'requests query facet with internal dynamic field' do
111
+ session.search Post do
112
+ facet :test do
113
+ row 'foo' do
114
+ dynamic :custom_string do
115
+ with :test, 'foo'
116
+ end
117
+ end
118
+ end
119
+ end
120
+ connection.should have_last_search_with(
121
+ :"facet.query" => 'custom_string\:test_s:foo'
122
+ )
123
+ end
124
+
125
+ it 'requests query facet with external dynamic field' do
126
+ session.search Post do
127
+ dynamic :custom_string do
128
+ facet :test do
129
+ row 'foo' do
130
+ with :test, 'foo'
131
+ end
132
+ end
133
+ end
134
+ end
135
+ connection.should have_last_search_including(
136
+ :"facet.query",
137
+ 'custom_string\:test_s:foo'
138
+ )
139
+ end
140
+
141
+ it 'allows scoping on dynamic fields common to all types' do
142
+ session.search Post, Namespaced::Comment do
143
+ dynamic :custom_string do
144
+ with(:test, 'test')
145
+ end
146
+ end
147
+ connection.should have_last_search_including(:fq, 'custom_string\\:test_s:test')
148
+ end
149
+ end
@@ -0,0 +1,296 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'faceting' do
4
+ describe 'on fields' do
5
+ it 'does not turn faceting on if no facet requested' do
6
+ session.search(Post)
7
+ connection.should_not have_last_search_with('facet')
8
+ end
9
+
10
+ it 'turns faceting on if facet is requested' do
11
+ session.search Post do
12
+ facet :category_ids
13
+ end
14
+ connection.should have_last_search_with(:facet => 'true')
15
+ end
16
+
17
+ it 'requests single field facet' do
18
+ session.search Post do
19
+ facet :category_ids
20
+ end
21
+ connection.should have_last_search_with(:"facet.field" => %w(category_ids_im))
22
+ end
23
+
24
+ it 'requests multiple field facets' do
25
+ session.search Post do
26
+ facet :category_ids, :blog_id
27
+ end
28
+ connection.should have_last_search_with(:"facet.field" => %w(category_ids_im blog_id_i))
29
+ end
30
+
31
+ it 'sets facet sort by count' do
32
+ session.search Post do
33
+ facet :category_ids, :sort => :count
34
+ end
35
+ connection.should have_last_search_with(:"f.category_ids_im.facet.sort" => 'true')
36
+ end
37
+
38
+ it 'sets facet sort by index' do
39
+ session.search Post do
40
+ facet :category_ids, :sort => :index
41
+ end
42
+ connection.should have_last_search_with(:"f.category_ids_im.facet.sort" => 'false')
43
+ end
44
+
45
+ it 'raises ArgumentError if bogus facet sort provided' do
46
+ lambda do
47
+ session.search Post do
48
+ facet :category_ids, :sort => :sideways
49
+ end
50
+ end.should raise_error(ArgumentError)
51
+ end
52
+
53
+ it 'sets the facet limit' do
54
+ session.search Post do
55
+ facet :category_ids, :limit => 10
56
+ end
57
+ connection.should have_last_search_with(:"f.category_ids_im.facet.limit" => 10)
58
+ end
59
+
60
+ it 'sets the facet minimum count' do
61
+ session.search Post do
62
+ facet :category_ids, :minimum_count => 5
63
+ end
64
+ connection.should have_last_search_with(:"f.category_ids_im.facet.mincount" => 5)
65
+ end
66
+
67
+ it 'sets the facet minimum count to zero if zeros are allowed' do
68
+ session.search Post do
69
+ facet :category_ids, :zeros => true
70
+ end
71
+ connection.should have_last_search_with(:"f.category_ids_im.facet.mincount" => 0)
72
+ end
73
+
74
+ it 'sets the facet minimum count to one by default' do
75
+ session.search Post do
76
+ facet :category_ids
77
+ end
78
+ connection.should have_last_search_with(:"f.category_ids_im.facet.mincount" => 1)
79
+ end
80
+
81
+ it 'sends a query facet for :any extra' do
82
+ session.search Post do
83
+ facet :category_ids, :extra => :any
84
+ end
85
+ connection.should have_last_search_with(:"facet.query" => "category_ids_im:[* TO *]")
86
+ end
87
+
88
+ it 'sends a query facet for :none extra' do
89
+ session.search Post do
90
+ facet :category_ids, :extra => :none
91
+ end
92
+ connection.should have_last_search_with(:"facet.query" => "-category_ids_im:[* TO *]")
93
+ end
94
+
95
+ it 'raises an ArgumentError if bogus extra is passed' do
96
+ lambda do
97
+ session.search Post do
98
+ facet :category_ids, :extra => :bogus
99
+ end
100
+ end.should raise_error(ArgumentError)
101
+ end
102
+ end
103
+
104
+ describe 'on time ranges' do
105
+ before :each do
106
+ @time_range = (Time.parse('2009-06-01 00:00:00 -0400')..
107
+ Time.parse('2009-07-01 00:00:00 -0400'))
108
+ end
109
+
110
+ it 'does not send date facet parameters if time range is not specified' do
111
+ session.search Post do |query|
112
+ query.facet :published_at
113
+ end
114
+ connection.should_not have_last_search_with(:"facet.date")
115
+ end
116
+
117
+ it 'sets the facet to a date facet if time range is specified' do
118
+ session.search Post do |query|
119
+ query.facet :published_at, :time_range => @time_range
120
+ end
121
+ connection.should have_last_search_with(:"facet.date" => ['published_at_d'])
122
+ end
123
+
124
+ it 'sets the facet start and end' do
125
+ session.search Post do |query|
126
+ query.facet :published_at, :time_range => @time_range
127
+ end
128
+ connection.should have_last_search_with(
129
+ :"f.published_at_d.facet.date.start" => '2009-06-01T04:00:00Z',
130
+ :"f.published_at_d.facet.date.end" => '2009-07-01T04:00:00Z'
131
+ )
132
+ end
133
+
134
+ it 'defaults the time interval to 1 day' do
135
+ session.search Post do |query|
136
+ query.facet :published_at, :time_range => @time_range
137
+ end
138
+ connection.should have_last_search_with(:"f.published_at_d.facet.date.gap" => "+86400SECONDS")
139
+ end
140
+
141
+ it 'uses custom time interval' do
142
+ session.search Post do |query|
143
+ query.facet :published_at, :time_range => @time_range, :time_interval => 3600
144
+ end
145
+ connection.should have_last_search_with(:"f.published_at_d.facet.date.gap" => "+3600SECONDS")
146
+ end
147
+
148
+ it 'does not allow date faceting on a non-date field' do
149
+ lambda do
150
+ session.search Post do |query|
151
+ query.facet :blog_id, :time_range => @time_range
152
+ end
153
+ end.should raise_error(ArgumentError)
154
+ end
155
+ end
156
+
157
+ describe 'using queries' do
158
+ it 'turns faceting on' do
159
+ session.search Post do
160
+ facet :foo do
161
+ row :bar do
162
+ with(:average_rating).between(4.0..5.0)
163
+ end
164
+ end
165
+ end
166
+ connection.should have_last_search_with(:facet => 'true')
167
+ end
168
+
169
+ it 'facets by query' do
170
+ session.search Post do
171
+ facet :foo do
172
+ row :bar do
173
+ with(:average_rating).between(4.0..5.0)
174
+ end
175
+ end
176
+ end
177
+ connection.should have_last_search_with(:"facet.query" => 'average_rating_f:[4\.0 TO 5\.0]')
178
+ end
179
+
180
+ it 'requests multiple query facets' do
181
+ session.search Post do
182
+ facet :foo do
183
+ row :bar do
184
+ with(:average_rating).between(3.0..4.0)
185
+ end
186
+ row :baz do
187
+ with(:average_rating).between(4.0..5.0)
188
+ end
189
+ end
190
+ end
191
+ connection.should have_last_search_with(
192
+ :"facet.query" => [
193
+ 'average_rating_f:[3\.0 TO 4\.0]',
194
+ 'average_rating_f:[4\.0 TO 5\.0]'
195
+ ]
196
+ )
197
+ end
198
+
199
+ it 'requests query facet with multiple conditions' do
200
+ session.search Post do
201
+ facet :foo do
202
+ row :bar do
203
+ with(:category_ids, 1)
204
+ with(:blog_id, 2)
205
+ end
206
+ end
207
+ end
208
+ connection.should have_last_search_with(
209
+ :"facet.query" => '(category_ids_im:1 AND blog_id_i:2)'
210
+ )
211
+ end
212
+
213
+ it 'requests query facet with disjunction' do
214
+ session.search Post do
215
+ facet :foo do
216
+ row :bar do
217
+ any_of do
218
+ with(:category_ids, 1)
219
+ with(:blog_id, 2)
220
+ end
221
+ end
222
+ end
223
+ end
224
+ connection.should have_last_search_with(
225
+ :"facet.query" => '(category_ids_im:1 OR blog_id_i:2)'
226
+ )
227
+ end
228
+
229
+ it 'builds query facets when passed :only argument to field facet declaration' do
230
+ session.search Post do
231
+ facet :category_ids, :only => [1, 3]
232
+ end
233
+ connection.should have_last_search_with(
234
+ :"facet.query" => ['category_ids_im:1', 'category_ids_im:3']
235
+ )
236
+ end
237
+
238
+ it 'converts limited query facet values to the correct type' do
239
+ session.search Post do
240
+ facet :published_at, :only => [Time.utc(2009, 8, 28, 15, 33), Time.utc(2008,8, 28, 15, 33)]
241
+ end
242
+ connection.should have_last_search_with(
243
+ :"facet.query" => [
244
+ 'published_at_d:2009\-08\-28T15\:33\:00Z',
245
+ 'published_at_d:2008\-08\-28T15\:33\:00Z'
246
+ ]
247
+ )
248
+ end
249
+
250
+ it 'ignores facet query with no rows' do
251
+ session.search Post do
252
+ facet(:foo) {}
253
+ end
254
+ connection.should_not have_last_search_with(:"facet.query")
255
+ end
256
+
257
+ it 'ignores facet query row with no restrictions' do
258
+ session.search Post do
259
+ facet :foo do
260
+ row(:bar) do
261
+ with(:blog_id, 1)
262
+ end
263
+ row(:baz) {}
264
+ end
265
+ end
266
+ connection.searches.last[:"facet.query"].should be_a(String)
267
+ end
268
+
269
+ it 'ignores facet query with only empty rows' do
270
+ session.search Post do
271
+ facet :foo do
272
+ row(:bar) {}
273
+ end
274
+ end
275
+ connection.should_not have_last_search_with(:"facet.query")
276
+ end
277
+
278
+ it 'does not allow 0 arguments to facet method with block' do
279
+ lambda do
280
+ session.search Post do
281
+ facet do
282
+ end
283
+ end
284
+ end.should raise_error(ArgumentError)
285
+ end
286
+
287
+ it 'does not allow more than 1 argument to facet method with block' do
288
+ lambda do
289
+ session.search Post do
290
+ facet :foo, :bar do
291
+ end
292
+ end
293
+ end.should raise_error(ArgumentError)
294
+ end
295
+ end
296
+ end