outoftime-sunspot 0.8.9 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/README.rdoc +13 -21
  2. data/Rakefile +0 -2
  3. data/TODO +2 -15
  4. data/VERSION.yml +2 -2
  5. data/bin/sunspot-configure-solr +46 -0
  6. data/bin/sunspot-solr +15 -7
  7. data/lib/sunspot/adapters.rb +5 -1
  8. data/lib/sunspot/composite_setup.rb +186 -0
  9. data/lib/sunspot/configuration.rb +7 -1
  10. data/lib/sunspot/data_extractor.rb +10 -0
  11. data/lib/sunspot/date_facet.rb +36 -0
  12. data/lib/sunspot/date_facet_row.rb +17 -0
  13. data/lib/sunspot/dsl/field_query.rb +72 -0
  14. data/lib/sunspot/dsl/fields.rb +30 -3
  15. data/lib/sunspot/dsl/query.rb +16 -35
  16. data/lib/sunspot/dsl/query_facet.rb +31 -0
  17. data/lib/sunspot/dsl/scope.rb +76 -20
  18. data/lib/sunspot/dsl/search.rb +30 -0
  19. data/lib/sunspot/dsl.rb +1 -1
  20. data/lib/sunspot/facet.rb +17 -3
  21. data/lib/sunspot/facet_row.rb +4 -4
  22. data/lib/sunspot/field.rb +130 -207
  23. data/lib/sunspot/field_factory.rb +126 -0
  24. data/lib/sunspot/indexer.rb +61 -14
  25. data/lib/sunspot/instantiated_facet.rb +38 -0
  26. data/lib/sunspot/instantiated_facet_row.rb +12 -0
  27. data/lib/sunspot/query/base_query.rb +90 -0
  28. data/lib/sunspot/query/connective.rb +77 -0
  29. data/lib/sunspot/query/dynamic_query.rb +39 -56
  30. data/lib/sunspot/query/field_facet.rb +132 -4
  31. data/lib/sunspot/query/field_query.rb +57 -0
  32. data/lib/sunspot/query/pagination.rb +1 -1
  33. data/lib/sunspot/query/query_facet.rb +72 -0
  34. data/lib/sunspot/query/query_facet_row.rb +19 -0
  35. data/lib/sunspot/query/restriction.rb +9 -7
  36. data/lib/sunspot/query/scope.rb +165 -0
  37. data/lib/sunspot/query/sort.rb +17 -14
  38. data/lib/sunspot/query/sort_composite.rb +33 -0
  39. data/lib/sunspot/query.rb +162 -351
  40. data/lib/sunspot/query_facet.rb +33 -0
  41. data/lib/sunspot/query_facet_row.rb +21 -0
  42. data/lib/sunspot/schema.rb +165 -0
  43. data/lib/sunspot/search/hit.rb +62 -0
  44. data/lib/sunspot/search.rb +104 -41
  45. data/lib/sunspot/session.rb +64 -32
  46. data/lib/sunspot/setup.rb +119 -48
  47. data/lib/sunspot/type.rb +48 -2
  48. data/lib/sunspot.rb +74 -8
  49. data/solr/solr/conf/schema.xml +44 -225
  50. data/spec/api/build_search_spec.rb +557 -63
  51. data/spec/api/indexer_spec.rb +156 -74
  52. data/spec/api/query_spec.rb +55 -31
  53. data/spec/api/search_retrieval_spec.rb +210 -33
  54. data/spec/api/session_spec.rb +81 -26
  55. data/spec/api/sunspot_spec.rb +5 -7
  56. data/spec/integration/faceting_spec.rb +130 -0
  57. data/spec/integration/keyword_search_spec.rb +72 -31
  58. data/spec/integration/scoped_search_spec.rb +13 -0
  59. data/spec/integration/stored_fields_spec.rb +10 -0
  60. data/spec/mocks/blog.rb +3 -0
  61. data/spec/mocks/comment.rb +12 -23
  62. data/spec/mocks/connection.rb +84 -0
  63. data/spec/mocks/mock_adapter.rb +11 -3
  64. data/spec/mocks/mock_record.rb +41 -0
  65. data/spec/mocks/photo.rb +8 -0
  66. data/spec/mocks/post.rb +18 -23
  67. data/spec/spec_helper.rb +29 -14
  68. data/tasks/gemspec.rake +4 -3
  69. data/tasks/rdoc.rake +2 -2
  70. data/tasks/schema.rake +19 -0
  71. data/templates/schema.xml.haml +24 -0
  72. metadata +48 -7
  73. data/spec/mocks/base_class.rb +0 -2
