nxa-sunspot 0.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/History.txt +153 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +191 -0
  4. data/Rakefile +9 -0
  5. data/TODO +14 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +40 -0
  8. data/bin/sunspot-solr +95 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot.rb +488 -0
  11. data/lib/sunspot/adapters.rb +265 -0
  12. data/lib/sunspot/composite_setup.rb +188 -0
  13. data/lib/sunspot/configuration.rb +56 -0
  14. data/lib/sunspot/data_extractor.rb +50 -0
  15. data/lib/sunspot/dsl.rb +4 -0
  16. data/lib/sunspot/dsl/field_query.rb +150 -0
  17. data/lib/sunspot/dsl/fields.rb +100 -0
  18. data/lib/sunspot/dsl/fulltext.rb +228 -0
  19. data/lib/sunspot/dsl/query.rb +162 -0
  20. data/lib/sunspot/dsl/query_facet.rb +36 -0
  21. data/lib/sunspot/dsl/restriction.rb +25 -0
  22. data/lib/sunspot/dsl/scope.rb +225 -0
  23. data/lib/sunspot/dsl/search.rb +30 -0
  24. data/lib/sunspot/field.rb +181 -0
  25. data/lib/sunspot/field_factory.rb +144 -0
  26. data/lib/sunspot/indexer.rb +133 -0
  27. data/lib/sunspot/query.rb +9 -0
  28. data/lib/sunspot/query/abstract_field_facet.rb +43 -0
  29. data/lib/sunspot/query/boost_query.rb +20 -0
  30. data/lib/sunspot/query/connective.rb +189 -0
  31. data/lib/sunspot/query/date_field_facet.rb +14 -0
  32. data/lib/sunspot/query/dismax.rb +88 -0
  33. data/lib/sunspot/query/field_facet.rb +9 -0
  34. data/lib/sunspot/query/highlighting.rb +55 -0
  35. data/lib/sunspot/query/local.rb +27 -0
  36. data/lib/sunspot/query/pagination.rb +38 -0
  37. data/lib/sunspot/query/query.rb +86 -0
  38. data/lib/sunspot/query/query_facet.rb +16 -0
  39. data/lib/sunspot/query/restriction.rb +254 -0
  40. data/lib/sunspot/query/scope.rb +9 -0
  41. data/lib/sunspot/query/sort.rb +105 -0
  42. data/lib/sunspot/query/sort_composite.rb +33 -0
  43. data/lib/sunspot/query/text_field_boost.rb +15 -0
  44. data/lib/sunspot/schema.rb +147 -0
  45. data/lib/sunspot/search.rb +216 -0
  46. data/lib/sunspot/search/date_facet.rb +35 -0
  47. data/lib/sunspot/search/facet_row.rb +27 -0
  48. data/lib/sunspot/search/field_facet.rb +44 -0
  49. data/lib/sunspot/search/highlight.rb +38 -0
  50. data/lib/sunspot/search/hit.rb +117 -0
  51. data/lib/sunspot/search/query_facet.rb +62 -0
  52. data/lib/sunspot/session.rb +236 -0
  53. data/lib/sunspot/setup.rb +323 -0
  54. data/lib/sunspot/text_field_setup.rb +29 -0
  55. data/lib/sunspot/type.rb +204 -0
  56. data/lib/sunspot/util.rb +210 -0
  57. data/solr/etc/jetty.xml +212 -0
  58. data/solr/etc/webdefault.xml +379 -0
  59. data/solr/lib/jetty-6.1.3.jar +0 -0
  60. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  61. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  62. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  63. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  64. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  65. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  66. data/solr/solr/conf/elevate.xml +36 -0
  67. data/solr/solr/conf/protwords.txt +21 -0
  68. data/solr/solr/conf/schema.xml +64 -0
  69. data/solr/solr/conf/solrconfig.xml +725 -0
  70. data/solr/solr/conf/stopwords.txt +57 -0
  71. data/solr/solr/conf/synonyms.txt +31 -0
  72. data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
  73. data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
  74. data/solr/solr/lib/jsr108-0.01.jar +0 -0
  75. data/solr/solr/lib/locallucene.jar +0 -0
  76. data/solr/solr/lib/localsolr.jar +0 -0
  77. data/solr/start.jar +0 -0
  78. data/solr/webapps/solr.war +0 -0
  79. data/spec/api/adapters_spec.rb +33 -0
  80. data/spec/api/indexer/attributes_spec.rb +110 -0
  81. data/spec/api/indexer/batch_spec.rb +46 -0
  82. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  83. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  84. data/spec/api/indexer/fulltext_spec.rb +43 -0
  85. data/spec/api/indexer/removal_spec.rb +46 -0
  86. data/spec/api/indexer/spec_helper.rb +1 -0
  87. data/spec/api/indexer_spec.rb +4 -0
  88. data/spec/api/query/adjust_params_spec.rb +37 -0
  89. data/spec/api/query/connectives_spec.rb +176 -0
  90. data/spec/api/query/dsl_spec.rb +12 -0
  91. data/spec/api/query/dynamic_fields_spec.rb +149 -0
  92. data/spec/api/query/faceting_spec.rb +296 -0
  93. data/spec/api/query/fulltext_spec.rb +281 -0
  94. data/spec/api/query/highlighting_spec.rb +225 -0
  95. data/spec/api/query/local_spec.rb +62 -0
  96. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  97. data/spec/api/query/scope_spec.rb +266 -0
  98. data/spec/api/query/spec_helper.rb +1 -0
  99. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  100. data/spec/api/query/types_spec.rb +20 -0
  101. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  102. data/spec/api/search/faceting_spec.rb +242 -0
  103. data/spec/api/search/highlighting_spec.rb +65 -0
  104. data/spec/api/search/hits_spec.rb +67 -0
  105. data/spec/api/search/results_spec.rb +52 -0
  106. data/spec/api/search/search_spec.rb +23 -0
  107. data/spec/api/search/spec_helper.rb +1 -0
  108. data/spec/api/session_spec.rb +198 -0
  109. data/spec/api/spec_helper.rb +1 -0
  110. data/spec/api/sunspot_spec.rb +18 -0
  111. data/spec/helpers/indexer_helper.rb +29 -0
  112. data/spec/helpers/query_helper.rb +13 -0
  113. data/spec/helpers/search_helper.rb +78 -0
  114. data/spec/integration/dynamic_fields_spec.rb +55 -0
  115. data/spec/integration/faceting_spec.rb +188 -0
  116. data/spec/integration/highlighting_spec.rb +22 -0
  117. data/spec/integration/indexing_spec.rb +7 -0
  118. data/spec/integration/keyword_search_spec.rb +245 -0
  119. data/spec/integration/local_search_spec.rb +56 -0
  120. data/spec/integration/scoped_search_spec.rb +303 -0
  121. data/spec/integration/spec_helper.rb +7 -0
  122. data/spec/integration/stored_fields_spec.rb +10 -0
  123. data/spec/integration/test_pagination.rb +32 -0
  124. data/spec/mocks/adapters.rb +32 -0
  125. data/spec/mocks/blog.rb +3 -0
  126. data/spec/mocks/comment.rb +19 -0
  127. data/spec/mocks/connection.rb +106 -0
  128. data/spec/mocks/mock_adapter.rb +30 -0
  129. data/spec/mocks/mock_record.rb +48 -0
  130. data/spec/mocks/photo.rb +11 -0
  131. data/spec/mocks/post.rb +75 -0
  132. data/spec/mocks/super_class.rb +2 -0
  133. data/spec/mocks/user.rb +8 -0
  134. data/spec/spec_helper.rb +68 -0
  135. data/tasks/gemspec.rake +42 -0
  136. data/tasks/rcov.rake +28 -0
  137. data/tasks/rdoc.rake +22 -0
  138. data/tasks/schema.rake +19 -0
  139. data/tasks/spec.rake +24 -0
  140. data/tasks/todo.rake +4 -0
  141. data/templates/schema.xml.erb +36 -0
  142. metadata +319 -0
