honkster-acts_as_solr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGE_LOG +233 -0
  2. data/LICENSE +19 -0
  3. data/README.markdown +118 -0
  4. data/README.rdoc +107 -0
  5. data/Rakefile +68 -0
  6. data/TESTING_THE_PLUGIN +25 -0
  7. data/VERSION +1 -0
  8. data/lib/acts_as_solr/acts_methods.rb +279 -0
  9. data/lib/acts_as_solr/class_methods.rb +236 -0
  10. data/lib/acts_as_solr/common_methods.rb +89 -0
  11. data/lib/acts_as_solr/deprecation.rb +61 -0
  12. data/lib/acts_as_solr/instance_methods.rb +166 -0
  13. data/lib/acts_as_solr/lazy_document.rb +18 -0
  14. data/lib/acts_as_solr/parser_methods.rb +201 -0
  15. data/lib/acts_as_solr/search_results.rb +68 -0
  16. data/lib/acts_as_solr/solr_fixtures.rb +13 -0
  17. data/lib/acts_as_solr/tasks/database.rake +16 -0
  18. data/lib/acts_as_solr/tasks/solr.rake +132 -0
  19. data/lib/acts_as_solr/tasks/test.rake +5 -0
  20. data/lib/acts_as_solr/tasks.rb +10 -0
  21. data/lib/acts_as_solr.rb +64 -0
  22. data/lib/solr/connection.rb +177 -0
  23. data/lib/solr/document.rb +75 -0
  24. data/lib/solr/exception.rb +13 -0
  25. data/lib/solr/field.rb +36 -0
  26. data/lib/solr/importer/array_mapper.rb +26 -0
  27. data/lib/solr/importer/delimited_file_source.rb +38 -0
  28. data/lib/solr/importer/hpricot_mapper.rb +27 -0
  29. data/lib/solr/importer/mapper.rb +51 -0
  30. data/lib/solr/importer/solr_source.rb +41 -0
  31. data/lib/solr/importer/xpath_mapper.rb +35 -0
  32. data/lib/solr/importer.rb +19 -0
  33. data/lib/solr/indexer.rb +52 -0
  34. data/lib/solr/request/add_document.rb +58 -0
  35. data/lib/solr/request/base.rb +36 -0
  36. data/lib/solr/request/commit.rb +29 -0
  37. data/lib/solr/request/delete.rb +48 -0
  38. data/lib/solr/request/dismax.rb +46 -0
  39. data/lib/solr/request/index_info.rb +22 -0
  40. data/lib/solr/request/modify_document.rb +46 -0
  41. data/lib/solr/request/optimize.rb +19 -0
  42. data/lib/solr/request/ping.rb +36 -0
  43. data/lib/solr/request/select.rb +54 -0
  44. data/lib/solr/request/spellcheck.rb +30 -0
  45. data/lib/solr/request/standard.rb +402 -0
  46. data/lib/solr/request/update.rb +23 -0
  47. data/lib/solr/request.rb +26 -0
  48. data/lib/solr/response/add_document.rb +17 -0
  49. data/lib/solr/response/base.rb +42 -0
  50. data/lib/solr/response/commit.rb +15 -0
  51. data/lib/solr/response/delete.rb +13 -0
  52. data/lib/solr/response/dismax.rb +8 -0
  53. data/lib/solr/response/index_info.rb +26 -0
  54. data/lib/solr/response/modify_document.rb +17 -0
  55. data/lib/solr/response/optimize.rb +14 -0
  56. data/lib/solr/response/ping.rb +26 -0
  57. data/lib/solr/response/ruby.rb +42 -0
  58. data/lib/solr/response/select.rb +17 -0
  59. data/lib/solr/response/spellcheck.rb +20 -0
  60. data/lib/solr/response/standard.rb +60 -0
  61. data/lib/solr/response/xml.rb +39 -0
  62. data/lib/solr/response.rb +27 -0
  63. data/lib/solr/solrtasks.rb +27 -0
  64. data/lib/solr/util.rb +32 -0
  65. data/lib/solr/xml.rb +44 -0
  66. data/lib/solr.rb +26 -0
  67. data/solr/CHANGES.txt +1207 -0
  68. data/solr/LICENSE.txt +712 -0
  69. data/solr/NOTICE.txt +90 -0
  70. data/solr/etc/jetty.xml +205 -0
  71. data/solr/etc/webdefault.xml +379 -0
  72. data/solr/lib/easymock.jar +0 -0
  73. data/solr/lib/jetty-6.1.3.jar +0 -0
  74. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  75. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  76. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  77. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  78. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  79. data/solr/lib/servlet-api-2.4.jar +0 -0
  80. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  81. data/solr/lib/xpp3-1.1.3.4.O.jar +0 -0
  82. data/solr/solr/README.txt +52 -0
  83. data/solr/solr/bin/abc +176 -0
  84. data/solr/solr/bin/abo +176 -0
  85. data/solr/solr/bin/backup +108 -0
  86. data/solr/solr/bin/backupcleaner +142 -0
  87. data/solr/solr/bin/commit +128 -0
  88. data/solr/solr/bin/optimize +129 -0
  89. data/solr/solr/bin/readercycle +129 -0
  90. data/solr/solr/bin/rsyncd-disable +77 -0
  91. data/solr/solr/bin/rsyncd-enable +76 -0
  92. data/solr/solr/bin/rsyncd-start +145 -0
  93. data/solr/solr/bin/rsyncd-stop +105 -0
  94. data/solr/solr/bin/scripts-util +83 -0
  95. data/solr/solr/bin/snapcleaner +148 -0
  96. data/solr/solr/bin/snapinstaller +168 -0
  97. data/solr/solr/bin/snappuller +248 -0
  98. data/solr/solr/bin/snappuller-disable +77 -0
  99. data/solr/solr/bin/snappuller-enable +77 -0
  100. data/solr/solr/bin/snapshooter +109 -0
  101. data/solr/solr/conf/admin-extra.html +31 -0
  102. data/solr/solr/conf/protwords.txt +21 -0
  103. data/solr/solr/conf/schema.xml +126 -0
  104. data/solr/solr/conf/scripts.conf +24 -0
  105. data/solr/solr/conf/solrconfig.xml +458 -0
  106. data/solr/solr/conf/stopwords.txt +57 -0
  107. data/solr/solr/conf/synonyms.txt +31 -0
  108. data/solr/solr/conf/xslt/example.xsl +132 -0
  109. data/solr/solr/conf/xslt/example_atom.xsl +63 -0
  110. data/solr/solr/conf/xslt/example_rss.xsl +62 -0
  111. data/solr/start.jar +0 -0
  112. data/solr/webapps/solr.war +0 -0
  113. data/test/config/solr.yml +2 -0
  114. data/test/db/connections/mysql/connection.rb +10 -0
  115. data/test/db/connections/sqlite/connection.rb +8 -0
  116. data/test/db/migrate/001_create_books.rb +15 -0
  117. data/test/db/migrate/002_create_movies.rb +12 -0
  118. data/test/db/migrate/003_create_categories.rb +11 -0
  119. data/test/db/migrate/004_create_electronics.rb +16 -0
  120. data/test/db/migrate/005_create_authors.rb +12 -0
  121. data/test/db/migrate/006_create_postings.rb +9 -0
  122. data/test/db/migrate/007_create_posts.rb +13 -0
  123. data/test/db/migrate/008_create_gadgets.rb +11 -0
  124. data/test/fixtures/authors.yml +9 -0
  125. data/test/fixtures/books.yml +13 -0
  126. data/test/fixtures/categories.yml +7 -0
  127. data/test/fixtures/db_definitions/mysql.sql +41 -0
  128. data/test/fixtures/electronics.yml +49 -0
  129. data/test/fixtures/movies.yml +9 -0
  130. data/test/fixtures/postings.yml +10 -0
  131. data/test/functional/acts_as_solr_test.rb +413 -0
  132. data/test/functional/association_indexing_test.rb +37 -0
  133. data/test/functional/faceted_search_test.rb +163 -0
  134. data/test/functional/multi_solr_search_test.rb +51 -0
  135. data/test/models/author.rb +10 -0
  136. data/test/models/book.rb +10 -0
  137. data/test/models/category.rb +8 -0
  138. data/test/models/electronic.rb +21 -0
  139. data/test/models/gadget.rb +9 -0
  140. data/test/models/movie.rb +17 -0
  141. data/test/models/novel.rb +2 -0
  142. data/test/models/post.rb +3 -0
  143. data/test/models/posting.rb +11 -0
  144. data/test/test_helper.rb +51 -0
  145. data/test/unit/acts_methods_shoulda.rb +68 -0
  146. data/test/unit/class_methods_shoulda.rb +85 -0
  147. data/test/unit/common_methods_shoulda.rb +111 -0
  148. data/test/unit/instance_methods_shoulda.rb +318 -0
  149. data/test/unit/lazy_document_shoulda.rb +34 -0
  150. data/test/unit/parser_instance.rb +19 -0
  151. data/test/unit/parser_methods_shoulda.rb +268 -0
  152. data/test/unit/solr_instance.rb +44 -0
  153. data/test/unit/test_helper.rb +22 -0
  154. metadata +239 -0
