benjaminkrause-sunspot 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/History.txt +107 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +159 -0
  4. data/Rakefile +9 -0
  5. data/TODO +11 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +46 -0
  8. data/bin/sunspot-solr +86 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot/adapters.rb +265 -0
  11. data/lib/sunspot/composite_setup.rb +184 -0
  12. data/lib/sunspot/configuration.rb +49 -0
  13. data/lib/sunspot/data_extractor.rb +50 -0
  14. data/lib/sunspot/dsl/field_query.rb +77 -0
  15. data/lib/sunspot/dsl/fields.rb +95 -0
  16. data/lib/sunspot/dsl/fulltext.rb +106 -0
  17. data/lib/sunspot/dsl/query.rb +107 -0
  18. data/lib/sunspot/dsl/query_facet.rb +31 -0
  19. data/lib/sunspot/dsl/restriction.rb +25 -0
  20. data/lib/sunspot/dsl/scope.rb +193 -0
  21. data/lib/sunspot/dsl/search.rb +30 -0
  22. data/lib/sunspot/dsl.rb +4 -0
  23. data/lib/sunspot/facet.rb +24 -0
  24. data/lib/sunspot/facet_data.rb +152 -0
  25. data/lib/sunspot/facet_row.rb +12 -0
  26. data/lib/sunspot/field.rb +148 -0
  27. data/lib/sunspot/field_factory.rb +141 -0
  28. data/lib/sunspot/indexer.rb +129 -0
  29. data/lib/sunspot/instantiated_facet.rb +45 -0
  30. data/lib/sunspot/instantiated_facet_row.rb +27 -0
  31. data/lib/sunspot/query/base_query.rb +55 -0
  32. data/lib/sunspot/query/boost_query.rb +20 -0
  33. data/lib/sunspot/query/connective.rb +148 -0
  34. data/lib/sunspot/query/dynamic_query.rb +61 -0
  35. data/lib/sunspot/query/field_facet.rb +129 -0
  36. data/lib/sunspot/query/field_query.rb +69 -0
  37. data/lib/sunspot/query/fulltext_base_query.rb +86 -0
  38. data/lib/sunspot/query/highlighting.rb +36 -0
  39. data/lib/sunspot/query/local.rb +24 -0
  40. data/lib/sunspot/query/pagination.rb +39 -0
  41. data/lib/sunspot/query/query_facet.rb +78 -0
  42. data/lib/sunspot/query/query_facet_row.rb +19 -0
  43. data/lib/sunspot/query/query_field_facet.rb +20 -0
  44. data/lib/sunspot/query/restriction.rb +272 -0
  45. data/lib/sunspot/query/scope.rb +185 -0
  46. data/lib/sunspot/query/sort.rb +105 -0
  47. data/lib/sunspot/query/sort_composite.rb +33 -0
  48. data/lib/sunspot/query/text_field_boost.rb +15 -0
  49. data/lib/sunspot/query.rb +108 -0
  50. data/lib/sunspot/schema.rb +147 -0
  51. data/lib/sunspot/search/highlight.rb +38 -0
  52. data/lib/sunspot/search/hit.rb +113 -0
  53. data/lib/sunspot/search.rb +240 -0
  54. data/lib/sunspot/session.rb +206 -0
  55. data/lib/sunspot/setup.rb +312 -0
  56. data/lib/sunspot/text_field_setup.rb +29 -0
  57. data/lib/sunspot/type.rb +200 -0
  58. data/lib/sunspot/util.rb +190 -0
  59. data/lib/sunspot.rb +459 -0
  60. data/solr/etc/jetty.xml +212 -0
  61. data/solr/etc/webdefault.xml +379 -0
  62. data/solr/lib/jetty-6.1.3.jar +0 -0
  63. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  64. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  65. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  66. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  67. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  68. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  69. data/solr/solr/conf/elevate.xml +36 -0
  70. data/solr/solr/conf/protwords.txt +21 -0
  71. data/solr/solr/conf/schema.xml +64 -0
  72. data/solr/solr/conf/solrconfig.xml +726 -0
  73. data/solr/solr/conf/stopwords.txt +57 -0
  74. data/solr/solr/conf/synonyms.txt +31 -0
  75. data/solr/start.jar +0 -0
  76. data/solr/webapps/solr.war +0 -0
  77. data/spec/api/adapters_spec.rb +33 -0
  78. data/spec/api/indexer/attributes_spec.rb +100 -0
  79. data/spec/api/indexer/batch_spec.rb +46 -0
  80. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  81. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  82. data/spec/api/indexer/fulltext_spec.rb +43 -0
  83. data/spec/api/indexer/removal_spec.rb +46 -0
  84. data/spec/api/indexer/spec_helper.rb +1 -0
  85. data/spec/api/indexer_spec.rb +4 -0
  86. data/spec/api/query/connectives_spec.rb +161 -0
  87. data/spec/api/query/dsl_spec.rb +12 -0
  88. data/spec/api/query/dynamic_fields_spec.rb +148 -0
  89. data/spec/api/query/faceting_spec.rb +272 -0
  90. data/spec/api/query/fulltext_spec.rb +152 -0
  91. data/spec/api/query/highlighting_spec.rb +82 -0
  92. data/spec/api/query/local_spec.rb +37 -0
  93. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  94. data/spec/api/query/scope_spec.rb +253 -0
  95. data/spec/api/query/spec_helper.rb +1 -0
  96. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  97. data/spec/api/query/types_spec.rb +20 -0
  98. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  99. data/spec/api/search/faceting_spec.rb +206 -0
  100. data/spec/api/search/highlighting_spec.rb +65 -0
  101. data/spec/api/search/hits_spec.rb +62 -0
  102. data/spec/api/search/results_spec.rb +52 -0
  103. data/spec/api/search/search_spec.rb +11 -0
  104. data/spec/api/search/spec_helper.rb +1 -0
  105. data/spec/api/session_spec.rb +157 -0
  106. data/spec/api/spec_helper.rb +1 -0
  107. data/spec/api/sunspot_spec.rb +18 -0
  108. data/spec/helpers/indexer_helper.rb +29 -0
  109. data/spec/helpers/query_helper.rb +13 -0
  110. data/spec/helpers/search_helper.rb +78 -0
  111. data/spec/integration/dynamic_fields_spec.rb +55 -0
  112. data/spec/integration/faceting_spec.rb +169 -0
  113. data/spec/integration/highlighting_spec.rb +22 -0
  114. data/spec/integration/keyword_search_spec.rb +148 -0
  115. data/spec/integration/local_search_spec.rb +47 -0
  116. data/spec/integration/scoped_search_spec.rb +303 -0
  117. data/spec/integration/spec_helper.rb +1 -0
  118. data/spec/integration/stored_fields_spec.rb +10 -0
  119. data/spec/integration/test_pagination.rb +32 -0
  120. data/spec/mocks/adapters.rb +32 -0
  121. data/spec/mocks/blog.rb +3 -0
  122. data/spec/mocks/comment.rb +19 -0
  123. data/spec/mocks/connection.rb +84 -0
  124. data/spec/mocks/mock_adapter.rb +30 -0
  125. data/spec/mocks/mock_record.rb +48 -0
  126. data/spec/mocks/photo.rb +8 -0
  127. data/spec/mocks/post.rb +75 -0
  128. data/spec/mocks/super_class.rb +2 -0
  129. data/spec/mocks/user.rb +8 -0
  130. data/spec/spec_helper.rb +60 -0
  131. data/tasks/gemspec.rake +35 -0
  132. data/tasks/rcov.rake +28 -0
  133. data/tasks/rdoc.rake +22 -0
  134. data/tasks/schema.rake +19 -0
  135. data/tasks/spec.rake +24 -0
  136. data/tasks/todo.rake +4 -0
  137. data/templates/schema.xml.erb +36 -0
  138. metadata +312 -0
