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,200 @@
1
+ module Sunspot
2
+ #
3
+ # This module contains singleton objects that represent the types that can be
4
+ # indexed and searched using Sunspot. Plugin developers should be able to
5
+ # add new constants to the Type module; as long as they implement the
6
+ # appropriate methods, Sunspot should be able to integrate them (note that
7
+ # this capability is untested at the moment). The required methods are:
8
+ #
9
+ # +indexed_name+::
10
+ # Convert a given field name into its form as stored in Solr. This
11
+ # generally means adding a suffix to match a Solr dynamicField definition.
12
+ # +to_indexed+::
13
+ # Convert a value of this type into the appropriate Solr string
14
+ # representation.
15
+ # +cast+::
16
+ # Convert a Solr string representation of a value into the appropriate
17
+ # Ruby type.
18
+ #
19
+ module Type
20
+ #
21
+ # Text is a special type that stores data for fulltext search. Unlike other
22
+ # types, Text fields are tokenized and are made available to the keyword
23
+ # search phrase. Text fields cannot be faceted, ordered upon, or used in
24
+ # restrictions. Similarly, text fields are the only fields that are made
25
+ # available to keyword search.
26
+ #
27
+ module TextType
28
+ class <<self
29
+ def indexed_name(name) #:nodoc:
30
+ "#{name}_text"
31
+ end
32
+
33
+ def to_indexed(value) #:nodoc:
34
+ value.to_s if value
35
+ end
36
+ end
37
+ end
38
+
39
+ #
40
+ # The String type represents string data.
41
+ #
42
+ module StringType
43
+ class <<self
44
+ def indexed_name(name) #:nodoc:
45
+ "#{name}_s"
46
+ end
47
+
48
+ def to_indexed(value) #:nodoc:
49
+ value.to_s if value
50
+ end
51
+
52
+ def cast(string) #:nodoc:
53
+ string
54
+ end
55
+ end
56
+ end
57
+
58
+ #
59
+ # The Integer type represents integers.
60
+ #
61
+ module IntegerType
62
+ class <<self
63
+ def indexed_name(name) #:nodoc:
64
+ "#{name}_i"
65
+ end
66
+
67
+ def to_indexed(value) #:nodoc:
68
+ value.to_i.to_s if value
69
+ end
70
+
71
+ def cast(string) #:nodoc:
72
+ string.to_i
73
+ end
74
+ end
75
+ end
76
+
77
+ #
78
+ # The Float type represents floating-point numbers.
79
+ #
80
+ module FloatType
81
+ class <<self
82
+ def indexed_name(name) #:nodoc:
83
+ "#{name}_f"
84
+ end
85
+
86
+ def to_indexed(value) #:nodoc:
87
+ value.to_f.to_s if value
88
+ end
89
+
90
+ def cast(string) #:nodoc:
91
+ string.to_f
92
+ end
93
+ end
94
+ end
95
+
96
+ #
97
+ # The time type represents times. Note that times are always converted to
98
+ # UTC before indexing, and facets of Time fields always return times in UTC.
99
+ #
100
+ module TimeType
101
+
102
+ class <<self
103
+ def indexed_name(name) #:nodoc:
104
+ "#{name}_d"
105
+ end
106
+
107
+ def to_indexed(value) #:nodoc:
108
+ if value
109
+ time =
110
+ if value.respond_to?(:utc)
111
+ value
112
+ else
113
+ Time.parse(value.to_s)
114
+ end
115
+ time.utc.xmlschema
116
+ end
117
+ end
118
+
119
+ def cast(string) #:nodoc:
120
+ Time.xmlschema(string)
121
+ end
122
+ end
123
+ end
124
+
125
+ #
126
+ # The DateType encapsulates dates (without time information). Internally,
127
+ # Solr does not have a date-only type, so this type indexes data using
128
+ # Solr's DateField type (which is actually date/time), midnight UTC of the
129
+ # indexed date.
130
+ #
131
+ module DateType
132
+ class <<self
133
+ def indexed_name(name) #:nodoc:
134
+ "#{name}_d"
135
+ end
136
+
137
+ def to_indexed(value) #:nodoc:
138
+ if value
139
+ time =
140
+ if %w(year mon mday).all? { |method| value.respond_to?(method) }
141
+ Time.utc(value.year, value.mon, value.mday)
142
+ else
143
+ date = Date.parse(value.to_s)
144
+ Time.utc(date.year, date.mon, date.mday)
145
+ end
146
+ time.utc.xmlschema
147
+ end
148
+ end
149
+
150
+ def cast(string) #:nodoc:
151
+ time = Time.xmlschema(string)
152
+ Date.civil(time.year, time.mon, time.mday)
153
+ end
154
+ end
155
+ end
156
+
157
+ #
158
+ # The boolean type represents true/false values. Note that +nil+ will not be
159
+ # indexed at all; only +false+ will be indexed with a false value.
160
+ #
161
+ module BooleanType
162
+ class <<self
163
+ def indexed_name(name) #:nodoc:
164
+ "#{name}_b"
165
+ end
166
+
167
+ def to_indexed(value) #:nodoc:
168
+ unless value.nil?
169
+ value ? 'true' : 'false'
170
+ end
171
+ end
172
+
173
+ def cast(string) #:nodoc:
174
+ case string
175
+ when 'true'
176
+ true
177
+ when 'false'
178
+ false
179
+ end
180
+ end
181
+ end
182
+ end
183
+
184
+ module ClassType
185
+ class <<self
186
+ def indexed_name(name) #:nodoc:
187
+ 'class_name'
188
+ end
189
+
190
+ def to_indexed(value) #:nodoc:
191
+ value.name
192
+ end
193
+
194
+ def cast(string) #:nodoc:
195
+ Sunspot::Util.full_const_get(string)
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,190 @@
1
+ module Sunspot
2
+ #
3
+ # The Sunspot::Util module provides utility methods used elsewhere in the
4
+ # library.
5
+ #
6
+ module Util #:nodoc:
7
+ class <<self
8
+ #
9
+ # Get all of the superclasses for a given class, including the class
10
+ # itself.
11
+ #
12
+ # ==== Parameters
13
+ #
14
+ # clazz<Class>:: class for which to get superclasses
15
+ #
16
+ # ==== Returns
17
+ #
18
+ # Array:: Collection containing class and its superclasses
19
+ #
20
+ def superclasses_for(clazz)
21
+ superclasses = [clazz]
22
+ superclasses << (clazz = clazz.superclass) while clazz.superclass != Object
23
+ superclasses
24
+ end
25
+
26
+ #
27
+ # Convert a string to snake case
28
+ #
29
+ # ==== Parameters
30
+ #
31
+ # string<String>:: String to convert to snake case
32
+ #
33
+ # ==== Returns
34
+ #
35
+ # String:: String in snake case
36
+ #
37
+ def snake_case(string)
38
+ string.scan(/(^|[A-Z])([^A-Z]+)/).map! { |word| word.join.downcase }.join('_')
39
+ end
40
+
41
+ #
42
+ # Convert a string to camel case
43
+ #
44
+ # ==== Parameters
45
+ #
46
+ # string<String>:: String to convert to camel case
47
+ #
48
+ # ==== Returns
49
+ #
50
+ # String:: String in camel case
51
+ #
52
+ def camel_case(string)
53
+ string.split('_').map! { |word| word.capitalize }.join
54
+ end
55
+
56
+ #
57
+ # Get a constant from a fully qualified name
58
+ #
59
+ # ==== Parameters
60
+ #
61
+ # string<String>:: The fully qualified name of a constant
62
+ #
63
+ # ==== Returns
64
+ #
65
+ # Object:: Value of constant named
66
+ #
67
+ def full_const_get(string)
68
+ string.split('::').inject(Object) do |context, const_name|
69
+ context.const_get(const_name)
70
+ end
71
+ end
72
+
73
+ #
74
+ # Evaluate the given proc in the context of the given object if the
75
+ # block's arity is non-positive, or by passing the given object as an
76
+ # argument if it is negative.
77
+ #
78
+ # ==== Parameters
79
+ #
80
+ # object<Object>:: Object to pass to the proc
81
+ #
82
+ def instance_eval_or_call(object, &block)
83
+ if block.arity > 0
84
+ block.call(object)
85
+ else
86
+ object.instance_eval(&block)
87
+ end
88
+ end
89
+
90
+ #
91
+ # Perform a deep merge of hashes, returning the result as a new hash.
92
+ # See #deep_merge_into for rules used to merge the hashes
93
+ #
94
+ # ==== Parameters
95
+ #
96
+ # left<Hash>:: Hash to merge
97
+ # right<Hash>:: The other hash to merge
98
+ #
99
+ # ==== Returns
100
+ #
101
+ # Hash:: New hash containing the given hashes deep-merged.
102
+ #
103
+ def deep_merge(left, right)
104
+ deep_merge_into({}, left, right)
105
+ end
106
+
107
+ #
108
+ # Perform a deep merge of the right hash into the left hash
109
+ #
110
+ # ==== Parameters
111
+ #
112
+ # left:: Hash to receive merge
113
+ # right:: Hash to merge into left
114
+ #
115
+ # ==== Returns
116
+ #
117
+ # Hash:: left
118
+ #
119
+ def deep_merge!(left, right)
120
+ deep_merge_into(left, left, right)
121
+ end
122
+
123
+ private
124
+
125
+ #
126
+ # Deep merge two hashes into a third hash, using rules that produce nice
127
+ # merged parameter hashes. The rules are as follows, for a given key:
128
+ #
129
+ # * If only one hash has a value, or if both hashes have the same value,
130
+ # just use the value.
131
+ # * If either of the values is not a hash, create arrays out of both
132
+ # values and concatenate them.
133
+ # * Otherwise, deep merge the two values (which are both hashes)
134
+ #
135
+ # ==== Parameters
136
+ #
137
+ # destination<Hash>:: Hash into which to perform the merge
138
+ # left<Hash>:: One hash to merge
139
+ # right<Hash>:: The other hash to merge
140
+ #
141
+ # ==== Returns
142
+ #
143
+ # Hash:: destination
144
+ #
145
+ def deep_merge_into(destination, left, right)
146
+ left.each_pair do |name, left_value|
147
+ right_value = right[name]
148
+ destination[name] =
149
+ if right_value.nil? || left_value == right_value
150
+ left_value
151
+ elsif !left_value.respond_to?(:each_pair) || !right_value.respond_to?(:each_pair)
152
+ Array(left_value) + Array(right_value)
153
+ else
154
+ merged_value = {}
155
+ deep_merge_into(merged_value, left_value, right_value)
156
+ end
157
+ end
158
+ left_keys = Set.new(left.keys)
159
+ destination.merge!(right.reject { |k, v| left_keys.include?(k) })
160
+ destination
161
+ end
162
+ end
163
+
164
+ class Coordinates #:nodoc:
165
+ def initialize(coords)
166
+ @coords = coords
167
+ end
168
+
169
+ def lat
170
+ if @coords.respond_to?(:[])
171
+ @coords[0]
172
+ else
173
+ @coords.lat
174
+ end.to_f
175
+ end
176
+
177
+ def lng
178
+ if @coords.respond_to?(:[])
179
+ @coords[1]
180
+ elsif @coords.respond_to?(:lng)
181
+ @coords.lng
182
+ elsif @coords.respond_to?(:lon)
183
+ @coords.lon
184
+ elsif @coords.respond_to?(:long)
185
+ @coords.long
186
+ end.to_f
187
+ end
188
+ end
189
+ end
190
+ end