@@ -0,0 +1,30 @@
1
+ module Sunspot
2
+ module DSL
3
+ #
4
+ # This top-level DSL class is the context in which the block passed to
5
+ # Sunspot.query. See Sunspot::DSL::Query, Sunspot::DSL::FieldQuery, and
6
+ # Sunspot::DSL::Scope for the full API presented.
7
+ #
8
+ class Search < Query
9
+ def initialize(search, setup) #:nodoc:
10
+ @search = search
11
+ super(search, search.query, setup)
12
+ end
13
+
14
+ #
15
+ # Retrieve the data accessor used to load instances of the given class
16
+ # out of persistent storage. Data accessors are free to implement any
17
+ # extra methods that may be useful in this context.
18
+ #
19
+ # ==== Example
20
+ #
21
+ # Sunspot.search Post do
22
+ # data_acccessor_for(Post).includes = [:blog, :comments]
23
+ # end
24
+ #
25
+ def data_accessor_for(clazz)
26
+ @search.data_accessor_for(clazz)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,181 @@
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, :default_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
+ @default_boost = options.delete(:default_boost)
109
+ raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless options.empty?
110
+ end
111
+
112
+ def indexed_name
113
+ "#{super}#{'s' if @stored}"
114
+ end
115
+ end
116
+
117
+ #
118
+ # AttributeField instances encapsulate non-tokenized attribute data.
119
+ # AttributeFields can have any type except TextType, and can also have
120
+ # a reference (for instantiated facets), optionally allow multiple values
121
+ # (false by default), and can store their values (false by default). All
122
+ # scoping, sorting, and faceting is done with attribute fields.
123
+ #
124
+ class AttributeField < Field #:nodoc:
125
+ def initialize(name, type, options = {})
126
+ super(name, type, options)
127
+ @multiple = !!options.delete(:multiple)
128
+ @reference =
129
+ if (reference = options.delete(:references)).respond_to?(:name)
130
+ reference.name
131
+ elsif reference.respond_to?(:to_sym)
132
+ reference.to_sym
133
+ end
134
+ raise ArgumentError, "Unknown field option #{options.keys.first.inspect} provided for field #{name.inspect}" unless options.empty?
135
+ end
136
+
137
+ # The name of the field as it is indexed in Solr. The indexed name
138
+ # contains a suffix that contains information about the type as well as
139
+ # whether the field allows multiple values for a document.
140
+ #
141
+ # ==== Returns
142
+ #
143
+ # String:: The field's indexed name
144
+ #
145
+ def indexed_name
146
+ "#{super}#{'m' if @multiple}#{'s' if @stored}"
147
+ end
148
+ end
149
+
150
+ class TypeField #:nodoc:
151
+ class <<self
152
+ def instance
153
+ @instance ||= new
154
+ end
155
+ end
156
+
157
+ def indexed_name
158
+ 'type'
159
+ end
160
+
161
+ def to_indexed(clazz)
162
+ clazz.name
163
+ end
164
+ end
165
+
166
+ class IdField #:nodoc:
167
+ class <<self
168
+ def instance
169
+ @instance ||= new
170
+ end
171
+ end
172
+
173
+ def indexed_name
174
+ 'id'
175
+ end
176
+
177
+ def to_indexed(id)
178
+ id.to_s
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,144 @@
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
+ Util.Array(@field.to_indexed(value)).each do |scalar_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
+ Util.Array(field_instance.to_indexed(value)).each do |scalar_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
+ class Coordinates
127
+ def initialize(name = nil, &block)
128
+ if block
129
+ @data_extractor = DataExtractor::BlockExtractor.new(&block)
130
+ else
131
+ @data_extractor = DataExtractor::AttributeExtractor.new(name)
132
+ end
133
+ end
134
+
135
+ def populate_document(document, model)
136
+ if coordinates = @data_extractor.value_for(model)
137
+ coordinates = Util::Coordinates.new(coordinates)
138
+ document.add_field(:lat, coordinates.lat)
139
+ document.add_field(:long, coordinates.lng)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,133 @@
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 = Util.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
+ setup.all_field_factories.each do |field_factory|
79
+ field_factory.populate_document(document, model)
80
+ end
81
+ document
82
+ end
83
+
84
+ def add_documents(documents)
85
+ begin
86
+ @connection.add(documents)
87
+ rescue Exception => e
88
+ puts e
89
+ end
90
+ end
91
+
92
+ #
93
+ # All indexed documents index and store the +id+ and +type+ fields.
94
+ # This method constructs the document hash containing those key-value
95
+ # pairs.
96
+ #
97
+ def document_for(model)
98
+ RSolr::Message::Document.new(
99
+ :id => Adapters::InstanceAdapter.adapt(model).index_id,
100
+ :type => Util.superclasses_for(model.class).map { |clazz| clazz.name }
101
+ )
102
+ end
103
+
104
+ #
105
+ # Get the Setup object for the given object's class.
106
+ #
107
+ # ==== Parameters
108
+ #
109
+ # object<Object>:: The object whose setup is to be retrieved
110
+ #
111
+ # ==== Returns
112
+ #
113
+ # Sunspot::Setup:: The setup for the object's class
114
+ #
115
+ def setup_for(object)
116
+ Setup.for(object.class) || raise(NoSetupError, "Sunspot is not configured for #{object.class.inspect}")
117
+ end
118
+
119
+
120
+ class <<self
121
+ #
122
+ # Delete all documents from the Solr index
123
+ #
124
+ # ==== Parameters
125
+ #
126
+ # connection<Solr::Connection>::
127
+ # connection to which to send the delete request
128
+ def remove_all(connection)
129
+ connection.delete_by_query("type:[* TO *]")
130
+ end
131
+ end
132
+ end
133
+ end