ruben-sunspot 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +22 -0
  3. data/History.txt +198 -0
  4. data/LICENSE +18 -0
  5. data/README.rdoc +244 -0
  6. data/Rakefile +11 -0
  7. data/TODO +13 -0
  8. data/VERSION.yml +4 -0
  9. data/bin/sunspot-installer +19 -0
  10. data/bin/sunspot-solr +74 -0
  11. data/installer/config/schema.yml +95 -0
  12. data/lib/light_config.rb +40 -0
  13. data/lib/sunspot.rb +529 -0
  14. data/lib/sunspot/adapters.rb +265 -0
  15. data/lib/sunspot/composite_setup.rb +202 -0
  16. data/lib/sunspot/configuration.rb +46 -0
  17. data/lib/sunspot/data_extractor.rb +50 -0
  18. data/lib/sunspot/dsl.rb +5 -0
  19. data/lib/sunspot/dsl/adjustable.rb +47 -0
  20. data/lib/sunspot/dsl/field_query.rb +266 -0
  21. data/lib/sunspot/dsl/fields.rb +113 -0
  22. data/lib/sunspot/dsl/fulltext.rb +243 -0
  23. data/lib/sunspot/dsl/function.rb +14 -0
  24. data/lib/sunspot/dsl/functional.rb +41 -0
  25. data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
  26. data/lib/sunspot/dsl/paginatable.rb +28 -0
  27. data/lib/sunspot/dsl/query_facet.rb +36 -0
  28. data/lib/sunspot/dsl/restriction.rb +25 -0
  29. data/lib/sunspot/dsl/scope.rb +229 -0
  30. data/lib/sunspot/dsl/search.rb +30 -0
  31. data/lib/sunspot/dsl/standard_query.rb +125 -0
  32. data/lib/sunspot/field.rb +192 -0
  33. data/lib/sunspot/field_factory.rb +147 -0
  34. data/lib/sunspot/indexer.rb +131 -0
  35. data/lib/sunspot/installer.rb +31 -0
  36. data/lib/sunspot/installer/library_installer.rb +45 -0
  37. data/lib/sunspot/installer/schema_builder.rb +219 -0
  38. data/lib/sunspot/installer/solrconfig_updater.rb +106 -0
  39. data/lib/sunspot/installer/task_helper.rb +18 -0
  40. data/lib/sunspot/query.rb +10 -0
  41. data/lib/sunspot/query/abstract_field_facet.rb +52 -0
  42. data/lib/sunspot/query/boost_query.rb +24 -0
  43. data/lib/sunspot/query/common_query.rb +85 -0
  44. data/lib/sunspot/query/composite_fulltext.rb +31 -0
  45. data/lib/sunspot/query/connective.rb +195 -0
  46. data/lib/sunspot/query/date_field_facet.rb +14 -0
  47. data/lib/sunspot/query/dismax.rb +126 -0
  48. data/lib/sunspot/query/field_facet.rb +41 -0
  49. data/lib/sunspot/query/filter.rb +38 -0
  50. data/lib/sunspot/query/function_query.rb +52 -0
  51. data/lib/sunspot/query/highlighting.rb +55 -0
  52. data/lib/sunspot/query/local.rb +26 -0
  53. data/lib/sunspot/query/more_like_this.rb +60 -0
  54. data/lib/sunspot/query/more_like_this_query.rb +12 -0
  55. data/lib/sunspot/query/pagination.rb +38 -0
  56. data/lib/sunspot/query/query_facet.rb +16 -0
  57. data/lib/sunspot/query/restriction.rb +265 -0
  58. data/lib/sunspot/query/scope.rb +16 -0
  59. data/lib/sunspot/query/sort.rb +95 -0
  60. data/lib/sunspot/query/sort_composite.rb +33 -0
  61. data/lib/sunspot/query/standard_query.rb +20 -0
  62. data/lib/sunspot/query/text_field_boost.rb +17 -0
  63. data/lib/sunspot/schema.rb +151 -0
  64. data/lib/sunspot/search.rb +9 -0
  65. data/lib/sunspot/search/abstract_search.rb +302 -0
  66. data/lib/sunspot/search/date_facet.rb +35 -0
  67. data/lib/sunspot/search/facet_row.rb +27 -0
  68. data/lib/sunspot/search/field_facet.rb +88 -0
  69. data/lib/sunspot/search/highlight.rb +38 -0
  70. data/lib/sunspot/search/hit.rb +136 -0
  71. data/lib/sunspot/search/more_like_this_search.rb +31 -0
  72. data/lib/sunspot/search/query_facet.rb +62 -0
  73. data/lib/sunspot/search/standard_search.rb +21 -0
  74. data/lib/sunspot/server.rb +152 -0
  75. data/lib/sunspot/session.rb +252 -0
  76. data/lib/sunspot/session_proxy.rb +71 -0
  77. data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
  78. data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
  79. data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
  80. data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
  81. data/lib/sunspot/session_proxy/sharding_session_proxy.rb +215 -0
  82. data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
  83. data/lib/sunspot/setup.rb +360 -0
  84. data/lib/sunspot/text_field_setup.rb +29 -0
  85. data/lib/sunspot/type.rb +342 -0
  86. data/lib/sunspot/util.rb +253 -0
  87. data/lib/sunspot/version.rb +3 -0
  88. data/solr/etc/jetty.xml +214 -0
  89. data/solr/etc/webdefault.xml +379 -0
  90. data/solr/lib/jetty-6.1.3.jar +0 -0
  91. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  92. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  93. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  94. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  95. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  96. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  97. data/solr/solr/conf/admin-extra.html +31 -0
  98. data/solr/solr/conf/elevate.xml +36 -0
  99. data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
  100. data/solr/solr/conf/protwords.txt +21 -0
  101. data/solr/solr/conf/schema.xml +238 -0
  102. data/solr/solr/conf/scripts.conf +24 -0
  103. data/solr/solr/conf/solrconfig.xml +938 -0
  104. data/solr/solr/conf/spellings.txt +2 -0
  105. data/solr/solr/conf/stopwords.txt +58 -0
  106. data/solr/solr/conf/synonyms.txt +31 -0
  107. data/solr/solr/lib/lucene-spatial-2.9.1.jar +0 -0
  108. data/solr/solr/lib/solr-spatial-light-0.0.6.jar +0 -0
  109. data/solr/start.jar +0 -0
  110. data/solr/webapps/solr.war +0 -0
  111. data/spec/api/adapters_spec.rb +33 -0
  112. data/spec/api/binding_spec.rb +38 -0
  113. data/spec/api/indexer/attributes_spec.rb +149 -0
  114. data/spec/api/indexer/batch_spec.rb +46 -0
  115. data/spec/api/indexer/dynamic_fields_spec.rb +42 -0
  116. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  117. data/spec/api/indexer/fulltext_spec.rb +43 -0
  118. data/spec/api/indexer/removal_spec.rb +53 -0
  119. data/spec/api/indexer/spec_helper.rb +1 -0
  120. data/spec/api/indexer_spec.rb +14 -0
  121. data/spec/api/query/advanced_manipulation_examples.rb +35 -0
  122. data/spec/api/query/connectives_examples.rb +186 -0
  123. data/spec/api/query/dsl_spec.rb +18 -0
  124. data/spec/api/query/dynamic_fields_examples.rb +165 -0
  125. data/spec/api/query/faceting_examples.rb +399 -0
  126. data/spec/api/query/fulltext_examples.rb +315 -0
  127. data/spec/api/query/function_spec.rb +70 -0
  128. data/spec/api/query/highlighting_examples.rb +225 -0
  129. data/spec/api/query/local_examples.rb +38 -0
  130. data/spec/api/query/more_like_this_spec.rb +140 -0
  131. data/spec/api/query/ordering_pagination_examples.rb +97 -0
  132. data/spec/api/query/scope_examples.rb +263 -0
  133. data/spec/api/query/spec_helper.rb +1 -0
  134. data/spec/api/query/standard_spec.rb +28 -0
  135. data/spec/api/query/text_field_scoping_examples.rb +30 -0
  136. data/spec/api/query/types_spec.rb +20 -0
  137. data/spec/api/search/dynamic_fields_spec.rb +33 -0
  138. data/spec/api/search/faceting_spec.rb +356 -0
  139. data/spec/api/search/highlighting_spec.rb +69 -0
  140. data/spec/api/search/hits_spec.rb +149 -0
  141. data/spec/api/search/results_spec.rb +79 -0
  142. data/spec/api/search/search_spec.rb +23 -0
  143. data/spec/api/search/spec_helper.rb +1 -0
  144. data/spec/api/server_spec.rb +91 -0
  145. data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
  146. data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
  147. data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
  148. data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
  149. data/spec/api/session_proxy/spec_helper.rb +9 -0
  150. data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +50 -0
  151. data/spec/api/session_spec.rb +198 -0
  152. data/spec/api/spec_helper.rb +3 -0
  153. data/spec/api/sunspot_spec.rb +18 -0
  154. data/spec/ext.rb +11 -0
  155. data/spec/helpers/indexer_helper.rb +29 -0
  156. data/spec/helpers/query_helper.rb +38 -0
  157. data/spec/helpers/search_helper.rb +80 -0
  158. data/spec/integration/dynamic_fields_spec.rb +55 -0
  159. data/spec/integration/faceting_spec.rb +238 -0
  160. data/spec/integration/highlighting_spec.rb +22 -0
  161. data/spec/integration/indexing_spec.rb +33 -0
  162. data/spec/integration/keyword_search_spec.rb +317 -0
  163. data/spec/integration/local_search_spec.rb +91 -0
  164. data/spec/integration/more_like_this_spec.rb +43 -0
  165. data/spec/integration/scoped_search_spec.rb +349 -0
  166. data/spec/integration/spec_helper.rb +7 -0
  167. data/spec/integration/stored_fields_spec.rb +10 -0
  168. data/spec/integration/test_pagination.rb +32 -0
  169. data/spec/mocks/adapters.rb +32 -0
  170. data/spec/mocks/blog.rb +3 -0
  171. data/spec/mocks/comment.rb +21 -0
  172. data/spec/mocks/connection.rb +126 -0
  173. data/spec/mocks/mock_adapter.rb +30 -0
  174. data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
  175. data/spec/mocks/mock_record.rb +52 -0
  176. data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
  177. data/spec/mocks/photo.rb +12 -0
  178. data/spec/mocks/post.rb +76 -0
  179. data/spec/mocks/super_class.rb +2 -0
  180. data/spec/mocks/user.rb +8 -0
  181. data/spec/spec_helper.rb +52 -0
  182. data/tasks/gemspec.rake +33 -0
  183. data/tasks/rcov.rake +28 -0
  184. data/tasks/rdoc.rake +27 -0
  185. data/tasks/schema.rake +19 -0
  186. data/tasks/spec.rake +24 -0
  187. data/tasks/todo.rake +4 -0
  188. metadata +356 -0
