kuahyeow-sunspot 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. data/History.txt +83 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +154 -0
  4. data/Rakefile +9 -0
  5. data/TODO +9 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +46 -0
  8. data/bin/sunspot-solr +62 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot/adapters.rb +265 -0
  11. data/lib/sunspot/composite_setup.rb +186 -0
  12. data/lib/sunspot/configuration.rb +38 -0
  13. data/lib/sunspot/data_extractor.rb +47 -0
  14. data/lib/sunspot/date_facet.rb +36 -0
  15. data/lib/sunspot/date_facet_row.rb +17 -0
  16. data/lib/sunspot/dsl/field_query.rb +72 -0
  17. data/lib/sunspot/dsl/fields.rb +86 -0
  18. data/lib/sunspot/dsl/query.rb +59 -0
  19. data/lib/sunspot/dsl/query_facet.rb +31 -0
  20. data/lib/sunspot/dsl/restriction.rb +25 -0
  21. data/lib/sunspot/dsl/scope.rb +193 -0
  22. data/lib/sunspot/dsl/search.rb +30 -0
  23. data/lib/sunspot/dsl.rb +3 -0
  24. data/lib/sunspot/facet.rb +51 -0
  25. data/lib/sunspot/facet_row.rb +34 -0
  26. data/lib/sunspot/field.rb +157 -0
  27. data/lib/sunspot/field_factory.rb +126 -0
  28. data/lib/sunspot/indexer.rb +123 -0
  29. data/lib/sunspot/instantiated_facet.rb +38 -0
  30. data/lib/sunspot/instantiated_facet_row.rb +12 -0
  31. data/lib/sunspot/query/base_query.rb +94 -0
  32. data/lib/sunspot/query/connective.rb +126 -0
  33. data/lib/sunspot/query/dynamic_query.rb +69 -0
  34. data/lib/sunspot/query/field_facet.rb +149 -0
  35. data/lib/sunspot/query/field_query.rb +57 -0
  36. data/lib/sunspot/query/pagination.rb +39 -0
  37. data/lib/sunspot/query/query_facet.rb +72 -0
  38. data/lib/sunspot/query/query_facet_row.rb +19 -0
  39. data/lib/sunspot/query/restriction.rb +233 -0
  40. data/lib/sunspot/query/scope.rb +165 -0
  41. data/lib/sunspot/query/sort.rb +36 -0
  42. data/lib/sunspot/query/sort_composite.rb +33 -0
  43. data/lib/sunspot/query.rb +190 -0
  44. data/lib/sunspot/query_facet.rb +33 -0
  45. data/lib/sunspot/query_facet_row.rb +21 -0
  46. data/lib/sunspot/schema.rb +165 -0
  47. data/lib/sunspot/search/hit.rb +66 -0
  48. data/lib/sunspot/search.rb +220 -0
  49. data/lib/sunspot/session.rb +201 -0
  50. data/lib/sunspot/setup.rb +271 -0
  51. data/lib/sunspot/type.rb +200 -0
  52. data/lib/sunspot/util.rb +164 -0
  53. data/lib/sunspot.rb +470 -0
  54. data/solr/etc/jetty.xml +212 -0
  55. data/solr/etc/webdefault.xml +379 -0
  56. data/solr/lib/jetty-6.1.3.jar +0 -0
  57. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  58. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  59. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  60. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  61. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  62. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  63. data/solr/solr/conf/elevate.xml +36 -0
  64. data/solr/solr/conf/protwords.txt +21 -0
  65. data/solr/solr/conf/schema.xml +50 -0
  66. data/solr/solr/conf/solrconfig.xml +696 -0
  67. data/solr/solr/conf/stopwords.txt +57 -0
  68. data/solr/solr/conf/synonyms.txt +31 -0
  69. data/solr/start.jar +0 -0
  70. data/solr/webapps/solr.war +0 -0
  71. data/spec/api/adapters_spec.rb +33 -0
  72. data/spec/api/build_search_spec.rb +1018 -0
  73. data/spec/api/indexer_spec.rb +311 -0
  74. data/spec/api/query_spec.rb +153 -0
  75. data/spec/api/search_retrieval_spec.rb +335 -0
  76. data/spec/api/session_spec.rb +157 -0
  77. data/spec/api/spec_helper.rb +1 -0
  78. data/spec/api/sunspot_spec.rb +18 -0
  79. data/spec/integration/dynamic_fields_spec.rb +55 -0
  80. data/spec/integration/faceting_spec.rb +169 -0
  81. data/spec/integration/keyword_search_spec.rb +83 -0
  82. data/spec/integration/scoped_search_spec.rb +289 -0
  83. data/spec/integration/spec_helper.rb +1 -0
  84. data/spec/integration/stored_fields_spec.rb +10 -0
  85. data/spec/integration/test_pagination.rb +32 -0
  86. data/spec/mocks/adapters.rb +32 -0
  87. data/spec/mocks/blog.rb +3 -0
  88. data/spec/mocks/comment.rb +19 -0
  89. data/spec/mocks/connection.rb +84 -0
  90. data/spec/mocks/mock_adapter.rb +30 -0
  91. data/spec/mocks/mock_record.rb +48 -0
  92. data/spec/mocks/photo.rb +8 -0
  93. data/spec/mocks/post.rb +73 -0
  94. data/spec/mocks/user.rb +8 -0
  95. data/spec/spec_helper.rb +47 -0
  96. data/tasks/gemspec.rake +24 -0
  97. data/tasks/rcov.rake +28 -0
  98. data/tasks/rdoc.rake +22 -0
  99. data/tasks/schema.rake +19 -0
  100. data/tasks/spec.rake +24 -0
  101. data/tasks/todo.rake +4 -0
  102. data/templates/schema.xml.haml +24 -0
  103. metadata +247 -0
