nuatt_sunspot 1.1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/History.txt +198 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +239 -0
  4. data/Rakefile +11 -0
  5. data/TODO +13 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-installer +19 -0
  8. data/bin/sunspot-solr +74 -0
  9. data/installer/config/schema.yml +95 -0
  10. data/lib/light_config.rb +40 -0
  11. data/lib/sunspot.rb +529 -0
  12. data/lib/sunspot/adapters.rb +265 -0
  13. data/lib/sunspot/composite_setup.rb +202 -0
  14. data/lib/sunspot/configuration.rb +46 -0
  15. data/lib/sunspot/data_extractor.rb +50 -0
  16. data/lib/sunspot/dsl.rb +5 -0
  17. data/lib/sunspot/dsl/adjustable.rb +47 -0
  18. data/lib/sunspot/dsl/field_query.rb +266 -0
  19. data/lib/sunspot/dsl/fields.rb +113 -0
  20. data/lib/sunspot/dsl/fulltext.rb +243 -0
  21. data/lib/sunspot/dsl/function.rb +14 -0
  22. data/lib/sunspot/dsl/functional.rb +41 -0
  23. data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
  24. data/lib/sunspot/dsl/paginatable.rb +28 -0
  25. data/lib/sunspot/dsl/query_facet.rb +36 -0
  26. data/lib/sunspot/dsl/restriction.rb +25 -0
  27. data/lib/sunspot/dsl/scope.rb +229 -0
  28. data/lib/sunspot/dsl/search.rb +30 -0
  29. data/lib/sunspot/dsl/standard_query.rb +125 -0
  30. data/lib/sunspot/field.rb +192 -0
  31. data/lib/sunspot/field_factory.rb +147 -0
  32. data/lib/sunspot/indexer.rb +131 -0
  33. data/lib/sunspot/installer.rb +31 -0
  34. data/lib/sunspot/installer/library_installer.rb +45 -0
  35. data/lib/sunspot/installer/schema_builder.rb +219 -0
  36. data/lib/sunspot/installer/solrconfig_updater.rb +106 -0
  37. data/lib/sunspot/installer/task_helper.rb +18 -0
  38. data/lib/sunspot/query.rb +10 -0
  39. data/lib/sunspot/query/abstract_field_facet.rb +52 -0
  40. data/lib/sunspot/query/boost_query.rb +24 -0
  41. data/lib/sunspot/query/common_query.rb +85 -0
  42. data/lib/sunspot/query/composite_fulltext.rb +31 -0
  43. data/lib/sunspot/query/connective.rb +191 -0
  44. data/lib/sunspot/query/date_field_facet.rb +14 -0
  45. data/lib/sunspot/query/dismax.rb +127 -0
  46. data/lib/sunspot/query/field_facet.rb +41 -0
  47. data/lib/sunspot/query/filter.rb +38 -0
  48. data/lib/sunspot/query/function_query.rb +52 -0
  49. data/lib/sunspot/query/highlighting.rb +55 -0
  50. data/lib/sunspot/query/local.rb +26 -0
  51. data/lib/sunspot/query/more_like_this.rb +60 -0
  52. data/lib/sunspot/query/more_like_this_query.rb +12 -0
  53. data/lib/sunspot/query/pagination.rb +38 -0
  54. data/lib/sunspot/query/query_facet.rb +16 -0
  55. data/lib/sunspot/query/restriction.rb +262 -0
  56. data/lib/sunspot/query/scope.rb +9 -0
  57. data/lib/sunspot/query/sort.rb +95 -0
  58. data/lib/sunspot/query/sort_composite.rb +33 -0
  59. data/lib/sunspot/query/standard_query.rb +20 -0
  60. data/lib/sunspot/query/text_field_boost.rb +17 -0
  61. data/lib/sunspot/schema.rb +151 -0
  62. data/lib/sunspot/search.rb +9 -0
  63. data/lib/sunspot/search/abstract_search.rb +302 -0
  64. data/lib/sunspot/search/date_facet.rb +35 -0
  65. data/lib/sunspot/search/facet_row.rb +27 -0
  66. data/lib/sunspot/search/field_facet.rb +88 -0
  67. data/lib/sunspot/search/highlight.rb +38 -0
  68. data/lib/sunspot/search/hit.rb +136 -0
  69. data/lib/sunspot/search/more_like_this_search.rb +31 -0
  70. data/lib/sunspot/search/query_facet.rb +62 -0
  71. data/lib/sunspot/search/standard_search.rb +21 -0
  72. data/lib/sunspot/server.rb +152 -0
  73. data/lib/sunspot/session.rb +252 -0
  74. data/lib/sunspot/session_proxy.rb +71 -0
  75. data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
  76. data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
  77. data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
  78. data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
  79. data/lib/sunspot/session_proxy/sharding_session_proxy.rb +215 -0
  80. data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
  81. data/lib/sunspot/setup.rb +366 -0
  82. data/lib/sunspot/text_field_setup.rb +29 -0
  83. data/lib/sunspot/type.rb +340 -0
  84. data/lib/sunspot/util.rb +253 -0
  85. data/lib/sunspot/version.rb +3 -0
  86. data/solr/etc/jetty.xml +214 -0
  87. data/solr/etc/webdefault.xml +379 -0
  88. data/solr/lib/jetty-6.1.3.jar +0 -0
  89. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  90. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  91. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  92. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  93. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  94. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  95. data/solr/solr/conf/admin-extra.html +31 -0
  96. data/solr/solr/conf/elevate.xml +36 -0
  97. data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
  98. data/solr/solr/conf/protwords.txt +21 -0
  99. data/solr/solr/conf/schema.xml +240 -0
  100. data/solr/solr/conf/scripts.conf +24 -0
  101. data/solr/solr/conf/solrconfig.xml +938 -0
  102. data/solr/solr/conf/spellings.txt +2 -0
  103. data/solr/solr/conf/stopwords.es.txt +345 -0
  104. data/solr/solr/conf/stopwords.txt +58 -0
  105. data/solr/solr/conf/synonyms.txt +31 -0
  106. data/solr/solr/lib/lucene-spatial-2.9.1.jar +0 -0
  107. data/solr/solr/lib/solr-spatial-light-0.0.6.jar +0 -0
  108. data/solr/start.jar +0 -0
  109. data/solr/webapps/solr.war +0 -0
  110. data/spec/api/adapters_spec.rb +33 -0
  111. data/spec/api/binding_spec.rb +38 -0
  112. data/spec/api/indexer/attributes_spec.rb +149 -0
  113. data/spec/api/indexer/batch_spec.rb +46 -0
  114. data/spec/api/indexer/dynamic_fields_spec.rb +42 -0
  115. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  116. data/spec/api/indexer/fulltext_spec.rb +43 -0
  117. data/spec/api/indexer/removal_spec.rb +53 -0
  118. data/spec/api/indexer/spec_helper.rb +1 -0
  119. data/spec/api/indexer_spec.rb +14 -0
  120. data/spec/api/query/advanced_manipulation_examples.rb +35 -0
  121. data/spec/api/query/connectives_examples.rb +176 -0
  122. data/spec/api/query/dsl_spec.rb +18 -0
  123. data/spec/api/query/dynamic_fields_examples.rb +165 -0
  124. data/spec/api/query/faceting_examples.rb +399 -0
  125. data/spec/api/query/fulltext_examples.rb +315 -0
  126. data/spec/api/query/function_spec.rb +70 -0
  127. data/spec/api/query/highlighting_examples.rb +225 -0
  128. data/spec/api/query/local_examples.rb +38 -0
  129. data/spec/api/query/more_like_this_spec.rb +140 -0
  130. data/spec/api/query/ordering_pagination_examples.rb +97 -0
  131. data/spec/api/query/scope_examples.rb +256 -0
  132. data/spec/api/query/spec_helper.rb +1 -0
  133. data/spec/api/query/standard_spec.rb +28 -0
  134. data/spec/api/query/text_field_scoping_examples.rb +30 -0
  135. data/spec/api/query/types_spec.rb +20 -0
  136. data/spec/api/search/dynamic_fields_spec.rb +33 -0
  137. data/spec/api/search/faceting_spec.rb +356 -0
  138. data/spec/api/search/highlighting_spec.rb +69 -0
  139. data/spec/api/search/hits_spec.rb +138 -0
  140. data/spec/api/search/results_spec.rb +79 -0
  141. data/spec/api/search/search_spec.rb +23 -0
  142. data/spec/api/search/spec_helper.rb +1 -0
  143. data/spec/api/server_spec.rb +91 -0
  144. data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
  145. data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
  146. data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
  147. data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
  148. data/spec/api/session_proxy/spec_helper.rb +9 -0
  149. data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +50 -0
  150. data/spec/api/session_spec.rb +198 -0
  151. data/spec/api/spec_helper.rb +3 -0
  152. data/spec/api/sunspot_spec.rb +18 -0
  153. data/spec/ext.rb +11 -0
  154. data/spec/helpers/indexer_helper.rb +29 -0
  155. data/spec/helpers/query_helper.rb +38 -0
  156. data/spec/helpers/search_helper.rb +80 -0
  157. data/spec/integration/dynamic_fields_spec.rb +55 -0
  158. data/spec/integration/faceting_spec.rb +238 -0
  159. data/spec/integration/highlighting_spec.rb +22 -0
  160. data/spec/integration/indexing_spec.rb +33 -0
  161. data/spec/integration/keyword_search_spec.rb +317 -0
  162. data/spec/integration/local_search_spec.rb +91 -0
  163. data/spec/integration/more_like_this_spec.rb +43 -0
  164. data/spec/integration/scoped_search_spec.rb +324 -0
  165. data/spec/integration/spec_helper.rb +7 -0
  166. data/spec/integration/stored_fields_spec.rb +10 -0
  167. data/spec/integration/test_pagination.rb +32 -0
  168. data/spec/mocks/adapters.rb +32 -0
  169. data/spec/mocks/blog.rb +3 -0
  170. data/spec/mocks/comment.rb +21 -0
  171. data/spec/mocks/connection.rb +122 -0
  172. data/spec/mocks/mock_adapter.rb +30 -0
  173. data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
  174. data/spec/mocks/mock_record.rb +52 -0
  175. data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
  176. data/spec/mocks/photo.rb +12 -0
  177. data/spec/mocks/post.rb +76 -0
  178. data/spec/mocks/super_class.rb +2 -0
  179. data/spec/mocks/user.rb +8 -0
  180. data/spec/spec_helper.rb +52 -0
  181. data/tasks/gemspec.rake +33 -0
  182. data/tasks/rcov.rake +28 -0
  183. data/tasks/rdoc.rake +27 -0
  184. data/tasks/schema.rake +19 -0
  185. data/tasks/spec.rake +24 -0
  186. data/tasks/todo.rake +4 -0
  187. metadata +355 -0
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'indexer', :type => :indexer do
4
+ it 'should completely wipe setup if class redefined (reloaded)' do
5
+ Object::ReloadableClass = Class.new(MockRecord)
6
+ Sunspot.setup(ReloadableClass) { string(:title) }
7
+ Object.class_eval { remove_const(:ReloadableClass) }
8
+ Object::ReloadableClass = Class.new(MockRecord)
9
+ Sunspot.setup(ReloadableClass) {}
10
+ lambda do
11
+ Sunspot.search(ReloadableClass) { with(:title, 'title') }
12
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ shared_examples_for "query with advanced manipulation" do
4
+ describe 'adjust_solr_params' do
5
+ before :each do
6
+ search do
7
+ adjust_solr_params do |params|
8
+ params[:rows] = 40
9
+ params[:qt] = 'complicated'
10
+ end
11
+ end
12
+ end
13
+
14
+ it "modifies existing param" do
15
+ connection.should have_last_search_with(:rows => 40)
16
+ end
17
+
18
+ it "adds new param" do
19
+ connection.should have_last_search_with(:qt => 'complicated')
20
+ end
21
+ end
22
+
23
+ describe 'request_handler' do
24
+ before :each do
25
+ connection.expected_handler = :myRequestHandler
26
+ search do
27
+ request_handler :myRequestHandler
28
+ end
29
+ end
30
+
31
+ it 'should use specified request handler' do
32
+ connection.should have_last_search_with({})
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,176 @@
1
+ shared_examples_for "query with connective scope" do
2
+ it 'creates a disjunction between two restrictions' do
3
+ search 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
+ search 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_ft:[3\.0 TO *]))'
27
+ )
28
+ end
29
+
30
+ it 'creates a disjunction with nested conjunction with negated restrictions' do
31
+ search 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_ft:[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
+ search 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
+ search 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_ft:[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
+ search 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_ft:2\.0)))'
84
+ )
85
+ end
86
+ it 'creates a disjunction with nested conjunction with nested disjunction with negated restriction' do
87
+ search 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_ft: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
+ search 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
+ search 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
+ search 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_ft:[* TO *] AND -average_rating_ft:[3\.0 TO *])'
153
+ )
154
+ end
155
+
156
+ it 'creates a disjunction with some text field components' do
157
+ search 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
+ search do
172
+ any_of {}
173
+ end
174
+ connection.should_not have_last_search_including(:fq, '')
175
+ end
176
+ end
@@ -0,0 +1,18 @@
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
+
12
+ it 'should accept a block in the #new_search method' do
13
+ search = session.new_search(Post) { with(:blog_id, 1) }
14
+ search.execute
15
+ connection.should have_last_search_including(:fq, 'blog_id_i:1')
16
+ end
17
+ end
18
+
@@ -0,0 +1,165 @@
1
+ shared_examples_for "query with dynamic field support" do
2
+ it 'restricts by dynamic string field with equality restriction' do
3
+ search do
4
+ dynamic :custom_string do
5
+ with :test, 'string'
6
+ end
7
+ end
8
+ connection.should have_last_search_including(:fq, 'custom_string\:test_ss:string')
9
+ end
10
+
11
+ it 'restricts by dynamic integer field with less than restriction' do
12
+ search do
13
+ dynamic :custom_integer do
14
+ with(:test).less_than(1)
15
+ end
16
+ end
17
+ connection.should have_last_search_including(:fq, 'custom_integer\:test_i:[* TO 1]')
18
+ end
19
+
20
+ it 'restricts by dynamic float field with between restriction' do
21
+ search do
22
+ dynamic :custom_float do
23
+ with(:test).between(2.2..3.3)
24
+ end
25
+ end
26
+ connection.should have_last_search_including(:fq, 'custom_float\:test_fm:[2\.2 TO 3\.3]')
27
+ end
28
+
29
+ it 'restricts by dynamic time field with any of restriction' do
30
+ search do
31
+ dynamic :custom_time do
32
+ with(:test).any_of([Time.parse('2009-02-10 14:00:00 UTC'),
33
+ Time.parse('2009-02-13 18:00:00 UTC')])
34
+ end
35
+ end
36
+ connection.should have_last_search_including(:fq, 'custom_time\:test_d:(2009\-02\-10T14\:00\:00Z OR 2009\-02\-13T18\:00\:00Z)')
37
+ end
38
+
39
+ it 'restricts by dynamic boolean field with equality restriction' do
40
+ search do
41
+ dynamic :custom_boolean do
42
+ with :test, false
43
+ end
44
+ end
45
+ connection.should have_last_search_including(:fq, 'custom_boolean\:test_b:false')
46
+ end
47
+
48
+ it 'negates a dynamic field restriction' do
49
+ search do
50
+ dynamic :custom_string do
51
+ without :test, 'foo'
52
+ end
53
+ end
54
+ connection.should have_last_search_including(:fq, '-custom_string\:test_ss:foo')
55
+ end
56
+
57
+ it 'scopes by a dynamic field inside a disjunction' do
58
+ search do
59
+ any_of do
60
+ dynamic :custom_string do
61
+ with :test, 'foo'
62
+ end
63
+ with :title, 'bar'
64
+ end
65
+ end
66
+ connection.should have_last_search_including(
67
+ :fq, '(custom_string\:test_ss:foo OR title_ss:bar)'
68
+ )
69
+ end
70
+
71
+ it 'orders by a dynamic field' do
72
+ search do
73
+ dynamic :custom_integer do
74
+ order_by :test, :desc
75
+ end
76
+ end
77
+ connection.should have_last_search_with(:sort => 'custom_integer:test_i desc')
78
+ end
79
+
80
+ it 'orders by a dynamic field and static field, with given precedence' do
81
+ search do
82
+ dynamic :custom_integer do
83
+ order_by :test, :desc
84
+ end
85
+ order_by :sort_title, :asc
86
+ end
87
+ connection.should have_last_search_with(:sort => 'custom_integer:test_i desc, sort_title_s asc')
88
+ end
89
+
90
+ it 'raises an UnrecognizedFieldError if an unknown dynamic field is searched by' do
91
+ lambda do
92
+ search do
93
+ dynamic(:bogus) { with :some, 'value' }
94
+ end
95
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
96
+ end
97
+
98
+ it 'raises a NoMethodError if pagination is attempted in a dynamic query' do
99
+ lambda do
100
+ search do
101
+ dynamic :custom_string do
102
+ paginate :page => 3, :per_page => 10
103
+ end
104
+ end
105
+ end.should raise_error(NoMethodError)
106
+ end
107
+
108
+ it 'requests field facet on dynamic field' do
109
+ search do
110
+ dynamic :custom_string do
111
+ facet(:test)
112
+ end
113
+ end
114
+ connection.should have_last_search_including(:"facet.field", 'custom_string:test_ss')
115
+ end
116
+
117
+ it 'requests named field facet on dynamic field' do
118
+ search do
119
+ dynamic :custom_string do
120
+ facet(:test, :name => :bogus)
121
+ end
122
+ end
123
+ connection.should have_last_search_including(:"facet.field", '{!key=bogus}custom_string:test_ss')
124
+ end
125
+
126
+ it 'requests query facet with internal dynamic field' do
127
+ search do
128
+ facet :test do
129
+ row 'foo' do
130
+ dynamic :custom_string do
131
+ with :test, 'foo'
132
+ end
133
+ end
134
+ end
135
+ end
136
+ connection.should have_last_search_with(
137
+ :"facet.query" => 'custom_string\:test_ss:foo'
138
+ )
139
+ end
140
+
141
+ it 'requests query facet with external dynamic field' do
142
+ search do
143
+ dynamic :custom_string do
144
+ facet :test do
145
+ row 'foo' do
146
+ with :test, 'foo'
147
+ end
148
+ end
149
+ end
150
+ end
151
+ connection.should have_last_search_including(
152
+ :"facet.query",
153
+ 'custom_string\:test_ss:foo'
154
+ )
155
+ end
156
+
157
+ it 'allows scoping on dynamic fields common to all types' do
158
+ search Post, Namespaced::Comment do
159
+ dynamic :custom_float do
160
+ with(:test, 1.23)
161
+ end
162
+ end
163
+ connection.should have_last_search_including(:fq, 'custom_float\\:test_fm:1\\.23')
164
+ end
165
+ end
@@ -0,0 +1,399 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ shared_examples_for "facetable query" do
4
+ describe 'on fields' do
5
+ it 'does not turn faceting on if no facet requested' do
6
+ search
7
+ connection.should_not have_last_search_with('facet')
8
+ end
9
+
10
+ it 'turns faceting on if facet is requested' do
11
+ search 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
+ search 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
+ search 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
+ search 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
+ search 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
+ search 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
+ search 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
+ search 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
+ search 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
+ search 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 'sets the facet prefix' do
82
+ search do
83
+ facet :title, :prefix => 'Test'
84
+ end
85
+ connection.should have_last_search_with(:"f.title_ss.facet.prefix" => 'Test')
86
+ end
87
+
88
+ it 'escapes the facet prefix' do
89
+ search do
90
+ facet :title, :prefix => 'Test Title'
91
+ end
92
+ connection.should have_last_search_with(:"f.title_ss.facet.prefix" => 'Test\ Title')
93
+ end
94
+
95
+ it 'sends a query facet for :any extra' do
96
+ search do
97
+ facet :category_ids, :extra => :any
98
+ end
99
+ connection.should have_last_search_with(:"facet.query" => "category_ids_im:[* TO *]")
100
+ end
101
+
102
+ it 'sends a query facet for :none extra' do
103
+ search do
104
+ facet :category_ids, :extra => :none
105
+ end
106
+ connection.should have_last_search_with(:"facet.query" => "-category_ids_im:[* TO *]")
107
+ end
108
+
109
+ it 'raises an ArgumentError if bogus extra is passed' do
110
+ lambda do
111
+ search do
112
+ facet :category_ids, :extra => :bogus
113
+ end
114
+ end.should raise_error(ArgumentError)
115
+ end
116
+
117
+ it 'tags and excludes a scope filter in a field facet' do
118
+ search do
119
+ blog_filter = with(:blog_id, 1)
120
+ facet(:blog_id, :exclude => blog_filter)
121
+ end
122
+ filter_tag = get_filter_tag('blog_id_i:1')
123
+ connection.should have_last_search_with(
124
+ :"facet.field" => %W({!ex=#{filter_tag}}blog_id_i)
125
+ )
126
+ end
127
+
128
+ it 'tags and excludes a disjunction filter in a field facet' do
129
+ search do
130
+ blog_filter = any_of do
131
+ with(:blog_id, 1)
132
+ with(:blog_id, 2)
133
+ end
134
+ facet(:blog_id, :exclude => blog_filter)
135
+ end
136
+ filter_tag = get_filter_tag('(blog_id_i:1 OR blog_id_i:2)')
137
+ connection.should have_last_search_with(
138
+ :"facet.field" => %W({!ex=#{filter_tag}}blog_id_i)
139
+ )
140
+ end
141
+
142
+ it 'tags and excludes multiple filters in a field facet' do
143
+ search do
144
+ blog_filter = with(:blog_id, 1)
145
+ category_filter = with(:category_ids, 2)
146
+ facet(:blog_id, :exclude => [blog_filter, category_filter])
147
+ end
148
+ filter_tags = %w(blog_id_i:1 category_ids_im:2).map do |phrase|
149
+ get_filter_tag(phrase)
150
+ end.join(',')
151
+ connection.should have_last_search_with(
152
+ :"facet.field" => %W({!ex=#{filter_tags}}blog_id_i)
153
+ )
154
+ end
155
+
156
+ it 'does not tag a filter if it is not excluded' do
157
+ search do
158
+ with(:blog_id, 1)
159
+ end
160
+ connection.should have_last_search_including(:fq, "blog_id_i:1")
161
+ end
162
+
163
+ it 'names a field facet' do
164
+ search do
165
+ facet(:blog_id, :name => :blog)
166
+ end
167
+ connection.should have_last_search_including(:"facet.field", "{!key=blog}blog_id_i")
168
+ end
169
+
170
+ it 'uses the custom field facet name in facet option parameters' do
171
+ search do
172
+ facet(:blog_id, :name => :blog, :sort => :count)
173
+ end
174
+ connection.should have_last_search_with(:"f.blog.facet.sort" => 'true')
175
+ end
176
+
177
+ it 'raises an ArgumentError if exclusion attempted on a query facet' do
178
+ lambda do
179
+ search do
180
+ blog_filter = with(:blog_id, 1)
181
+ facet(:bad, :exclude => blog_filter) do
182
+ row(:bogus) { with(:blog_id, 1) }
183
+ end
184
+ end
185
+ end.should raise_error(ArgumentError)
186
+ end
187
+
188
+ it 'raises an ArgumentError if exclusion attempted on a restricted field facet' do
189
+ lambda do
190
+ search do
191
+ blog_filter = with(:blog_id, 1)
192
+ facet(:blog_id, :only => 1, :exclude => blog_filter)
193
+ end
194
+ end.should raise_error(ArgumentError)
195
+ end
196
+
197
+ it 'raises an ArgumentError if exclusion attempted on a facet with :extra' do
198
+ lambda do
199
+ search do
200
+ blog_filter = with(:blog_id, 1)
201
+ facet(:blog_id, :extra => :all, :exclude => blog_filter)
202
+ end
203
+ end.should raise_error(ArgumentError)
204
+ end
205
+ end
206
+
207
+ describe 'on time ranges' do
208
+ before :each do
209
+ @time_range = (Time.parse('2009-06-01 00:00:00 -0400')..
210
+ Time.parse('2009-07-01 00:00:00 -0400'))
211
+ end
212
+
213
+ it 'does not send date facet parameters if time range is not specified' do
214
+ search do |query|
215
+ query.facet :published_at
216
+ end
217
+ connection.should_not have_last_search_with(:"facet.date")
218
+ end
219
+
220
+ it 'sets the facet to a date facet if time range is specified' do
221
+ search do |query|
222
+ query.facet :published_at, :time_range => @time_range
223
+ end
224
+ connection.should have_last_search_with(:"facet.date" => ['published_at_dt'])
225
+ end
226
+
227
+ it 'sets the facet start and end' do
228
+ search do |query|
229
+ query.facet :published_at, :time_range => @time_range
230
+ end
231
+ connection.should have_last_search_with(
232
+ :"f.published_at_dt.facet.date.start" => '2009-06-01T04:00:00Z',
233
+ :"f.published_at_dt.facet.date.end" => '2009-07-01T04:00:00Z'
234
+ )
235
+ end
236
+
237
+ it 'defaults the time interval to 1 day' do
238
+ search do |query|
239
+ query.facet :published_at, :time_range => @time_range
240
+ end
241
+ connection.should have_last_search_with(:"f.published_at_dt.facet.date.gap" => "+86400SECONDS")
242
+ end
243
+
244
+ it 'uses custom time interval' do
245
+ search do |query|
246
+ query.facet :published_at, :time_range => @time_range, :time_interval => 3600
247
+ end
248
+ connection.should have_last_search_with(:"f.published_at_dt.facet.date.gap" => "+3600SECONDS")
249
+ end
250
+
251
+ it 'does not allow date faceting on a non-date field' do
252
+ lambda do
253
+ search do |query|
254
+ query.facet :blog_id, :time_range => @time_range
255
+ end
256
+ end.should raise_error(ArgumentError)
257
+ end
258
+ end
259
+
260
+ describe 'using queries' do
261
+ it 'turns faceting on' do
262
+ search do
263
+ facet :foo do
264
+ row :bar do
265
+ with(:average_rating).between(4.0..5.0)
266
+ end
267
+ end
268
+ end
269
+ connection.should have_last_search_with(:facet => 'true')
270
+ end
271
+
272
+ it 'facets by query' do
273
+ search do
274
+ facet :foo do
275
+ row :bar do
276
+ with(:average_rating).between(4.0..5.0)
277
+ end
278
+ end
279
+ end
280
+ connection.should have_last_search_with(:"facet.query" => 'average_rating_ft:[4\.0 TO 5\.0]')
281
+ end
282
+
283
+ it 'requests multiple query facets' do
284
+ search do
285
+ facet :foo do
286
+ row :bar do
287
+ with(:average_rating).between(3.0..4.0)
288
+ end
289
+ row :baz do
290
+ with(:average_rating).between(4.0..5.0)
291
+ end
292
+ end
293
+ end
294
+ connection.should have_last_search_with(
295
+ :"facet.query" => [
296
+ 'average_rating_ft:[3\.0 TO 4\.0]',
297
+ 'average_rating_ft:[4\.0 TO 5\.0]'
298
+ ]
299
+ )
300
+ end
301
+
302
+ it 'requests query facet with multiple conditions' do
303
+ search do
304
+ facet :foo do
305
+ row :bar do
306
+ with(:category_ids, 1)
307
+ with(:blog_id, 2)
308
+ end
309
+ end
310
+ end
311
+ connection.should have_last_search_with(
312
+ :"facet.query" => '(category_ids_im:1 AND blog_id_i:2)'
313
+ )
314
+ end
315
+
316
+ it 'requests query facet with disjunction' do
317
+ search do
318
+ facet :foo do
319
+ row :bar do
320
+ any_of do
321
+ with(:category_ids, 1)
322
+ with(:blog_id, 2)
323
+ end
324
+ end
325
+ end
326
+ end
327
+ connection.should have_last_search_with(
328
+ :"facet.query" => '(category_ids_im:1 OR blog_id_i:2)'
329
+ )
330
+ end
331
+
332
+ it 'builds query facets when passed :only argument to field facet declaration' do
333
+ search do
334
+ facet :category_ids, :only => [1, 3]
335
+ end
336
+ connection.should have_last_search_with(
337
+ :"facet.query" => ['category_ids_im:1', 'category_ids_im:3']
338
+ )
339
+ end
340
+
341
+ it 'converts limited query facet values to the correct type' do
342
+ search do
343
+ facet :published_at, :only => [Time.utc(2009, 8, 28, 15, 33), Time.utc(2008,8, 28, 15, 33)]
344
+ end
345
+ connection.should have_last_search_with(
346
+ :"facet.query" => [
347
+ 'published_at_dt:2009\-08\-28T15\:33\:00Z',
348
+ 'published_at_dt:2008\-08\-28T15\:33\:00Z'
349
+ ]
350
+ )
351
+ end
352
+
353
+ it 'ignores facet query with no rows' do
354
+ search do
355
+ facet(:foo) {}
356
+ end
357
+ connection.should_not have_last_search_with(:"facet.query")
358
+ end
359
+
360
+ it 'ignores facet query row with no restrictions' do
361
+ search do
362
+ facet :foo do
363
+ row(:bar) do
364
+ with(:blog_id, 1)
365
+ end
366
+ row(:baz) {}
367
+ end
368
+ end
369
+ connection.searches.last[:"facet.query"].should be_a(String)
370
+ end
371
+
372
+ it 'ignores facet query with only empty rows' do
373
+ search do
374
+ facet :foo do
375
+ row(:bar) {}
376
+ end
377
+ end
378
+ connection.should_not have_last_search_with(:"facet.query")
379
+ end
380
+
381
+ it 'does not allow 0 arguments to facet method with block' do
382
+ lambda do
383
+ search do
384
+ facet do
385
+ end
386
+ end
387
+ end.should raise_error(ArgumentError)
388
+ end
389
+
390
+ it 'does not allow more than 1 argument to facet method with block' do
391
+ lambda do
392
+ search do
393
+ facet :foo, :bar do
394
+ end
395
+ end
396
+ end.should raise_error(ArgumentError)
397
+ end
398
+ end
399
+ end