@@ -0,0 +1,11 @@
1
+ ENV['RUBYOPT'] = '-W1'
2
+
3
+ task :environment do
4
+ require File.dirname(__FILE__) + '/lib/sunspot'
5
+ end
6
+
7
+ require File.join(File.dirname(__FILE__), 'lib', 'sunspot', 'version')
8
+
9
+ Dir['tasks/**/*.rake'].each { |t| load t }
10
+
11
+ task :default => 'spec:api'
data/TODO ADDED
@@ -0,0 +1,13 @@
1
+ === Sunspot 1.1.x ===
2
+ * commitWithin (needs support from RSolr, currently available in master)
3
+ * commit options (non-blocking, etc.)
4
+
5
+ === Someday ===
6
+ * Shorthand arguments to Sunspot#search
7
+ * Rudimentary fulltext prefixing support
8
+
9
+ === Solr 1.5 ===
10
+ * Field Collapsing (SOLR-236)
11
+ * Support for official spatial support (SOLR-773)
12
+ * Support sorting by function
13
+ * Support string constants in functions
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 10
3
+ :patch: 8
4
+ :major: 0
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'sunspot', 'installer')
5
+
6
+ home = ARGV.select { |arg| arg !~ /^-/ }.last || Dir.pwd
7
+ options = {}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: sunspot-install [options] [solr-home]"
11
+ opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
12
+ options[:verbose] = v
13
+ end
14
+ opts.on('-f', '--[no-]force', 'Overwrite schema and solrconfig files with Sunspot default, instead of modifying them (recommended for new Solr installation)') do |f|
15
+ options[:force] = f
16
+ end
17
+ end.parse!
18
+
19
+ Sunspot::Installer.execute(home, options)
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require 'fileutils'
4
+ require 'tempfile'
5
+ require 'tmpdir'
6
+ require 'optparse'
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'sunspot', 'server')
8
+ rescue LoadError => e
9
+ if require 'rubygems'
10
+ retry
11
+ else
12
+ raise(e)
13
+ end
14
+ end
15
+
16
+ server = Sunspot::Server.new
17
+
18
+ OptionParser.new do |opts|
19
+ opts.banner = "Usage: sunspot-solr start [options]"
20
+
21
+ opts.on '-p', '--port=PORT', 'Port on which to run Solr (default 8983)' do |p|
22
+ server.port = p
23
+ end
24
+
25
+ opts.on '-d', '--data-directory=DIRECTORY', 'Solr data directory' do |d|
26
+ server.solr_data_dir = File.expand_path(d)
27
+ end
28
+
29
+ opts.on '-s', '--solr-home=HOME', 'Solr home directory (should contain conf/ directory)' do |s|
30
+ server.solr_home = File.expand_path(s)
31
+ end
32
+
33
+ opts.on '-j', '--solr-jar=JAR', 'Solr start jar' do |j|
34
+ server.solr_jar = File.expand_path(j)
35
+ end
36
+
37
+ opts.on '--pid-dir=PID_DIR', 'Directory for pid files' do |pd|
38
+ server.pid_dir = File.expand_path(pd)
39
+ end
40
+
41
+ opts.on '-l', '--log-level=LOG_LEVEL', 'Solr logging level' do |l|
42
+ server.log_level = l
43
+ end
44
+
45
+ opts.on '--log-file=LOG_FILE', 'Path to Solr log file' do |lf|
46
+ server.log_file = File.expand_path(lf)
47
+ end
48
+
49
+ opts.on '--max-memory=MEMORY', 'Specify the maximum size of the memory allocation pool' do |mm|
50
+ server.max_memory = mm
51
+ end
52
+
53
+ opts.on '--min-memory=MEMORY', 'Specify the initial size of the memory allocation pool' do |mm|
54
+ server.min_memory = mm
55
+ end
56
+ end.parse!
57
+
58
+ begin
59
+ case ARGV[0]
60
+ when 'start'
61
+ server.start
62
+ when 'run'
63
+ server.run
64
+ when 'stop'
65
+ server.stop
66
+ when 'restart'
67
+ server.stop
68
+ server.start
69
+ else
70
+ abort("Usage: sunspot-solr (start|stop|run)")
71
+ end
72
+ rescue Sunspot::Server::ServerError => e
73
+ abort(e.message)
74
+ end
@@ -0,0 +1,95 @@
1
+ types: !omap
2
+ - text:
3
+ class: TextField
4
+ suffix: text
5
+ omit_norms: false
6
+ tokenizer: StandardTokenizerFactory
7
+ filters:
8
+ - StandardFilterFactory
9
+ - LowerCaseFilterFactory
10
+ invariants:
11
+ multiValued: true
12
+ - boolean:
13
+ class: BoolField
14
+ suffix: b
15
+ invariants:
16
+ termVectors: valse
17
+ - date:
18
+ class: DateField
19
+ suffix: d
20
+ invariants:
21
+ termVectors: valse
22
+ - rand:
23
+ class: RandomSortField
24
+ - sdouble:
25
+ class: SortableDoubleField
26
+ suffix: e
27
+ invariants:
28
+ termVectors: valse
29
+ - sfloat:
30
+ class: SortableFloatField
31
+ suffix: f
32
+ invariants:
33
+ termVectors: valse
34
+ - sint:
35
+ class: SortableIntField
36
+ suffix: i
37
+ invariants:
38
+ termVectors: valse
39
+ - slong:
40
+ class: SortableLongField
41
+ suffix: l
42
+ invariants:
43
+ termVectors: valse
44
+ - string:
45
+ class: StrField
46
+ suffix: s
47
+ invariants:
48
+ termVectors: valse
49
+ - tint:
50
+ class: TrieIntField
51
+ suffix: it
52
+ invariants:
53
+ termVectors: valse
54
+ - tfloat:
55
+ class: TrieFloatField
56
+ suffix: ft
57
+ invariants:
58
+ termVectors: valse
59
+ - tdouble:
60
+ class: TrieDoubleField
61
+ suffix: et
62
+ invariants:
63
+ termVectors: valse
64
+ - tdate:
65
+ class: TrieDateField
66
+ suffix: dt
67
+ invariants:
68
+ termVectors: valse
69
+ fixed: !omap
70
+ - id:
71
+ type: string
72
+ attributes: [stored]
73
+ - type:
74
+ type: string
75
+ attributes: [multiValued]
76
+ - class_name:
77
+ type: string
78
+ - text:
79
+ type: string
80
+ attributes: [multiValued]
81
+ - lat:
82
+ type: tdouble
83
+ attributes: [stored]
84
+ - lng:
85
+ type: tdouble
86
+ attributes: [stored]
87
+ - 'random_*':
88
+ type: rand
89
+ - '_local*':
90
+ type: tdouble
91
+
92
+ variants: !omap
93
+ - multiValued: m
94
+ - stored: s
95
+ - termVectors: v
@@ -0,0 +1,40 @@
1
+ module LightConfig
2
+ class Configuration
3
+ def initialize(&block)
4
+ @properties = {}
5
+ ::LightConfig::Builder.new(self).instance_eval(&block)
6
+ singleton = (class <<self; self; end)
7
+ @properties.keys.each do |property|
8
+ singleton.module_eval do
9
+ define_method property do
10
+ @properties[property]
11
+ end
12
+
13
+ define_method "#{property}=" do |value|
14
+ @properties[property] = value
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Builder
22
+ def initialize(configuration)
23
+ @configuration = configuration
24
+ end
25
+
26
+ def method_missing(method, *args, &block)
27
+ raise ArgumentError("wrong number of arguments(#{args.length} for 1)") unless args.length < 2
28
+ value = if block then ::LightConfig::Configuration.new(&block)
29
+ else args.first
30
+ end
31
+ @configuration.instance_variable_get(:@properties)[method] = value
32
+ end
33
+ end
34
+
35
+ class <<self
36
+ def build(&block)
37
+ LightConfig::Configuration.new(&block)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,529 @@
1
+ require 'set'
2
+ require 'time'
3
+ require 'date'
4
+ require 'enumerator'
5
+ require 'cgi'
6
+ begin
7
+ require 'rsolr'
8
+ rescue LoadError
9
+ require 'rubygems'
10
+ require 'rsolr'
11
+ end
12
+
13
+ require File.join(File.dirname(__FILE__), 'light_config')
14
+
15
+ %w(util adapters configuration setup composite_setup text_field_setup field
16
+ field_factory data_extractor indexer query search session session_proxy
17
+ type dsl).each do |filename|
18
+ require File.join(File.dirname(__FILE__), 'sunspot', filename)
19
+ end
20
+
21
+ #
22
+ # The Sunspot module provides class-method entry points to most of the
23
+ # functionality provided by the Sunspot library. Internally, the Sunspot
24
+ # singleton class contains a (non-thread-safe!) instance of Sunspot::Session,
25
+ # to which it delegates most of the class methods it exposes. In the method
26
+ # documentation below, this instance is referred to as the "singleton session".
27
+ #
28
+ # Though the singleton session provides a convenient entry point to Sunspot,
29
+ # it is by no means required to use the Sunspot class methods. Multiple sessions
30
+ # may be instantiated and used (if you need to connect to multiple Solr
31
+ # instances, for example.)
32
+ #
33
+ # Note that the configuration of classes for index/search (the +setup+
34
+ # method) is _not_ session-specific, but rather global.
35
+ #
36
+ module Sunspot
37
+ UnrecognizedFieldError = Class.new(StandardError)
38
+ UnrecognizedRestrictionError = Class.new(StandardError)
39
+ NoAdapterError = Class.new(StandardError)
40
+ NoSetupError = Class.new(StandardError)
41
+ IllegalSearchError = Class.new(StandardError)
42
+ NotImplementedError = Class.new(StandardError)
43
+
44
+ autoload :Server, File.join(File.dirname(__FILE__), 'sunspot', 'server')
45
+ autoload :Installer, File.join(File.dirname(__FILE__), 'sunspot', 'installer')
46
+
47
+ class <<self
48
+ #
49
+ # Clients can inject a session proxy, allowing them to implement custom
50
+ # session-management logic while retaining the Sunspot singleton API as
51
+ # an available interface. The object assigned to this attribute must
52
+ # respond to all of the public methods of the Sunspot::Session class.
53
+ #
54
+ attr_writer :session
55
+
56
+ # Configures indexing and search for a given class.
57
+ #
58
+ # ==== Parameters
59
+ #
60
+ # clazz<Class>:: class to configure
61
+ #
62
+ # ==== Example
63
+ #
64
+ # Sunspot.setup(Post) do
65
+ # text :title, :body
66
+ # string :author_name
67
+ # integer :blog_id
68
+ # integer :category_ids
69
+ # float :average_rating, :using => :ratings_average
70
+ # time :published_at
71
+ # string :sort_title do
72
+ # title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
73
+ # end
74
+ # end
75
+ #
76
+ # ====== Attribute Fields vs. Virtual Fields
77
+ #
78
+ # Attribute fields call a method on the indexed object and index the
79
+ # return value. All of the fields defined above except for the last one are
80
+ # attribute fields. By default, the field name will also be the attribute
81
+ # used; this can be overriden with the +:using+ option, as in
82
+ # +:average_rating+ above. In that case, the attribute +:ratings_average+
83
+ # will be indexed with the field name +:average_rating+.
84
+ #
85
+ # +:sort_title+ is a virtual field, which evaluates the block inside the
86
+ # context of the instance being indexed, and indexes the value returned
87
+ # by the block. If the block you pass takes an argument, it will be passed
88
+ # the instance rather than being evaluated inside of it; so, the following
89
+ # example is equivalent to the one above (assuming #title is public):
90
+ #
91
+ # Sunspot.setup(Post) do
92
+ # string :sort_title do |post|
93
+ # post.title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
94
+ # end
95
+ # end
96
+ #
97
+ # ===== Field Types
98
+ #
99
+ # The available types are:
100
+ #
101
+ # * +text+
102
+ # * +string+
103
+ # * +integer+
104
+ # * +float+
105
+ # * +time+
106
+ # * +boolean+
107
+ #
108
+ # Note that the +text+ type behaves quite differently from the others -
109
+ # this is the type that is indexed as fulltext, and is searched using the
110
+ # +keywords+ method inside the search DSL. Text fields cannot have
111
+ # restrictions set on them, nor can they be used in order statements or
112
+ # for facets. All other types are indexed literally, and thus can be used
113
+ # for all of those operations. They will not, however, be searched in
114
+ # fulltext. In this way, Sunspot provides a complete barrier between
115
+ # fulltext fields and value fields.
116
+ #
117
+ # It is fine to specify a field both as a text field and a string field;
118
+ # internally, the fields will have different names so there is no danger
119
+ # of conflict.
120
+ #
121
+ # ===== Dynamic Fields
122
+ #
123
+ # For use cases which have highly dynamic data models (for instance, an
124
+ # open set of key-value pairs attached to a model), it may be useful to
125
+ # defer definition of fields until indexing time. Sunspot exposes dynamic
126
+ # fields, which define a data accessor (either attribute or virtual, see
127
+ # above), which accepts a hash of field names to values. Note that the field
128
+ # names in the hash are internally scoped to the base name of the dynamic
129
+ # field, so any time they are referred to, they are referred to using both
130
+ # the base name and the dynamic (runtime-specified) name.
131
+ #
132
+ # Dynamic fields are speficied in the setup block using the type name
133
+ # prefixed by +dynamic_+. For example:
134
+ #
135
+ # Sunspot.setup(Post) do
136
+ # dynamic_string :custom_values do
137
+ # key_value_pairs.inject({}) do |hash, key_value_pair|
138
+ # hash[key_value_pair.key.to_sym] = key_value_pair.value
139
+ # end
140
+ # end
141
+ # end
142
+ #
143
+ # If you later wanted to facet all of the values for the key "cuisine",
144
+ # you could issue:
145
+ #
146
+ # Sunspot.search(Post) do
147
+ # dynamic :custom_values do
148
+ # facet :cuisine
149
+ # end
150
+ # end
151
+ #
152
+ # In the documentation, +:custom_values+ is referred to as the "base name" -
153
+ # that is, the one specified statically - and +:cuisine+ is referred to as
154
+ # the dynamic name, which is the part that is specified at indexing time.
155
+ #
156
+ def setup(clazz, &block)
157
+ Setup.setup(clazz, &block)
158
+ end
159
+
160
+ # Indexes objects on the singleton session.
161
+ #
162
+ # ==== Parameters
163
+ #
164
+ # objects...<Object>:: objects to index (may pass an array or varargs)
165
+ #
166
+ # ==== Example
167
+ #
168
+ # post1, post2 = new Array(2) { Post.create }
169
+ # Sunspot.index(post1, post2)
170
+ #
171
+ # Note that indexed objects won't be reflected in search until a commit is
172
+ # sent - see Sunspot.index! and Sunspot.commit
173
+ #
174
+ def index(*objects)
175
+ session.index(*objects)
176
+ end
177
+
178
+ # Indexes objects on the singleton session and commits immediately.
179
+ #
180
+ # See: Sunspot.index and Sunspot.commit
181
+ #
182
+ # ==== Parameters
183
+ #
184
+ # objects...<Object>:: objects to index (may pass an array or varargs)
185
+ #
186
+ def index!(*objects)
187
+ session.index!(*objects)
188
+ end
189
+
190
+ # Commits the singleton session
191
+ #
192
+ # When documents are added to or removed from Solr, the changes are
193
+ # initially stored in memory, and are not reflected in Solr's existing
194
+ # searcher instance. When a commit message is sent, the changes are written
195
+ # to disk, and a new searcher is spawned. Commits are thus fairly
196
+ # expensive, so if your application needs to index several documents as part
197
+ # of a single operation, it is advisable to index them all and then call
198
+ # commit at the end of the operation.
199
+ #
200
+ # Note that Solr can also be configured to automatically perform a commit
201
+ # after either a specified interval after the last change, or after a
202
+ # specified number of documents are added. See
203
+ # http://wiki.apache.org/solr/SolrConfigXml
204
+ #
205
+ def commit
206
+ session.commit
207
+ end
208
+
209
+ #
210
+ # Create a new Search instance, but do not execute it immediately. Generally
211
+ # you will want to use the #search method to build and execute searches in
212
+ # one step, but if you are building searches piecemeal you may call
213
+ # #new_search and then call #build one or more times to add components to
214
+ # the query.
215
+ #
216
+ # ==== Example
217
+ #
218
+ # search = Sunspot.new_search do
219
+ # with(:blog_id, 1)
220
+ # end
221
+ # search.build do
222
+ # keywords('some keywords')
223
+ # end
224
+ # search.build do
225
+ # order_by(:published_at, :desc)
226
+ # end
227
+ # search.execute
228
+ #
229
+ # # This is equivalent to:
230
+ # Sunspot.search do
231
+ # with(:blog_id, 1)
232
+ # keywords('some keywords')
233
+ # order_by(:published_at, :desc)
234
+ # end
235
+ #
236
+ # ==== Parameters
237
+ #
238
+ # types<Class>...::
239
+ # One or more types to search for. If no types are passed, all
240
+ # configured types will be searched for.
241
+ #
242
+ # ==== Returns
243
+ #
244
+ # Sunspot::Search::
245
+ # Search object, not yet executed. Query parameters can be added manually;
246
+ # then #execute should be called.
247
+ #
248
+ def new_search(*types, &block)
249
+ session.new_search(*types, &block)
250
+ end
251
+
252
+
253
+ # Search for objects in the index.
254
+ #
255
+ # ==== Parameters
256
+ #
257
+ # types<Class>...::
258
+ # One or more types to search for. If no types are passed, all
259
+ # configured types will be searched.
260
+ #
261
+ # ==== Returns
262
+ #
263
+ # Sunspot::Search:: Object containing results, facets, count, etc.
264
+ #
265
+ # The fields available for restriction, ordering, etc. are those that meet
266
+ # the following criteria:
267
+ #
268
+ # * They are not of type +text+.
269
+ # * They are defined for at least one of the classes being searched
270
+ # * They have the same data type for all of the classes being searched.
271
+ # * They have the same multiple flag for all of the classes being searched.
272
+ # * They have the same stored flag for all of the classes being searched.
273
+ #
274
+ # The restrictions available are the constants defined in the
275
+ # Sunspot::Restriction class. The standard restrictions are:
276
+ #
277
+ # with(:field_name).equal_to(value)
278
+ # with(:field_name, value) # shorthand for above
279
+ # with(:field_name).less_than(value)
280
+ # with(:field_name).greater_than(value)
281
+ # with(:field_name).between(value1..value2)
282
+ # with(:field_name).any_of([value1, value2, value3])
283
+ # with(:field_name).all_of([value1, value2, value3])
284
+ # without(some_instance) # exclude that particular instance
285
+ #
286
+ # +without+ can be substituted for +with+, causing the restriction to be
287
+ # negated. In the last example above, only +without+ works, as it does not
288
+ # make sense to search only for an instance you already have.
289
+ #
290
+ # Equality restrictions can take +nil+ as a value, which restricts the
291
+ # results to documents that have no value for the given field. Passing +nil+
292
+ # as a value to other restriction types is illegal. Thus:
293
+ #
294
+ # with(:field_name, nil) # ok
295
+ # with(:field_name).equal_to(nil) # ok
296
+ # with(:field_name).less_than(nil) # bad
297
+ #
298
+ # ==== Example
299
+ #
300
+ # Sunspot.search(Post) do
301
+ # keywords 'great pizza'
302
+ # with(:published_at).less_than Time.now
303
+ # with :blog_id, 1
304
+ # without current_post
305
+ # facet :category_ids
306
+ # order_by :published_at, :desc
307
+ # paginate 2, 15
308
+ # end
309
+ #
310
+ # If the block passed to #search takes an argument, that argument will
311
+ # present the DSL, and the block will be evaluated in the calling context.
312
+ # This will come in handy for building searches using instance data or
313
+ # methods, e.g.:
314
+ #
315
+ # Sunspot.search(Post) do |query|
316
+ # query.with(:blog_id, @current_blog.id)
317
+ # end
318
+ #
319
+ # See Sunspot::DSL::Search, Sunspot::DSL::Scope, Sunspot::DSL::FieldQuery
320
+ # and Sunspot::DSL::Query for the full API presented inside the block.
321
+ #
322
+ def search(*types, &block)
323
+ session.search(*types, &block)
324
+ end
325
+
326
+ def new_more_like_this(object, *types, &block)
327
+ session.new_more_like_this(object, *types, &block)
328
+ end
329
+
330
+ def more_like_this(object, *types, &block)
331
+ session.more_like_this(object, *types, &block)
332
+ end
333
+
334
+ # Remove objects from the index. Any time an object is destroyed, it must
335
+ # be removed from the index; otherwise, the index will contain broken
336
+ # references to objects that do not exist, which will cause errors when
337
+ # those objects are matched in search results.
338
+ #
339
+ # If a block is passed, it is evaluated as a search scope; in this way,
340
+ # documents can be removed by an arbitrary query. In this case, the
341
+ # arguments to the method should be the classes to run the query on.
342
+ #
343
+ # ==== Parameters
344
+ #
345
+ # objects...<Object>::
346
+ # Objects to remove from the index (may pass an array or varargs)
347
+ #
348
+ # ==== Example (remove a document)
349
+ #
350
+ # post.destroy
351
+ # Sunspot.remove(post)
352
+ #
353
+ # ==== Example (remove by query)
354
+ #
355
+ # Sunspot.remove(Post) do
356
+ # with(:created_at).less_than(Time.now - 14.days)
357
+ # end
358
+ #
359
+ def remove(*objects, &block)
360
+ session.remove(*objects, &block)
361
+ end
362
+
363
+ #
364
+ # Remove objects from the index and immediately commit. See Sunspot.remove
365
+ #
366
+ # ==== Parameters
367
+ #
368
+ # objects...<Object>:: Objects to remove from the index
369
+ #
370
+ def remove!(*objects)
371
+ session.remove!(*objects)
372
+ end
373
+
374
+ #
375
+ # Remove an object from the index using its class name and primary key.
376
+ # Useful if you know this information and want to remove an object without
377
+ # instantiating it from persistent storage
378
+ #
379
+ # ==== Parameters
380
+ #
381
+ # clazz<Class>:: Class of the object, or class name as a string or symbol
382
+ # id::
383
+ # Primary key of the object. This should be the same id that would be
384
+ # returned by the class's instance adapter.
385
+ #
386
+ def remove_by_id(clazz, id)
387
+ session.remove_by_id(clazz, id)
388
+ end
389
+
390
+ #
391
+ # Remove an object by class name and primary key, and immediately commit.
392
+ # See #remove_by_id and #commit
393
+ #
394
+ def remove_by_id!(clazz, id)
395
+ session.remove_by_id!(clazz, id)
396
+ end
397
+
398
+ # Remove all objects of the given classes from the index. There isn't much
399
+ # use for this in general operations but it can be useful for maintenance,
400
+ # testing, etc. If no arguments are passed, remove everything from the
401
+ # index.
402
+ #
403
+ # ==== Parameters
404
+ #
405
+ # classes...<Class>::
406
+ # classes for which to remove all instances from the index (may pass an
407
+ # array or varargs)
408
+ #
409
+ # ==== Example
410
+ #
411
+ # Sunspot.remove_all(Post, Blog)
412
+ #
413
+ def remove_all(*classes)
414
+ session.remove_all(*classes)
415
+ end
416
+
417
+ #
418
+ # Remove all objects of the given classes from the index and immediately
419
+ # commit. See Sunspot.remove_all
420
+ #
421
+ # ==== Parameters
422
+ #
423
+ # classes...<Class>::
424
+ # classes for which to remove all instances from the index
425
+ def remove_all!(*classes)
426
+ session.remove_all!(*classes)
427
+ end
428
+
429
+ #
430
+ # Process all adds in a batch. Any Sunspot adds initiated inside the block
431
+ # will be sent in bulk when the block finishes. Useful if your application
432
+ # initiates index adds from various places in code as part of a single
433
+ # operation; doing a batch add will give better performance.
434
+ #
435
+ # ==== Example
436
+ #
437
+ # Sunspot.batch do
438
+ # post = Post.new
439
+ # Sunspot.add(post)
440
+ # comment = Comment.new
441
+ # Sunspot.add(comment)
442
+ # end
443
+ #
444
+ # Sunspot will send both the post and the comment in a single request.
445
+ #
446
+ def batch(&block)
447
+ session.batch(&block)
448
+ end
449
+
450
+ #
451
+ # True if documents have been added, updated, or removed since the last
452
+ # commit.
453
+ #
454
+ # ==== Returns
455
+ #
456
+ # Boolean:: Whether there have been any updates since the last commit
457
+ #
458
+ def dirty?
459
+ session.dirty?
460
+ end
461
+
462
+ #
463
+ # Sends a commit if the session is dirty (see #dirty?).
464
+ #
465
+ def commit_if_dirty
466
+ session.commit_if_dirty
467
+ end
468
+
469
+ #
470
+ # True if documents have been removed since the last commit.
471
+ #
472
+ # ==== Returns
473
+ #
474
+ # Boolean:: Whether there have been any deletes since the last commit
475
+ #
476
+ def delete_dirty?
477
+ session.delete_dirty?
478
+ end
479
+
480
+ #
481
+ # Sends a commit if the session has deletes since the last commit (see #delete_dirty?).
482
+ #
483
+ def commit_if_delete_dirty
484
+ session.commit_if_delete_dirty
485
+ end
486
+
487
+ # Returns the configuration associated with the singleton session. See
488
+ # Sunspot::Configuration for details.
489
+ #
490
+ # ==== Returns
491
+ #
492
+ # LightConfig::Configuration:: configuration for singleton session
493
+ #
494
+ def config
495
+ session.config
496
+ end
497
+
498
+ #
499
+ # Resets the singleton session. This is useful for clearing out all
500
+ # static data between tests, but probably nowhere else.
501
+ #
502
+ # ==== Parameters
503
+ #
504
+ # keep_config<Boolean>::
505
+ # Whether to retain the configuration used by the current singleton
506
+ # session. Default false.
507
+ #
508
+ def reset!(keep_config = false)
509
+ config =
510
+ if keep_config
511
+ session.config
512
+ else
513
+ Configuration.build
514
+ end
515
+ @session = Session.new(config)
516
+ end
517
+
518
+ #
519
+ # Get the singleton session, creating it if none yet exists.
520
+ #
521
+ # ==== Returns
522
+ #
523
+ # Sunspot::Session:: the singleton session
524
+ #
525
+ def session #:nodoc:
526
+ @session ||= Session.new
527
+ end
528
+ end
529
+ end