@@ -0,0 +1,36 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Base
14
+
15
+
16
+ #TODO : Add base support for the debugQuery flag, and such that the response provides debug output easily
17
+
18
+ # returns either :xml or :ruby depending on what the
19
+ # response type is for a given request
20
+
21
+ def response_format
22
+ raise "unknown request type: #{self.class}"
23
+ end
24
+
25
+ def content_type
26
+ 'text/xml; charset=utf-8'
27
+ end
28
+
29
+ # returns the solr handler or url fragment that can
30
+ # respond to this type of request
31
+
32
+ def handler
33
+ raise "unknown request type: #{self.class}"
34
+ end
35
+
36
+ end
@@ -0,0 +1,29 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Commit < Solr::Request::Update
14
+
15
+ def initialize(options={})
16
+ @wait_searcher = options[:wait_searcher] || true
17
+ @wait_flush = options[:wait_flush] || true
18
+ end
19
+
20
+
21
+ def to_s
22
+ e = Solr::XML::Element.new('commit')
23
+ e.attributes['waitSearcher'] = @wait_searcher ? 'true' : 'false'
24
+ e.attributes['waitFlush'] = @wait_flush ? 'true' : 'false'
25
+
26
+ e.to_s
27
+ end
28
+
29
+ end
@@ -0,0 +1,48 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Delete < Solr::Request::Update
14
+
15
+ # A delete request can be for a specific document id
16
+ #
17
+ # request = Solr::Request::Delete.new(:id => 1234)
18
+ #
19
+ # or by query:
20
+ #
21
+ # request = Solr::Request::Delete.new(:query =>
22
+ #
23
+ def initialize(options)
24
+ unless options.kind_of?(Hash) and (options[:id] or options[:query])
25
+ raise Solr::Exception.new("must pass in :id or :query")
26
+ end
27
+ if options[:id] and options[:query]
28
+ raise Solr::Exception.new("can't pass in both :id and :query")
29
+ end
30
+ @document_id = options[:id]
31
+ @query = options[:query]
32
+ end
33
+
34
+ def to_s
35
+ delete_element = Solr::XML::Element.new('delete')
36
+ if @document_id
37
+ id_element = Solr::XML::Element.new('id')
38
+ id_element.text = @document_id
39
+ delete_element.add_element(id_element)
40
+ elsif @query
41
+ query = Solr::XML::Element.new('query')
42
+ query.text = @query
43
+ delete_element.add_element(query)
44
+ end
45
+ delete_element.to_s
46
+ end
47
+ end
48
+
@@ -0,0 +1,46 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Dismax < Solr::Request::Standard
14
+
15
+ VALID_PARAMS.replace(VALID_PARAMS + [:tie_breaker, :query_fields, :minimum_match, :phrase_fields, :phrase_slop,
16
+ :boost_query, :boost_functions])
17
+
18
+ def initialize(params)
19
+ @alternate_query = params.delete(:alternate_query)
20
+ @sort_values = params.delete(:sort)
21
+
22
+ super
23
+
24
+ @query_type = "dismax"
25
+ end
26
+
27
+ def to_hash
28
+ hash = super
29
+ hash[:tie] = @params[:tie_breaker]
30
+ hash[:mm] = @params[:minimum_match]
31
+ hash[:qf] = @params[:query_fields]
32
+ hash[:pf] = @params[:phrase_fields]
33
+ hash[:ps] = @params[:phrase_slop]
34
+ hash[:bq] = @params[:boost_query]
35
+ hash[:bf] = @params[:boost_functions]
36
+ hash["q.alt"] = @alternate_query
37
+ # FIXME: 2007-02-13 <coda.hale@gmail.com> -- This code is duplicated in
38
+ # Solr::Request::Standard. It should be refactored into a single location.
39
+ hash[:sort] = @sort_values.collect do |sort|
40
+ key = sort.keys[0]
41
+ "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"
42
+ end.join(',') if @sort_values
43
+ return hash
44
+ end
45
+
46
+ end
@@ -0,0 +1,22 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::IndexInfo < Solr::Request::Select
14
+
15
+ def handler
16
+ 'admin/luke'
17
+ end
18
+
19
+ def to_hash
20
+ {:numTerms => 0}.merge(super.to_hash)
21
+ end
22
+ end
@@ -0,0 +1,46 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::ModifyDocument < Solr::Request::Update
14
+
15
+ # Example: ModifyDocument.new(:id => 10, :overwrite => {:field_name => "new value"})
16
+ def initialize(update_data)
17
+ modes = []
18
+ @doc = {}
19
+ [:overwrite, :append, :distinct, :increment, :delete].each do |mode|
20
+ field_data = update_data[mode]
21
+ if field_data
22
+ field_data.each do |field_name, field_value|
23
+ modes << "#{field_name}:#{mode.to_s.upcase}"
24
+ @doc[field_name] = field_value if field_value # if value is nil, omit so it can be removed
25
+ end
26
+ update_data.delete mode
27
+ end
28
+ end
29
+ @mode = modes.join(",")
30
+
31
+ # only one key should be left over, the id
32
+ @doc[update_data.keys[0].to_s] = update_data.values[0]
33
+ end
34
+
35
+ # returns the request as a string suitable for posting
36
+ def to_s
37
+ e = Solr::XML::Element.new 'add'
38
+ e.add_element(Solr::Document.new(@doc).to_xml)
39
+ return e.to_s
40
+ end
41
+
42
+ def handler
43
+ "update?mode=#{@mode}"
44
+ end
45
+
46
+ end
@@ -0,0 +1,19 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Optimize < Solr::Request::Update
14
+
15
+ def to_s
16
+ Solr::XML::Element.new('optimize').to_s
17
+ end
18
+
19
+ end
@@ -0,0 +1,36 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ # TODO: Consider something lazy like this?
14
+ # Solr::Request::Ping = Solr::Request.simple_request :format=>:xml, :handler=>'admin/ping'
15
+ # class Solr::Request
16
+ # def self.simple_request(options)
17
+ # Class.new do
18
+ # def response_format
19
+ # options[:format]
20
+ # end
21
+ # def handler
22
+ # options[:handler]
23
+ # end
24
+ # end
25
+ # end
26
+ # end
27
+
28
+ class Solr::Request::Ping < Solr::Request::Base
29
+ def response_format
30
+ :xml
31
+ end
32
+
33
+ def handler
34
+ 'admin/ping'
35
+ end
36
+ end
@@ -0,0 +1,54 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ # "Abstract" base class, only useful with subclasses that add parameters
14
+ class Solr::Request::Select < Solr::Request::Base
15
+
16
+ attr_reader :query_type
17
+
18
+ def initialize(qt=nil, params={})
19
+ @query_type = qt
20
+ @select_params = params
21
+ end
22
+
23
+ def response_format
24
+ :ruby
25
+ end
26
+
27
+ def handler
28
+ 'select'
29
+ end
30
+
31
+ def content_type
32
+ 'application/x-www-form-urlencoded; charset=utf-8'
33
+ end
34
+
35
+ def to_hash
36
+ return {:qt => query_type, :wt => 'ruby'}.merge(@select_params)
37
+ end
38
+
39
+ def to_s
40
+ raw_params = self.to_hash
41
+
42
+ http_params = []
43
+ raw_params.each do |key,value|
44
+ if value.respond_to? :each
45
+ value.each { |v| http_params << "#{key}=#{ERB::Util::url_encode(v)}" unless v.nil?}
46
+ else
47
+ http_params << "#{key}=#{ERB::Util::url_encode(value)}" unless value.nil?
48
+ end
49
+ end
50
+
51
+ http_params.join("&")
52
+ end
53
+
54
+ end
@@ -0,0 +1,30 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Spellcheck < Solr::Request::Select
14
+
15
+ def initialize(params)
16
+ super('spellchecker')
17
+ @params = params
18
+ end
19
+
20
+ def to_hash
21
+ hash = super
22
+ hash[:q] = @params[:query]
23
+ hash[:suggestionCount] = @params[:suggestion_count]
24
+ hash[:accuracy] = @params[:accuracy]
25
+ hash[:onlyMorePopular] = @params[:only_more_popular]
26
+ hash[:cmd] = @params[:command]
27
+ return hash
28
+ end
29
+
30
+ end
@@ -0,0 +1,402 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Standard < Solr::Request::Select
14
+
15
+ VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows, :shards, :date_facets,
16
+ :filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting, :mlt]
17
+
18
+ def initialize(params)
19
+ super('standard')
20
+
21
+ raise "Invalid parameters: #{(params.keys - VALID_PARAMS).join(',')}" unless
22
+ (params.keys - VALID_PARAMS).empty?
23
+
24
+ raise ":query parameter required" unless params[:query]
25
+
26
+ @params = params.dup
27
+
28
+ # Validate operator
29
+ if params[:operator]
30
+ raise "Only :and/:or operators allowed" unless
31
+ [:and, :or].include?(params[:operator])
32
+
33
+ @params[:operator] = params[:operator].to_s.upcase
34
+ end
35
+
36
+ # Validate start, rows can be transformed to ints
37
+ @params[:start] = params[:start].to_i if params[:start]
38
+ @params[:rows] = params[:rows].to_i if params[:rows]
39
+
40
+ @params[:field_list] ||= ["*","score"]
41
+
42
+ @params[:shards] ||= []
43
+ end
44
+
45
+ def to_hash
46
+ hash = {}
47
+
48
+ # standard request param processing
49
+ sort = @params[:sort].collect do |sort|
50
+ key = sort.keys[0]
51
+ "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"
52
+ end.join(',') if @params[:sort]
53
+ hash[:q] = sort ? "#{@params[:query]};#{sort}" : @params[:query]
54
+ hash["q.op"] = @params[:operator]
55
+ hash[:df] = @params[:default_field]
56
+
57
+ # common parameter processing
58
+ hash[:start] = @params[:start]
59
+ hash[:rows] = @params[:rows]
60
+ hash[:fq] = @params[:filter_queries]
61
+ hash[:fl] = @params[:field_list].join(',')
62
+ hash[:debugQuery] = @params[:debug_query]
63
+ hash[:explainOther] = @params[:explain_other]
64
+ hash[:shards] = @params[:shards].join(',') unless @params[:shards].empty?
65
+
66
+ # facet parameter processing
67
+ if @params[:facets]
68
+ # TODO need validation of all that is under the :facets Hash too
69
+ hash[:facet] = true
70
+ hash["facet.field"] = []
71
+ hash["facet.query"] = @params[:facets][:queries]
72
+ hash["facet.sort"] = (@params[:facets][:sort] == :count) if @params[:facets][:sort]
73
+ hash["facet.limit"] = @params[:facets][:limit]
74
+ hash["facet.missing"] = @params[:facets][:missing]
75
+ hash["facet.mincount"] = @params[:facets][:mincount]
76
+ hash["facet.prefix"] = @params[:facets][:prefix]
77
+ hash["facet.offset"] = @params[:facets][:offset]
78
+ if @params[:facets][:fields] # facet fields are optional (could be facet.query only)
79
+ @params[:facets][:fields].each do |f|
80
+ if f.kind_of? Hash
81
+ key = f.keys[0]
82
+ value = f[key]
83
+ hash["facet.field"] << key
84
+ hash["f.#{key}.facet.sort"] = (value[:sort] == :count) if value[:sort]
85
+ hash["f.#{key}.facet.limit"] = value[:limit]
86
+ hash["f.#{key}.facet.missing"] = value[:missing]
87
+ hash["f.#{key}.facet.mincount"] = value[:mincount]
88
+ hash["f.#{key}.facet.prefix"] = value[:prefix]
89
+ hash["f.#{key}.facet.offset"] = value[:offset]
90
+ else
91
+ hash["facet.field"] << f
92
+ end
93
+ end
94
+ end
95
+
96
+ if @params[:date_facets]
97
+ hash["facet.date"] = []
98
+ if @params[:date_facets][:fields]
99
+ @params[:date_facets][:fields].each do |f|
100
+ if f.kind_of? Hash
101
+ key = f.keys[0]
102
+ hash["facet.date"] << key
103
+ f[key].each { |k, v|
104
+ hash["f.#{key}.facet.date.#{k}"] = v
105
+ }
106
+ else
107
+ hash["facet.date"] << f
108
+ end
109
+ end
110
+ end
111
+ hash["facet.date.start"] = @params[:date_facets][:start]
112
+ hash["facet.date.end"] = @params[:date_facets][:end]
113
+ hash["facet.date.gap"] = @params[:date_facets][:gap]
114
+ hash["facet.date.other"] = @params[:date_facets][:other]
115
+ hash["facet.date.hardend"] = @params[:date_facets][:hardend]
116
+ if @params[:date_facets][:filter]
117
+ if hash[:fq]
118
+ hash[:fq] << @params[:date_facets][:filter]
119
+ else
120
+ hash[:fq] = @params[:date_facets][:filter]
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ # highlighting parameter processing - http://wiki.apache.org/solr/HighlightingParameters
127
+ if @params[:highlighting]
128
+ hash[:hl] = true
129
+ hash["hl.fl"] = @params[:highlighting][:field_list].join(',') if @params[:highlighting][:field_list]
130
+
131
+ snippets = @params[:highlighting][:max_snippets]
132
+ if snippets
133
+ if snippets.kind_of? Hash
134
+ if snippets[:default]
135
+ hash["hl.snippets"] = snippets[:default]
136
+ end
137
+ if snippets[:fields]
138
+ snippets[:fields].each do |k,v|
139
+ hash["f.#{k}.hl.snippets"] = v
140
+ end
141
+ end
142
+ else
143
+ hash["hl.snippets"] = snippets
144
+ end
145
+ end
146
+
147
+ fragsize = @params[:highlighting][:fragment_size]
148
+ if fragsize
149
+ if fragsize.kind_of? Hash
150
+ if fragsize[:default]
151
+ hash["hl.fragsize"] = fragsize[:default]
152
+ end
153
+ if fragsize[:fields]
154
+ fragsize[:fields].each do |k,v|
155
+ hash["f.#{k}.hl.fragsize"] = v
156
+ end
157
+ end
158
+ else
159
+ hash["hl.fragsize"] = fragsize
160
+ end
161
+ end
162
+
163
+ rfm = @params[:highlighting][:require_field_match]
164
+ if nil != rfm
165
+ if rfm.kind_of? Hash
166
+ if nil != rfm[:default]
167
+ hash["hl.requireFieldMatch"] = rfm[:default]
168
+ end
169
+ if rfm[:fields]
170
+ rfm[:fields].each do |k,v|
171
+ hash["f.#{k}.hl.requireFieldMatch"] = v
172
+ end
173
+ end
174
+ else
175
+ hash["hl.requireFieldMatch"] = rfm
176
+ end
177
+ end
178
+
179
+ mac = @params[:highlighting][:max_analyzed_chars]
180
+ if mac
181
+ if mac.kind_of? Hash
182
+ if mac[:default]
183
+ hash["hl.maxAnalyzedChars"] = mac[:default]
184
+ end
185
+ if mac[:fields]
186
+ mac[:fields].each do |k,v|
187
+ hash["f.#{k}.hl.maxAnalyzedChars"] = v
188
+ end
189
+ end
190
+ else
191
+ hash["hl.maxAnalyzedChars"] = mac
192
+ end
193
+ end
194
+
195
+ prefix = @params[:highlighting][:prefix]
196
+ if prefix
197
+ if prefix.kind_of? Hash
198
+ if prefix[:default]
199
+ hash["hl.simple.pre"] = prefix[:default]
200
+ end
201
+ if prefix[:fields]
202
+ prefix[:fields].each do |k,v|
203
+ hash["f.#{k}.hl.simple.pre"] = v
204
+ end
205
+ end
206
+ else
207
+ hash["hl.simple.pre"] = prefix
208
+ end
209
+ end
210
+
211
+ suffix = @params[:highlighting][:suffix]
212
+ if suffix
213
+ if suffix.kind_of? Hash
214
+ if suffix[:default]
215
+ hash["hl.simple.post"] = suffix[:default]
216
+ end
217
+ if suffix[:fields]
218
+ suffix[:fields].each do |k,v|
219
+ hash["f.#{k}.hl.simple.post"] = v
220
+ end
221
+ end
222
+ else
223
+ hash["hl.simple.post"] = suffix
224
+ end
225
+ end
226
+
227
+ formatter = @params[:highlighting][:formatter]
228
+ if formatter
229
+ if formatter.kind_of? Hash
230
+ if formatter[:default]
231
+ hash["hl.formatter"] = formatter[:default]
232
+ end
233
+ if formatter[:fields]
234
+ formatter[:fields].each do |k,v|
235
+ hash["f.#{k}.hl.formatter"] = v
236
+ end
237
+ end
238
+ else
239
+ hash["hl.formatter"] = formatter
240
+ end
241
+ end
242
+
243
+ fragmenter = @params[:highlighting][:fragmenter]
244
+ if fragmenter
245
+ if fragmenter.kind_of? Hash
246
+ if fragmenter[:default]
247
+ hash["hl.fragmenter"] = fragmenter[:default]
248
+ end
249
+ if fragmenter[:fields]
250
+ fragmenter[:fields].each do |k,v|
251
+ hash["f.#{k}.hl.fragmenter"] = v
252
+ end
253
+ end
254
+ else
255
+ hash["hl.fragmenter"] = fragmenter
256
+ end
257
+ end
258
+
259
+ merge_contiguous = @params[:highlighting][:merge_contiguous]
260
+ if nil != merge_contiguous
261
+ if merge_contiguous.kind_of? Hash
262
+ if nil != merge_contiguous[:default]
263
+ hash["hl.mergeContiguous"] = merge_contiguous[:default]
264
+ end
265
+ if merge_contiguous[:fields]
266
+ merge_contiguous[:fields].each do |k,v|
267
+ hash["f.#{k}.hl.mergeContiguous"] = v
268
+ end
269
+ end
270
+ else
271
+ hash["hl.mergeContiguous"] = merge_contiguous
272
+ end
273
+ end
274
+
275
+ increment = @params[:highlighting][:increment]
276
+ if increment
277
+ if increment.kind_of? Hash
278
+ if increment[:default]
279
+ hash["hl.increment"] = increment[:default]
280
+ end
281
+ if increment[:fields]
282
+ increment[:fields].each do |k,v|
283
+ hash["f.#{k}.hl.increment"] = v
284
+ end
285
+ end
286
+ else
287
+ hash["hl.increment"] = increment
288
+ end
289
+ end
290
+
291
+ # support "old style"
292
+ alternate_fields = @params[:highlighting][:alternate_fields]
293
+ if alternate_fields
294
+ alternate_fields.each do |f,v|
295
+ hash["f.#{f}.hl.alternateField"] = v
296
+ end
297
+ end
298
+
299
+ alternate_field = @params[:highlighting][:alternate_field]
300
+ if alternate_field
301
+ if alternate_field.kind_of? Hash
302
+ if alternate_field[:default]
303
+ hash["hl.alternateField"] = alternate_field[:default]
304
+ end
305
+ if alternate_field[:fields]
306
+ alternate_field[:fields].each do |k,v|
307
+ hash["f.#{k}.hl.alternateField"] = v
308
+ end
309
+ end
310
+ else
311
+ hash["hl.alternateField"] = alternate_field
312
+ end
313
+ end
314
+
315
+ mafl = @params[:highlighting][:max_alternate_field_length]
316
+ if mafl
317
+ if mafl.kind_of? Hash
318
+ if mafl[:default]
319
+ hash["hl.maxAlternateFieldLength"] = mafl[:default]
320
+ end
321
+ if mafl[:fields]
322
+ mafl[:fields].each do |k,v|
323
+ hash["f.#{k}.hl.maxAlternateFieldLength"] = v
324
+ end
325
+ else
326
+ # support "old style"
327
+ mafl.each do |k,v|
328
+ hash["f.#{k}.hl.maxAlternateFieldLength"] = v
329
+ end
330
+ end
331
+ else
332
+ hash["hl.maxAlternateFieldLength"] = mafl
333
+ end
334
+ end
335
+
336
+ hash["hl.usePhraseHighlighter"] = @params[:highlighting][:use_phrase_highlighter]
337
+
338
+ regex = @params[:highlighting][:regex]
339
+ if regex
340
+ if regex[:slop]
341
+ if regex[:slop].kind_of? Hash
342
+ if regex[:slop][:default]
343
+ hash["hl.regex.slop"] = regex[:slop][:default]
344
+ end
345
+ if regex[:slop][:fields]
346
+ regex[:slop][:fields].each do |k,v|
347
+ hash["f.#{k}.hl.regex.slop"] = v
348
+ end
349
+ end
350
+ else
351
+ hash["hl.regex.slop"] = regex[:slop]
352
+ end
353
+ end
354
+ if regex[:pattern]
355
+ if regex[:pattern].kind_of? Hash
356
+ if regex[:pattern][:default]
357
+ hash["hl.regex.pattern"] = regex[:pattern][:default]
358
+ end
359
+ if regex[:pattern][:fields]
360
+ regex[:pattern][:fields].each do |k,v|
361
+ hash["f.#{k}.hl.regex.pattern"] = v
362
+ end
363
+ end
364
+ else
365
+ hash["hl.regex.pattern"] = regex[:pattern]
366
+ end
367
+ end
368
+ if regex[:max_analyzed_chars]
369
+ if regex[:max_analyzed_chars].kind_of? Hash
370
+ if regex[:max_analyzed_chars][:default]
371
+ hash["hl.regex.maxAnalyzedChars"] = regex[:max_analyzed_chars][:default]
372
+ end
373
+ if regex[:max_analyzed_chars][:fields]
374
+ regex[:max_analyzed_chars][:fields].each do |k,v|
375
+ hash["f.#{k}.hl.regex.maxAnalyzedChars"] = v
376
+ end
377
+ end
378
+ else
379
+ hash["hl.regex.maxAnalyzedChars"] = regex[:max_analyzed_chars]
380
+ end
381
+ end
382
+ end
383
+
384
+ end
385
+
386
+ if @params[:mlt]
387
+ hash[:mlt] = true
388
+ hash["mlt.count"] = @params[:mlt][:count]
389
+ hash["mlt.fl"] = @params[:mlt][:field_list].join(',')
390
+ hash["mlt.mintf"] = @params[:mlt][:min_term_freq]
391
+ hash["mlt.mindf"] = @params[:mlt][:min_doc_freq]
392
+ hash["mlt.minwl"] = @params[:mlt][:min_word_length]
393
+ hash["mlt.maxwl"] = @params[:mlt][:max_word_length]
394
+ hash["mlt.maxqt"] = @params[:mlt][:max_query_terms]
395
+ hash["mlt.maxntp"] = @params[:mlt][:max_tokens_parsed]
396
+ hash["mlt.boost"] = @params[:mlt][:boost]
397
+ end
398
+
399
+ hash.merge(super.to_hash)
400
+ end
401
+
402
+ end