@@ -0,0 +1,126 @@
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
+ end
126
+ end
@@ -6,8 +6,10 @@ module Sunspot
6
6
  # subclasses).
7
7
  #
8
8
  class Indexer #:nodoc:
9
- def initialize(connection, setup)
10
- @connection, @setup = connection, setup
9
+ include RSolr::Char
10
+
11
+ def initialize(connection)
12
+ @connection = connection
11
13
  end
12
14
 
13
15
  #
@@ -19,34 +21,62 @@ module Sunspot
19
21
  # model<Object>:: the model to index
20
22
  #
21
23
  def add(model)
22
- @connection.add(Array(model).map { |m| prepare(m) })
24
+ documents = Array(model).map { |m| prepare(m) }
25
+ if @batch.nil?
26
+ add_documents(documents)
27
+ else
28
+ @batch.concat(documents)
29
+ end
23
30
  end
24
31
 
25
32
  #
26
33
  # Remove the given model from the Solr index
27
34
  #
28
35
  def remove(model)
29
- @connection.delete(Adapters::InstanceAdapter.adapt(model).index_id)
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
+ )
30
43
  end
31
44
 
32
45
  #
33
46
  # Delete all documents of the class indexed by this indexer from Solr.
34
47
  #
35
- def remove_all
36
- @connection.delete_by_query("type:#{Solr::Util.query_parser_escape(@setup.clazz.name)}")
48
+ def remove_all(clazz)
49
+ @connection.delete_by_query("type:#{escape(clazz.name)}")
50
+ end
51
+
52
+ def start_batch
53
+ @batch = []
54
+ end
55
+
56
+ def flush_batch
57
+ add_documents(@batch)
58
+ @batch = nil
37
59
  end
38
60
 
39
- protected
61
+ private
40
62
 
41
63
  #
42
64
  # Convert documents into hash of indexed properties
43
65
  #
44
66
  def prepare(model)
45
- hash = static_hash_for(model)
46
- for field in @setup.all_fields
47
- hash.merge!(field.pairs_for(model))
67
+ document = document_for(model)
68
+ setup = setup_for(model)
69
+ if boost = setup.document_boost_for(model)
70
+ document.attrs[:boost] = boost
48
71
  end
49
- hash
72
+ for field_factory in setup.all_field_factories
73
+ field_factory.populate_document(document, model)
74
+ end
75
+ document
76
+ end
77
+
78
+ def add_documents(documents)
79
+ @connection.add(documents)
50
80
  end
51
81
 
52
82
  #
@@ -54,9 +84,26 @@ module Sunspot
54
84
  # This method constructs the document hash containing those key-value
55
85
  # pairs.
56
86
  #
57
- def static_hash_for(model)
58
- { :id => Adapters::InstanceAdapter.adapt(model).index_id,
59
- :type => Util.superclasses_for(model.class).map { |clazz| clazz.name }}
87
+ def document_for(model)
88
+ RSolr::Message::Document.new(
89
+ :id => Adapters::InstanceAdapter.adapt(model).index_id,
90
+ :type => Util.superclasses_for(model.class).map { |clazz| clazz.name }
91
+ )
92
+ end
93
+
94
+ #
95
+ # Get the Setup object for the given object's class.
96
+ #
97
+ # ==== Parameters
98
+ #
99
+ # object<Object>:: The object whose setup is to be retrieved
100
+ #
101
+ # ==== Returns
102
+ #
103
+ # Sunspot::Setup:: The setup for the object's class
104
+ #
105
+ def setup_for(object)
106
+ Setup.for(object.class) || raise(NoSetupError, "Sunspot is not configured for #{object.class.inspect}")
60
107
  end
61
108
 
62
109
 