@@ -0,0 +1,72 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # QueryFacets encapsulate requests for Sunspot's query faceting capability.
5
+ # They are created by the FieldQuery#add_query_facet method.
6
+ #
7
+ #--
8
+ #
9
+ # The actual concept of a QueryFacet is somewhat artificial - it provides a
10
+ # grouping for the facet at the Sunspot level, which provides a nicer and
11
+ # more consistent API in Sunspot; Solr does not provide any grouping for
12
+ # query facet rows, instead returning each requested row individually, keyed
13
+ # by the boolean phrase used in the facet query.
14
+ #
15
+ class QueryFacet
16
+ attr_reader :name #:nodoc:
17
+
18
+ def initialize(name, setup) #:nodoc:
19
+ @name = name
20
+ @setup = setup
21
+ @components = []
22
+ end
23
+
24
+ #
25
+ # Add a QueryFacetRow to this facet. The label argument becomes the value
26
+ # of the Sunspot::QueryFacetRow object corresponding to this query facet
27
+ # row.
28
+ #
29
+ # ==== Parameters
30
+ #
31
+ # label<Object>::
32
+ # An object that will become the value of the result row. Use whatever
33
+ # type is most intuitive.
34
+ #
35
+ # ==== Returns
36
+ #
37
+ # QueryFacetRow:: QueryFacetRow object containing scope for this row
38
+ #
39
+ def add_row(label)
40
+ @components << row = QueryFacetRow.new(label, @setup)
41
+ row
42
+ end
43
+
44
+ #
45
+ # Express this query facet as Solr parameters
46
+ #
47
+ # ==== Returns
48
+ #
49
+ # Hash:: Solr params hash
50
+ #
51
+ def to_params #:nodoc:
52
+ components = @components.map { |component| component.to_boolean_phrase }
53
+ components = components.first if components.length == 1
54
+ {
55
+ :facet => 'true',
56
+ :"facet.query" => components
57
+ }
58
+ end
59
+
60
+ #
61
+ # Get query facet rows (used when constructing results)
62
+ #
63
+ # ==== Returns
64
+ #
65
+ # Array:: Array of QueryFacetRow objects.
66
+ #
67
+ def rows #:nodoc:
68
+ @components
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # QueryFacetRow objects encapsulate restrictions for a particular
5
+ # QueryFacet. They also contain a label attribute, which is used as the
6
+ # value for the search result's corresponding facet row object.
7
+ #
8
+ # See Query::Scope for the API provided.
9
+ #
10
+ class QueryFacetRow < Connective::Conjunction
11
+ attr_reader :label #:nodoc:
12
+
13
+ def initialize(label, setup) #:nodoc:
14
+ super(setup)
15
+ @label = label
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,233 @@
1
+ module Sunspot
2
+ module Query
3
+ module Restriction #:nodoc:
4
+ class <<self
5
+ #
6
+ # Return the names of all of the restriction classes that should be made
7
+ # available to the DSL.
8
+ #
9
+ # ==== Returns
10
+ #
11
+ # Array:: Collection of restriction class names
12
+ #
13
+ def names
14
+ constants - %w(Base SameAs) #XXX this seems ugly
15
+ end
16
+
17
+ def [](restriction_name)
18
+ @types ||= {}
19
+ @types[restriction_name.to_sym] ||= const_get(Sunspot::Util.camel_case(restriction_name.to_s))
20
+ end
21
+ end
22
+
23
+ #
24
+ # Subclasses of this class represent restrictions that can be applied to
25
+ # a Sunspot query. The Sunspot::DSL::Restriction class presents a builder
26
+ # API for instances of this class.
27
+ #
28
+ # Implementations of this class must respond to #to_params and
29
+ # #to_negated_params. Instead of implementing those methods, they may
30
+ # choose to implement any of:
31
+ #
32
+ # * #to_positive_boolean_phrase, and optionally #to_negated_boolean_phrase
33
+ # * #to_solr_conditional
34
+ #
35
+ class Base #:nodoc:
36
+ include RSolr::Char
37
+
38
+ def initialize(field, value, negated = false)
39
+ @field, @value, @negated = field, value, negated
40
+ end
41
+
42
+ #
43
+ # A hash representing this restriction in solr-ruby's parameter format.
44
+ # All restriction implementations must respond to this method; however,
45
+ # the base implementation delegates to the #to_positive_boolean_phrase method, so
46
+ # subclasses may (and probably should) choose to implement that method
47
+ # instead.
48
+ #
49
+ # ==== Returns
50
+ #
51
+ # Hash:: Representation of this restriction as solr-ruby parameters
52
+ #
53
+ def to_params
54
+ { :fq => [to_boolean_phrase] }
55
+ end
56
+
57
+ #
58
+ # Return the boolean phrase associated with this restriction object.
59
+ # Differentiates between positive and negated boolean phrases depending
60
+ # on whether this restriction is negated.
61
+ #
62
+ def to_boolean_phrase
63
+ unless negated?
64
+ to_positive_boolean_phrase
65
+ else
66
+ to_negated_boolean_phrase
67
+ end
68
+ end
69
+
70
+ #
71
+ # Boolean phrase representing this restriction in the positive. Subclasses
72
+ # may choose to implement this method rather than #to_params; however,
73
+ # this method delegates to the abstract #to_solr_conditional method, which
74
+ # in most cases will be what subclasses will want to implement.
75
+ # #to_solr_conditional contains the boolean phrase representing the
76
+ # condition but leaves out the field name (see built-in implementations
77
+ # for examples)
78
+ #
79
+ # ==== Returns
80
+ #
81
+ # String:: Boolean phrase for restriction in the positive
82
+ #
83
+ def to_positive_boolean_phrase
84
+ "#{escape(@field.indexed_name)}:#{to_solr_conditional}"
85
+ end
86
+
87
+ #
88
+ # Boolean phrase representing this restriction in the negated. Subclasses
89
+ # may choose to implement this method, but it is not necessary, as the
90
+ # base implementation delegates to #to_positive_boolean_phrase.
91
+ #
92
+ # ==== Returns
93
+ #
94
+ # String:: Boolean phrase for restriction in the negated
95
+ #
96
+ def to_negated_boolean_phrase
97
+ "-#{to_positive_boolean_phrase}"
98
+ end
99
+
100
+ #
101
+ # Whether this restriction should be negated from its original meaning
102
+ #
103
+ def negated? #:nodoc:
104
+ !!@negated
105
+ end
106
+
107
+ def negate
108
+ self.class.new(@field, @value, !@negated)
109
+ end
110
+
111
+ protected
112
+
113
+ #
114
+ # Return escaped Solr API representation of given value
115
+ #
116
+ # ==== Parameters
117
+ #
118
+ # value<Object>::
119
+ # value to convert to Solr representation (default: @value)
120
+ #
121
+ # ==== Returns
122
+ #
123
+ # String:: Solr API representation of given value
124
+ #
125
+ def solr_value(value = @value)
126
+ escape(@field.to_indexed(value))
127
+ end
128
+ end
129
+
130
+ #
131
+ # Results must have field with value equal to given value. If the value
132
+ # is nil, results must have no value for the given field.
133
+ #
134
+ class EqualTo < Base
135
+ def to_positive_boolean_phrase
136
+ unless @value.nil?
137
+ super
138
+ else
139
+ "#{escape(@field.indexed_name)}:[* TO *]"
140
+ end
141
+ end
142
+
143
+ def negated?
144
+ if @value.nil?
145
+ !super
146
+ else
147
+ super
148
+ end
149
+ end
150
+
151
+ private
152
+
153
+ def to_solr_conditional
154
+ "#{solr_value}"
155
+ end
156
+ end
157
+
158
+ #
159
+ # Results must have field with value less than given value
160
+ #
161
+ class LessThan < Base
162
+ private
163
+
164
+ def to_solr_conditional
165
+ "[* TO #{solr_value}]"
166
+ end
167
+ end
168
+
169
+ #
170
+ # Results must have field with value greater than given value
171
+ #
172
+ class GreaterThan < Base
173
+ private
174
+
175
+ def to_solr_conditional
176
+ "[#{solr_value} TO *]"
177
+ end
178
+ end
179
+
180
+ #
181
+ # Results must have field with value in given range
182
+ #
183
+ class Between < Base
184
+ private
185
+
186
+ def to_solr_conditional
187
+ "[#{solr_value(@value.first)} TO #{solr_value(@value.last)}]"
188
+ end
189
+ end
190
+
191
+ #
192
+ # Results must have field with value included in given collection
193
+ #
194
+ class AnyOf < Base
195
+ private
196
+
197
+ def to_solr_conditional
198
+ "(#{@value.map { |v| solr_value v } * ' OR '})"
199
+ end
200
+ end
201
+
202
+ #
203
+ # Results must have field with values matching all values in given
204
+ # collection (only makes sense for fields with multiple values)
205
+ #
206
+ class AllOf < Base
207
+ private
208
+
209
+ def to_solr_conditional
210
+ "(#{@value.map { |v| solr_value v } * ' AND '})"
211
+ end
212
+ end
213
+
214
+ #
215
+ # Result must be the exact instance given (only useful when negated).
216
+ #
217
+ class SameAs < Base
218
+ def initialize(object, negated = false)
219
+ @object, @negated = object, negated
220
+ end
221
+
222
+ def to_positive_boolean_phrase
223
+ adapter = Adapters::InstanceAdapter.adapt(@object)
224
+ "id:#{escape(adapter.index_id)}"
225
+ end
226
+
227
+ def negate
228
+ SameAs.new(@object, !negated?)
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,165 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # The Scope class encapsulates a set of restrictions that scope search
5
+ # results (as well as query facets rows). This class's API is exposed by
6
+ # Query::Query and Query::QueryFacetRow.
7
+ #
8
+ class Scope
9
+ #
10
+ # Add a restriction to the query.
11
+ #
12
+ # ==== Parameters
13
+ #
14
+ # field_name<Symbol>:: Name of the field to which the restriction applies
15
+ # restriction_type<Class,Symbol>::
16
+ # Subclass of Sunspot::Query::Restriction::Base, or snake_cased name as symbol
17
+ # (e.g., +:equal_to+)
18
+ # value<Object>::
19
+ # Value against which the restriction applies (e.g. less_than(2) has a
20
+ # value of 2)
21
+ # negated::
22
+ # Whether this restriction should be negated (use add_negated_restriction)
23
+ #
24
+ def add_restriction(field_name, restriction_type, value, negated = false)
25
+ if restriction_type.is_a?(Symbol)
26
+ restriction_type = Restriction[restriction_type]
27
+ end
28
+ add_component(
29
+ restriction = restriction_type.new(
30
+ build_field(field_name), value, negated
31
+ )
32
+ )
33
+ restriction
34
+ end
35
+
36
+ #
37
+ # Add a negated restriction to the query. The restriction will be taken as
38
+ # the opposite of its usual meaning (e.g., an :equal_to restriction will
39
+ # be "not equal to".
40
+ #
41
+ # ==== Parameters
42
+ #
43
+ # field_name<Symbol>:: Name of the field to which the restriction applies
44
+ # restriction_type<Class>::
45
+ # Subclass of Sunspot::Query::Restriction::Base to instantiate
46
+ # value<Object>::
47
+ # Value against which the restriction applies (e.g. less_than(2) has a
48
+ # value of 2)
49
+ #
50
+ def add_negated_restriction(field_name, restriction_type, value)
51
+ add_restriction(field_name, restriction_type, value, true)
52
+ end
53
+
54
+ #
55
+ # Add a disjunction to the scope. The disjunction can then take a set of
56
+ # restrictions, which are combined with OR semantics.
57
+ #
58
+ # ==== Returns
59
+ #
60
+ # Connective::Disjunction:: New disjunction
61
+ #
62
+ def add_disjunction
63
+ add_component(disjunction = Connective::Disjunction.new(setup))
64
+ disjunction
65
+ end
66
+
67
+ #
68
+ # Add a conjunction to the scope. In most cases, this will simply return
69
+ # the Scope object itself, since scopes by default combine their
70
+ # restrictions with OR semantics. The Connective::Disjunction class
71
+ # overrides this method to return a Connective::Conjunction.
72
+ #
73
+ # ==== Returns
74
+ #
75
+ # Scope:: Self or another scope with conjunctive semantics.
76
+ #
77
+ def add_conjunction
78
+ self
79
+ end
80
+
81
+ #
82
+ # Exclude a particular instance from the search results
83
+ #
84
+ # ==== Parameters
85
+ #
86
+ # instance<Object>:: instance to exclude from results
87
+ #
88
+ def exclude_instance(instance)
89
+ add_component(Restriction::SameAs.new(instance, true))
90
+ end
91
+
92
+ #
93
+ # Generate a DynamicQuery instance for the given base name.
94
+ # This gives you access to a subset of the Query API but the operations
95
+ # apply to dynamic fields inside the dynamic field definition specified
96
+ # by +base_name+.
97
+ #
98
+ # ==== Parameters
99
+ #
100
+ # base_name<Symbol>::
101
+ # Base name of the dynamic field definition to use in the dynamic query
102
+ # operations
103
+ #
104
+ # ==== Returns
105
+ #
106
+ # DynamicQuery::
107
+ # Instance providing dynamic query functionality for the given field
108
+ # definitions.
109
+ #
110
+ def dynamic_query(base_name)
111
+ DynamicQuery.new(setup.dynamic_field_factory(base_name), self)
112
+ end
113
+
114
+ #
115
+ # Determine which restriction type to add based on the type of the value.
116
+ # Used to interpret query conditions passed as a hash, as well as the
117
+ # short-form DSL::Scope#with method.
118
+ #
119
+ # ==== Parameters
120
+ #
121
+ # field_name<Symbol>:: Name of the field on which to apply the restriction
122
+ # value<Object,Array,Range>:: Value to which to apply to the restriction
123
+ #--
124
+ # negated<Boolean>:: Whether to negate the restriction.
125
+ #
126
+ def add_shorthand_restriction(field_name, value, negated = false) #:nodoc:
127
+ restriction_type =
128
+ case value
129
+ when Range
130
+ Restriction::Between
131
+ when Array
132
+ Restriction::AnyOf
133
+ else
134
+ Restriction::EqualTo
135
+ end
136
+ add_restriction(field_name, restriction_type, value, negated)
137
+ end
138
+
139
+ #
140
+ # Add a negated shorthand restriction. See #add_shorthand_restriction
141
+ #
142
+ def add_negated_shorthand_restriction(field_name, value)
143
+ add_shorthand_restriction(field_name, value, true)
144
+ end
145
+
146
+ private
147
+
148
+ #
149
+ # Build a field with the given field name. Subclasses may override this
150
+ # method.
151
+ #
152
+ def build_field(field_name)
153
+ setup.field(field_name)
154
+ end
155
+
156
+ #
157
+ # Return a setup object which can return a field object given a name.
158
+ # Subclasses may override this method.
159
+ #
160
+ def setup
161
+ @setup
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,36 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # The Sort class is a query component representing a sort by a given field.
5
+ #
6
+ class Sort #:nodoc:
7
+ DIRECTIONS = {
8
+ :asc => 'asc',
9
+ :ascending => 'asc',
10
+ :desc => 'desc',
11
+ :descending => 'desc'
12
+ }
13
+
14
+ def initialize(field, direction = nil)
15
+ if field.multiple?
16
+ raise(ArgumentError, "#{field.name} cannot be used for ordering because it is a multiple-value field")
17
+ end
18
+ @field, @direction = field, (direction || :asc).to_sym
19
+ end
20
+
21
+ def to_param
22
+ "#{@field.indexed_name.to_sym} #{direction_for_solr}"
23
+ end
24
+
25
+ private
26
+
27
+ def direction_for_solr
28
+ DIRECTIONS[@direction] ||
29
+ raise(
30
+ ArgumentError,
31
+ "Unknown sort direction #{@direction}. Acceptable input is: #{DIRECTIONS.keys.map { |input| input.inspect } * ', '}"
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # The SortComposite class encapsulates an ordered collection of Sort
5
+ # objects. It's necessary to keep this as a separate class as Solr takes
6
+ # the sort as a single parameter, so adding sorts as regular components
7
+ # would not merge correctly in the #to_params method.
8
+ #
9
+ class SortComposite #:nodoc:
10
+ def initialize
11
+ @sorts = []
12
+ end
13
+
14
+ #
15
+ # Add a sort to the composite
16
+ #
17
+ def <<(sort)
18
+ @sorts << sort
19
+ end
20
+
21
+ #
22
+ # Combine the sorts into a single param by joining them
23
+ #
24
+ def to_params
25
+ unless @sorts.empty?
26
+ { :sort => @sorts.map { |sort| sort.to_param } * ', ' }
27
+ else
28
+ {}
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end