@@ -0,0 +1,152 @@
1
+ require 'enumerator'
2
+
3
+ module Sunspot
4
+ #
5
+ # The FacetData classes encapsulate various sources of facet data (field
6
+ # facet, # date facet, query facet), presenting a polymorphic API to the Facet
7
+ # class.
8
+ #
9
+ module FacetData #:nodoc:all
10
+ #
11
+ # Base class for facet data.
12
+ #
13
+ class Abstract
14
+ attr_reader :field #:nodoc:
15
+
16
+ def reference
17
+ @field.reference if @field
18
+ end
19
+
20
+ def cast(value)
21
+ if @field
22
+ @field.cast(value)
23
+ else
24
+ value
25
+ end
26
+ end
27
+
28
+ def row_value(value)
29
+ cast(value)
30
+ end
31
+ end
32
+
33
+ #
34
+ # FieldFacetData encapsulates the data returned by field facets
35
+ #
36
+ class FieldFacetData < Abstract
37
+ def initialize(facet_values, field)
38
+ @facet_values, @field = facet_values, field
39
+ end
40
+
41
+ # The name of the field that contains this facet's values
42
+ #
43
+ # ==== Returns
44
+ #
45
+ # Symbol:: The field name
46
+ #
47
+ def name
48
+ @field.name
49
+ end
50
+
51
+ # The rows returned for this facet.
52
+ #
53
+ # ==== Returns
54
+ #
55
+ # Array:: Collection of FacetRow objects, in the order returned by Solr
56
+ #
57
+ def rows
58
+ @rows ||=
59
+ begin
60
+ rows = []
61
+ @facet_values.each_slice(2) do |value, count|
62
+ rows << yield(row_value(value), count)
63
+ end
64
+ rows
65
+ end
66
+ end
67
+ end
68
+
69
+ class DateFacetData < FieldFacetData
70
+ def initialize(facet_values, field)
71
+ @gap = facet_values.delete('gap')[/\+(\d+)SECONDS/,1].to_i
72
+ %w(start end).each { |key| facet_values.delete(key) }
73
+ super(facet_values.to_a.flatten, field)
74
+ end
75
+
76
+ #
77
+ # Get the rows of this date facet, which are instances of DateFacetRow.
78
+ # The rows will always be sorted in chronological order.
79
+ #
80
+ #--
81
+ #
82
+ # The date facet info comes back from Solr as a hash, so we need to sort
83
+ # it manually. FIXME this currently assumes we want to do a "lexical"
84
+ # sort, but we should support count sort as well, even if it's not a
85
+ # common use case.
86
+ #
87
+ def rows(&block)
88
+ super(&block).sort { |a, b| a.value.first <=> b.value.first }
89
+ end
90
+
91
+ private
92
+
93
+ def row_value(value)
94
+ cast(value)..(cast(value) + @gap)
95
+ end
96
+ end
97
+
98
+ #
99
+ # QueryFacetData encapsulates the data returned by a query facet.
100
+ #
101
+ class QueryFacetData < Abstract
102
+ def initialize(outgoing_query_facet, row_data) #:nodoc:
103
+ @outgoing_query_facet, @row_data = outgoing_query_facet, row_data
104
+ @field = @outgoing_query_facet.field
105
+ end
106
+
107
+ def name
108
+ outgoing_query_facet.name
109
+ end
110
+
111
+ #
112
+ # Get the rows associated with this query facet. Returned rows are always
113
+ # ordered by count.
114
+ #
115
+ # ==== Returns
116
+ #
117
+ # Array:: Collection of QueryFacetRow objects, ordered by count
118
+ #
119
+ def rows
120
+ @rows ||=
121
+ begin
122
+ rows = []
123
+ options = @outgoing_query_facet.options
124
+ minimum_count =
125
+ if options[:zeros] then 0
126
+ elsif options[:minimum_count] then options[:minimum_count]
127
+ else 1
128
+ end
129
+ for outgoing_row in @outgoing_query_facet.rows
130
+ row_query = outgoing_row.to_boolean_phrase
131
+ if @row_data.has_key?(row_query)
132
+ row = yield(outgoing_row.label, @row_data[row_query])
133
+ rows << row if row.count >= minimum_count
134
+ end
135
+ end
136
+ if options[:sort] == :index || !options[:limit] && options[:sort] != :count
137
+ if rows.all? { |row| row.value.respond_to?(:<=>) }
138
+ rows.sort! { |x, y| x.value <=> y.value }
139
+ end
140
+ else
141
+ rows.sort! { |x, y| y.count <=> x.count }
142
+ end
143
+ if limit = options[:limit]
144
+ rows[0, limit]
145
+ else
146
+ rows
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,12 @@
1
+ module Sunspot
2
+ #
3
+ # This class encapsulates a facet row (value) for a facet.
4
+ #
5
+ class FacetRow
6
+ attr_reader :value, :count
7
+
8
+ def initialize(value, count) #:nodoc:
9
+ @value, @count = value, count
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,148 @@
1
+ module Sunspot
2
+ class Field #:nodoc:
3
+ attr_accessor :name # The public-facing name of the field
4
+ attr_accessor :type # The Type of the field
5
+ attr_accessor :reference # Model class that the value of this field refers to
6
+ attr_reader :attributes
7
+
8
+ #
9
+ #
10
+ def initialize(name, type, options = {}) #:nodoc
11
+ @name, @type = name.to_sym, type
12
+ @stored = !!options.delete(:stored)
13
+ @attributes = {}
14
+ end
15
+
16
+ # Convert a value to its representation for Solr indexing. This delegates
17
+ # to the #to_indexed method on the field's type.
18
+ #
19
+ # ==== Parameters
20
+ #
21
+ # value<Object>:: Value to convert to Solr representation
22
+ #
23
+ # ==== Returns
24
+ #
25
+ # String:: Solr representation of the object
26
+ #
27
+ # ==== Raises
28
+ #
29
+ # ArgumentError::
30
+ # the value is an array, but this field does not allow multiple values
31
+ #
32
+ def to_indexed(value)
33
+ if value.is_a? Array
34
+ if @multiple
35
+ value.map { |val| to_indexed(val) }
36
+ else
37
+ raise ArgumentError, "#{name} is not a multiple-value field, so it cannot index values #{value.inspect}"
38
+ end
39
+ else
40
+ @type.to_indexed(value)
41
+ end
42
+ end
43
+
44
+ # Cast the value into the appropriate Ruby class for the field's type
45
+ #
46
+ # ==== Parameters
47
+ #
48
+ # value<String>:: Solr's representation of the value
49
+ #
50
+ # ==== Returns
51
+ #
52
+ # Object:: The cast value
53
+ #
54
+ def cast(value)
55
+ @type.cast(value)
56
+ end
57
+
58
+ #
59
+ # Name with which this field is indexed internally. Based on public name and
60
+ # type.
61
+ #
62
+ # ==== Returns
63
+ #
64
+ # String:: Internal name of the field
65
+ #
66
+ def indexed_name
67
+ @type.indexed_name(@name)
68
+ end
69
+
70
+ #
71
+ # Whether this field accepts multiple values.
72
+ #
73
+ # ==== Returns
74
+ #
75
+ # Boolean:: True if this field accepts multiple values.
76
+ #
77
+ def multiple?
78
+ !!@multiple
79
+ end
80
+
81
+ def hash
82
+ indexed_name.hash
83
+ end
84
+
85
+ def eql?(field)
86
+ indexed_name == field.indexed_name
87
+ end
88
+ alias_method :==, :eql?
89
+ end
90
+
91
+ #
92
+ # FulltextField instances represent fields that are indexed as fulltext.
93
+ # These fields are tokenized in the index, and can have boost applied to
94
+ # them. They also always allow multiple values (since the only downside of
95
+ # allowing multiple values is that it prevents the field from being sortable,
96
+ # and sorting on tokenized fields is nonsensical anyway, there is no reason
97
+ # to do otherwise). FulltextField instances always have the type TextType.
98
+ #
99
+ class FulltextField < Field #:nodoc:
100
+ attr_reader :boost
101
+
102
+ def initialize(name, options = {})
103
+ super(name, Type::TextType, options)
104
+ @multiple = true
105
+ if boost = options.delete(:boost)
106
+ @attributes[:boost] = boost
107
+ end
108
+ raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless options.empty?
109
+ end
110
+
111
+ def indexed_name
112
+ "#{super}#{'s' if @stored}"
113
+ end
114
+ end
115
+
116
+ #
117
+ # AttributeField instances encapsulate non-tokenized attribute data.
118
+ # AttributeFields can have any type except TextType, and can also have
119
+ # a reference (for instantiated facets), optionally allow multiple values
120
+ # (false by default), and can store their values (false by default). All
121
+ # scoping, sorting, and faceting is done with attribute fields.
122
+ #
123
+ class AttributeField < Field #:nodoc:
124
+ def initialize(name, type, options = {})
125
+ super(name, type, options)
126
+ @multiple = !!options.delete(:multiple)
127
+ @reference =
128
+ if (reference = options.delete(:references)).respond_to?(:name)
129
+ reference.name
130
+ elsif reference.respond_to?(:to_sym)
131
+ reference.to_sym
132
+ end
133
+ raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless options.empty?
134
+ end
135
+
136
+ # The name of the field as it is indexed in Solr. The indexed name
137
+ # contains a suffix that contains information about the type as well as
138
+ # whether the field allows multiple values for a document.
139
+ #
140
+ # ==== Returns
141
+ #
142
+ # String:: The field's indexed name
143
+ #
144
+ def indexed_name
145
+ "#{super}#{'m' if @multiple}#{'s' if @stored}"
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,141 @@
1
+ module Sunspot
2
+ #
3
+ # The FieldFactory module contains classes for generating fields. FieldFactory
4
+ # implementation classes should implement a #build method, although the arity
5
+ # of the method depends on the type of factory. They also must implement a
6
+ # #populate_document method, which extracts field data from a given model and
7
+ # adds it into the RSolr document for indexing.
8
+ #
9
+ module FieldFactory #:nodoc:all
10
+ #
11
+ # Base class for field factories.
12
+ #
13
+ class Abstract
14
+ attr_reader :name
15
+
16
+ def initialize(name, options = {}, &block)
17
+ @name = name.to_sym
18
+ @data_extractor =
19
+ if block
20
+ DataExtractor::BlockExtractor.new(&block)
21
+ else
22
+ DataExtractor::AttributeExtractor.new(options.delete(:using) || name)
23
+ end
24
+ end
25
+ end
26
+
27
+ #
28
+ # A StaticFieldFactory generates normal static fields. Each factory instance
29
+ # contains an eager-initialized field instance, which is returned by the
30
+ # #build method.
31
+ #
32
+ class Static < Abstract
33
+ def initialize(name, type, options = {}, &block)
34
+ super(name, options, &block)
35
+ unless name.to_s =~ /^\w+$/
36
+ raise ArgumentError, "Invalid field name #{name}: only letters, numbers, and underscores are allowed."
37
+ end
38
+ @field =
39
+ if type == Type::TextType
40
+ FulltextField.new(name, options)
41
+ else
42
+ AttributeField.new(name, type, options)
43
+ end
44
+ end
45
+
46
+ #
47
+ # Return the field instance built by this factory
48
+ #
49
+ def build
50
+ @field
51
+ end
52
+
53
+ #
54
+ # Extract the encapsulated field's data from the given model and add it
55
+ # into the RSolr document for indexing.
56
+ #
57
+ def populate_document(document, model) #:nodoc:
58
+ unless (value = @data_extractor.value_for(model)).nil?
59
+ for scalar_value in Array(@field.to_indexed(value))
60
+ document.add_field(
61
+ @field.indexed_name.to_sym,
62
+ scalar_value, @field.attributes
63
+ )
64
+ end
65
+ end
66
+ end
67
+
68
+ #
69
+ # A unique signature identifying this field by name and type.
70
+ #
71
+ def signature
72
+ [@field.name, @field.type]
73
+ end
74
+ end
75
+
76
+ #
77
+ # DynamicFieldFactories create dynamic field instances based on dynamic
78
+ # configuration.
79
+ #
80
+ class Dynamic < Abstract
81
+ attr_accessor :name, :type
82
+
83
+ def initialize(name, type, options = {}, &block)
84
+ super(name, options, &block)
85
+ @type, @options = type, options
86
+ end
87
+
88
+ #
89
+ # Build a field based on the dynamic name given.
90
+ #
91
+ def build(dynamic_name)
92
+ AttributeField.new("#{@name}:#{dynamic_name}", @type, @options.dup)
93
+ end
94
+ #
95
+ # This alias allows a DynamicFieldFactory to be used in place of a Setup
96
+ # or CompositeSetup instance by query components.
97
+ #
98
+ alias_method :field, :build
99
+
100
+ #
101
+ # Generate dynamic fields based on hash returned by data accessor and
102
+ # add the field data to the document.
103
+ #
104
+ def populate_document(document, model)
105
+ if values = @data_extractor.value_for(model)
106
+ values.each_pair do |dynamic_name, value|
107
+ field_instance = build(dynamic_name)
108
+ for scalar_value in Array(field_instance.to_indexed(value))
109
+ document.add_field(
110
+ field_instance.indexed_name.to_sym,
111
+ scalar_value
112
+ )
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ #
119
+ # Unique signature identifying this dynamic field based on name and type
120
+ #
121
+ def signature
122
+ [@name, @type]
123
+ end
124
+ end
125
+
126
+ #XXX Right now this doubles as a Field and a FieldFactory - good idea?
127
+ class Coordinates
128
+ def initialize(name)
129
+ @data_extractor = DataExtractor::AttributeExtractor.new(name)
130
+ end
131
+
132
+ def populate_document(document, model)
133
+ if coordinates = @data_extractor.value_for(model)
134
+ coordinates = Util::Coordinates.new(coordinates)
135
+ document.add_field(:lat, coordinates.lat)
136
+ document.add_field(:long, coordinates.lng)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,129 @@
1
+ module Sunspot
2
+ #
3
+ # This class presents a service for adding, updating, and removing data
4
+ # from the Solr index. An Indexer instance is associated with a particular
5
+ # setup, and thus is capable of indexing instances of a certain class (and its
6
+ # subclasses).
7
+ #
8
+ class Indexer #:nodoc:
9
+ include RSolr::Char
10
+
11
+ def initialize(connection)
12
+ @connection = connection
13
+ end
14
+
15
+ #
16
+ # Construct a representation of the model for indexing and send it to the
17
+ # connection for indexing
18
+ #
19
+ # ==== Parameters
20
+ #
21
+ # model<Object>:: the model to index
22
+ #
23
+ def add(model)
24
+ documents = Array(model).map { |m| prepare(m) }
25
+ if @batch.nil?
26
+ add_documents(documents)
27
+ else
28
+ @batch.concat(documents)
29
+ end
30
+ end
31
+
32
+ #
33
+ # Remove the given model from the Solr index
34
+ #
35
+ def remove(model)
36
+ @connection.delete_by_id(Adapters::InstanceAdapter.adapt(model).index_id)
37
+ end
38
+
39
+ def remove_by_id(class_name, id)
40
+ @connection.delete_by_id(
41
+ Adapters::InstanceAdapter.index_id_for(class_name, id)
42
+ )
43
+ end
44
+
45
+ #
46
+ # Delete all documents of the class indexed by this indexer from Solr.
47
+ #
48
+ def remove_all(clazz)
49
+ @connection.delete_by_query("type:#{escape(clazz.name)}")
50
+ end
51
+
52
+ #
53
+ # Start batch processing
54
+ #
55
+ def start_batch
56
+ @batch = []
57
+ end
58
+
59
+ #
60
+ # Write batch out to Solr and clear it
61
+ #
62
+ def flush_batch
63
+ add_documents(@batch)
64
+ @batch = nil
65
+ end
66
+
67
+ private
68
+
69
+ #
70
+ # Convert documents into hash of indexed properties
71
+ #
72
+ def prepare(model)
73
+ document = document_for(model)
74
+ setup = setup_for(model)
75
+ if boost = setup.document_boost_for(model)
76
+ document.attrs[:boost] = boost
77
+ end
78
+ for field_factory in setup.all_field_factories
79
+ field_factory.populate_document(document, model)
80
+ end
81
+ document
82
+ end
83
+
84
+ def add_documents(documents)
85
+ @connection.add(documents)
86
+ end
87
+
88
+ #
89
+ # All indexed documents index and store the +id+ and +type+ fields.
90
+ # This method constructs the document hash containing those key-value
91
+ # pairs.
92
+ #
93
+ def document_for(model)
94
+ RSolr::Message::Document.new(
95
+ :id => Adapters::InstanceAdapter.adapt(model).index_id,
96
+ :type => Util.superclasses_for(model.class).map { |clazz| clazz.name }
97
+ )
98
+ end
99
+
100
+ #
101
+ # Get the Setup object for the given object's class.
102
+ #
103
+ # ==== Parameters
104
+ #
105
+ # object<Object>:: The object whose setup is to be retrieved
106
+ #
107
+ # ==== Returns
108
+ #
109
+ # Sunspot::Setup:: The setup for the object's class
110
+ #
111
+ def setup_for(object)
112
+ Setup.for(object.class) || raise(NoSetupError, "Sunspot is not configured for #{object.class.inspect}")
113
+ end
114
+
115
+
116
+ class <<self
117
+ #
118
+ # Delete all documents from the Solr index
119
+ #
120
+ # ==== Parameters
121
+ #
122
+ # connection<Solr::Connection>::
123
+ # connection to which to send the delete request
124
+ def remove_all(connection)
125
+ connection.delete_by_query("type:[* TO *]")
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,45 @@
1
+ module Sunspot
2
+ #
3
+ # InstantiatedFacet instances allow access to a model instance based on a
4
+ # primary key stored in facet rows' values. The rows are hydrated lazily, but
5
+ # all rows are hydrated the first time #instance is called on any of the rows.
6
+ #
7
+ # The #rows method returns InstantiatedFacetRow objects.
8
+ #
9
+ class InstantiatedFacet < Facet
10
+ #
11
+ # Hydrate all rows for the facet. For data accessors that can efficiently
12
+ # batch load, this is more efficient than individually lazy-loading
13
+ # instances for each row, but allows us to still stay lazy and not do work
14
+ # in the persistent store if the instances are not needed.
15
+ #
16
+ def populate_instances! #:nodoc:
17
+ ids = rows.map { |row| row.value }
18
+ reference_class = Sunspot::Util.full_const_get(@facet_data.reference.to_s)
19
+ accessor = Adapters::DataAccessor.create(reference_class)
20
+ instance_map = accessor.load_all(ids).inject({}) do |map, instance|
21
+ map[Adapters::InstanceAdapter.adapt(instance).id] = instance
22
+ map
23
+ end
24
+ for row in rows
25
+ row.instance = instance_map[row.value]
26
+ end
27
+ end
28
+
29
+ #
30
+ # A collection of InstantiatedFacetRow objects
31
+ #
32
+ def rows
33
+ @facet_data.rows { |value, count| InstantiatedFacetRow.new(value, count, self) }
34
+ end
35
+
36
+ private
37
+
38
+ #
39
+ # Override the Facet#new_row method to return an InstantiatedFacetRow
40
+ #
41
+ def new_row(pair)
42
+ InstantiatedFacetRow.new(pair, self)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ module Sunspot
2
+ #
3
+ # InstantiatedFacetRow objects represent a single value for an instantiated
4
+ # facet. As well as the usual FacetRow methods, InstantedFacetRow objects
5
+ # provide access to the persistent object referenced by the row's value.
6
+ #
7
+ class InstantiatedFacetRow < FacetRow
8
+ attr_writer :instance #:nodoc:
9
+
10
+ def initialize(value, count, facet) #:nodoc:
11
+ super(value, count)
12
+ @facet = facet
13
+ end
14
+
15
+ #
16
+ # Get the persistent object referenced by this row's value. Instances are
17
+ # batch-lazy-loaded, which means that for a given facet, all of the
18
+ # instances are loaded the first time any row's instance is requested.
19
+ #
20
+ def instance
21
+ unless defined?(@instance)
22
+ @facet.populate_instances!
23
+ end
24
+ @instance
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,55 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # Encapsulates information common to all queries - in particular, types.
5
+ # Subclassed by FulltextBaseQuery, which puts the types in a filter query
6
+ # and sets up dismax search.
7
+ #
8
+ class BaseQuery #:nodoc:
9
+ include RSolr::Char
10
+
11
+ attr_reader :types
12
+ attr_writer :keywords
13
+ attr_writer :phrase_fields
14
+
15
+ def initialize(types, setup)
16
+ @types, @setup = types, setup
17
+ end
18
+
19
+ #
20
+ # Generate params for the base query. If keywords are specified, build
21
+ # params for a dismax query, request all stored fields plus the score,
22
+ # and put the types in a filter query. If keywords are not specified,
23
+ # put the types query in the q parameter.
24
+ #
25
+ def to_params
26
+ { :q => types_phrase }
27
+ end
28
+
29
+ private
30
+
31
+ #
32
+ # Boolean phrase that restricts results to objects of the type(s) under
33
+ # query. If this is an open query (no types specified) then it sends a
34
+ # no-op phrase because Solr requires that the :q parameter not be empty.
35
+ #
36
+ # ==== Returns
37
+ #
38
+ # String:: Boolean phrase for type restriction
39
+ #
40
+ def types_phrase
41
+ if escaped_types.length == 1 then "type:#{escaped_types.first}"
42
+ else "type:(#{escaped_types * ' OR '})"
43
+ end
44
+ end
45
+
46
+ #
47
+ # Wraps each type in quotes to escape names of the form Namespace::Class
48
+ #
49
+ def escaped_types
50
+ @escaped_types ||=
51
+ @types.map { |type| escape(type.name)}
52
+ end
53
+ end
54
+ end
55
+ end