@@ -0,0 +1,38 @@
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(@field.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
+ private
30
+
31
+ #
32
+ # Override the Facet#new_row method to return an InstantiateFacetRow
33
+ #
34
+ def new_row(pair)
35
+ InstantiatedFacetRow.new(pair, self)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ module Sunspot
2
+ class InstantiatedFacetRow < FacetRow
3
+ attr_writer :instance
4
+
5
+ def instance
6
+ unless defined?(@instance)
7
+ @facet.populate_instances!
8
+ end
9
+ @instance
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,90 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # Encapsulates information common to all queries - in particular, keywords
5
+ # and types.
6
+ #
7
+ class BaseQuery #:nodoc:
8
+ include RSolr::Char
9
+
10
+ attr_writer :keywords
11
+
12
+ def initialize(setup)
13
+ @setup = setup
14
+ end
15
+
16
+ #
17
+ # Generate params for the base query. If keywords are specified, build
18
+ # params for a dismax query, request all stored fields plus the score,
19
+ # and put the types in a filter query. If keywords are not specified,
20
+ # put the types query in the q parameter.
21
+ #
22
+ def to_params
23
+ params = {}
24
+ if @keywords
25
+ params[:q] = @keywords
26
+ params[:fl] = '* score'
27
+ params[:fq] = types_phrase
28
+ params[:qf] = text_field_names.join(' ')
29
+ params[:defType] = 'dismax'
30
+ else
31
+ params[:q] = types_phrase
32
+ end
33
+ params
34
+ end
35
+
36
+ #
37
+ # Set keyword options
38
+ #
39
+ def keyword_options=(options)
40
+ if options
41
+ @text_field_names = options.delete(:fields)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ #
48
+ # Boolean phrase that restricts results to objects of the type(s) under
49
+ # query. If this is an open query (no types specified) then it sends a
50
+ # no-op phrase because Solr requires that the :q parameter not be empty.
51
+ #
52
+ # ==== Returns
53
+ #
54
+ # String:: Boolean phrase for type restriction
55
+ #
56
+ def types_phrase
57
+ if escaped_types.length == 1 then "type:#{escaped_types.first}"
58
+ else "type:(#{escaped_types * ' OR '})"
59
+ end
60
+ end
61
+
62
+ #
63
+ # Wraps each type in quotes to escape names of the form Namespace::Class
64
+ #
65
+ def escaped_types
66
+ @escaped_types ||=
67
+ @setup.type_names.map { |name| escape(name)}
68
+ end
69
+
70
+ #
71
+ # Returns the names of text fields that should be queried in a keyword
72
+ # search. If specific fields are requested, use those; otherwise use the
73
+ # union of all fields configured for the types under search.
74
+ #
75
+ def text_field_names
76
+ text_fields =
77
+ if @text_field_names
78
+ Array(@text_field_names).map do |field_name|
79
+ @setup.text_field(field_name.to_sym)
80
+ end
81
+ else
82
+ @setup.text_fields
83
+ end
84
+ text_fields.map do |text_field|
85
+ text_field.indexed_name
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,77 @@
1
+ module Sunspot
2
+ module Query
3
+ module Connective #:nodoc:
4
+ #
5
+ # Base class for connectives (conjunctions and disjunctions).
6
+ #
7
+ class Abstract < Scope
8
+ def initialize(setup) #:nodoc:
9
+ @setup = setup
10
+ @components = []
11
+ end
12
+
13
+ #
14
+ # Connective as solr params.
15
+ #
16
+ def to_params #:nodoc:
17
+ { :fq => to_boolean_phrase }
18
+ end
19
+
20
+ #
21
+ # Express the connective as a Lucene boolean phrase.
22
+ #
23
+ def to_boolean_phrase #:nodoc:
24
+ if @components.length == 1
25
+ @components.first.to_boolean_phrase
26
+ else
27
+ component_phrases = @components.map do |component|
28
+ component.to_boolean_phrase
29
+ end
30
+ "(#{component_phrases.join(" #{connector} ")})"
31
+ end
32
+ end
33
+
34
+ #
35
+ # Add a component to the connective. All components must implement the
36
+ # #to_boolean_phrase method.
37
+ #
38
+ def add_component(component) #:nodoc:
39
+ @components << component
40
+ end
41
+ end
42
+
43
+ #
44
+ # Disjunctions combine their components with an OR operator.
45
+ #
46
+ class Disjunction < Abstract
47
+ #
48
+ # Add a conjunction to the disjunction. This overrides the method in
49
+ # the Scope class since scopes are implicitly conjunctive and thus
50
+ # can return themselves as a conjunction. Inside a disjunction, however,
51
+ # a conjunction must explicitly be created.
52
+ #
53
+ def add_conjunction
54
+ @components << conjunction = Conjunction.new(setup)
55
+ conjunction
56
+ end
57
+
58
+ private
59
+
60
+ def connector
61
+ 'OR'
62
+ end
63
+ end
64
+
65
+ #
66
+ # Conjunctions combine their components with an AND operator.
67
+ #
68
+ class Conjunction < Abstract
69
+ private
70
+
71
+ def connector
72
+ 'AND'
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,9 +1,9 @@
1
1
  module Sunspot
2
- class Query
2
+ module Query
3
3
  #
4
- # A dynamic query is a proxy object that implements a subset of the API of
5
- # the Query class, but wraps a dynamic field definition and thus applies the
6
- # query components using dynamic field instances.
4
+ # A dynamic query is a proxy object that implements the API of the FieldQuery
5
+ # class, but wraps a dynamic field factory and thus applies the query
6
+ # components using dynamic field instances.
7
7
  #--
8
8
  # Dynamic queries do not hold their own state, but rather proxy to the query
9
9
  # that generated them, adding components directly to the owning query's
@@ -12,74 +12,57 @@ module Sunspot
12
12
  # DynamicQuery instances are publicly generated by the Query#dynamic_query
13
13
  # factory method.
14
14
  #
15
- class DynamicQuery
16
- def initialize(dynamic_field, query) #:nodoc:
17
- @dynamic_field, @query = dynamic_field, query
15
+ class DynamicQuery < FieldQuery
16
+ def initialize(dynamic_field_factory, query) #:nodoc:
17
+ @dynamic_field_factory, @query = dynamic_field_factory, query
18
18
  end
19
-
19
+
20
20
  #
21
- # Add a restriction based on the dynamic field definition and dynamic name
22
- # given.
23
- #
24
- # ==== Parameters
21
+ # This has the same effect as calling Query#exclude_instance; it is
22
+ # included for interface completeness.
25
23
  #
26
- # dynamic_name<Symbol>::
27
- # Dynamic name to apply to the field in the restriction.
28
- # restriction_type<Symbol,Class>::
29
- # Type of restriction to apply (e.g. Sunspot::Query::Restriction::EqualTo), or
30
- # symbol shorthand (e.g. :equal_to)
31
- # value::
32
- # Value to apply to the restriction.
33
- # negated::
34
- # Whether to negate the restriction (prefer #add_negated_restriction)
35
- #
36
- def add_restriction(dynamic_name, restriction_type, value, negated = false)
37
- if restriction_type.is_a?(Symbol)
38
- restriction_type = Restriction[restriction_type]
39
- end
40
- @query.add_component(restriction_type.new(@dynamic_field.build(dynamic_name), value, negated))
24
+ def exclude_instance(instance)
25
+ @query.exclude_instance(instance)
41
26
  end
42
27
 
43
28
  #
44
- # Add a negated restriction based on the dynamic field definition and
45
- # dynamic name given.
46
- #
47
- # ==== Parameters
48
- #
49
- # dynamic_name<Symbol>::
50
- # Dynamic name to apply to the field in the restriction.
51
- # restriction_type<Symbol,Class>::
52
- # Type of restriction to apply (e.g. Sunspot::Query::Restriction::EqualTo), or
53
- # symbol shorthand (e.g. :equal_to)
54
- # value::
55
- # Value to apply to the restriction.
56
- #
57
- def add_negated_restriction(dynamic_name, restriction_type, value)
58
- add_restriction(dynamic_name, restriction_type, value, true)
29
+ # This has the same effect as calling Query#exclude_instance; it is
30
+ # included for interface completeness.
31
+ #
32
+ def dynamic_query(field_name)
33
+ @query.dynamic_query(field_name)
59
34
  end
60
35
 
61
36
  #
62
- # Add a field facet based on the dynamic field definition and dynamic name
63
- # given.
37
+ # Add a Sort to the query
64
38
  #
65
- # ==== Parameters
66
- #
67
- # dynamic_name<Symbol>:: Dynamic name to facet on
68
- #
69
- def add_field_facet(dynamic_name)
70
- @query.add_component(FieldFacet.new(@dynamic_field.build(dynamic_name)))
39
+ def add_sort(sort) #:nodoc:
40
+ @query.add_sort(sort)
71
41
  end
72
42
 
73
43
  #
74
- # Order by the given dynamic field.
44
+ # Add a component to the query
75
45
  #
76
- # ==== Parameters
46
+ def add_component(component) #:nodoc:
47
+ @query.add_component(component)
48
+ end
49
+
50
+ private
51
+
77
52
  #
78
- # dynamic_name<Symbol>:: Dynamic name of ordering field
79
- # direction<Symbol>:: Direction in which to order (:asc, :desc)
53
+ # DynamicFieldFactory implements the part of the Setup interface that we
54
+ # need, so methods in DynamicQuery's superclasses can rely on it without
55
+ # knowing what it is.
56
+ #
57
+ def setup
58
+ @dynamic_field_factory
59
+ end
60
+
80
61
  #
81
- def order_by(dynamic_name, direction)
82
- @query.add_component(Sort.new(@dynamic_field.build(dynamic_name), direction))
62
+ # So query facets can be added to the query from within dynamic queries
63
+ #
64
+ def query_facets
65
+ @query.query_facets
83
66
  end
84
67
  end
85
68
  end