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,9 @@
1
+ %w(abstract_field_facet connective boost_query date_field_facet dismax
2
+ field_facet highlighting local pagination restriction query query_facet
3
+ scope sort sort_composite text_field_boost).each do |file|
4
+ require(File.join(File.dirname(__FILE__), 'query', file))
5
+ end
6
+ module Sunspot
7
+ module Query #:nodoc:all
8
+ end
9
+ end
@@ -0,0 +1,43 @@
1
+ module Sunspot
2
+ module Query
3
+ class AbstractFieldFacet
4
+ def initialize(field, options)
5
+ @field, @options = field, options
6
+ end
7
+
8
+ def to_params
9
+ params = {
10
+ :facet => 'true',
11
+ }
12
+ case @options[:sort]
13
+ when :count
14
+ params[qualified_param('sort')] = 'true'
15
+ when :index
16
+ params[qualified_param('sort')] = 'false'
17
+ when nil
18
+ else
19
+ raise(
20
+ ArgumentError,
21
+ "#{@options[:sort].inspect} is not an allowed value for :sort. Allowed options are :count and :index"
22
+ )
23
+ end
24
+ if @options[:limit]
25
+ params[qualified_param('limit')] = @options[:limit].to_i
26
+ end
27
+ params[qualified_param('mincount')] =
28
+ case
29
+ when @options[:minimum_count] then @options[:minimum_count].to_i
30
+ when @options[:zeros] then 0
31
+ else 1
32
+ end
33
+ params
34
+ end
35
+
36
+ private
37
+
38
+ def qualified_param(param)
39
+ :"f.#{@field.indexed_name}.facet.#{param}"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # Representation of a BoostQuery, which allows the searcher to specify a
5
+ # scope for which matching documents should have an extra boost. This is
6
+ # essentially a conjunction, with an extra instance variable containing
7
+ # the boost that should be applied.
8
+ #
9
+ class BoostQuery < Connective::Conjunction #:nodoc:
10
+ def initialize(boost)
11
+ super(false)
12
+ @boost = boost
13
+ end
14
+
15
+ def to_boolean_phrase
16
+ "#{super}^#{@boost}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,189 @@
1
+ module Sunspot
2
+ module Query
3
+ module Connective #:nodoc:all
4
+ #
5
+ # Base class for connectives (conjunctions and disjunctions).
6
+ #
7
+ class Abstract
8
+ def initialize(negated = false) #:nodoc:
9
+ @negated = negated
10
+ @components = []
11
+ end
12
+
13
+ #
14
+ # Add a restriction to the connective.
15
+ #
16
+ def add_restriction(field, restriction_type, value, negated = false)
17
+ @components << restriction_type.new(field, value, negated)
18
+ end
19
+
20
+ #
21
+ # Add a shorthand restriction; the restriction type is determined by
22
+ # the value.
23
+ #
24
+ def add_shorthand_restriction(field, value, negated = false)
25
+ restriction_type =
26
+ case value
27
+ when Array then Restriction::AnyOf
28
+ when Range then Restriction::Between
29
+ else Restriction::EqualTo
30
+ end
31
+ add_restriction(field, restriction_type, value, negated)
32
+ end
33
+
34
+ #
35
+ # Add a negated restriction. The added restriction will match all
36
+ # documents who do not match the terms of the restriction.
37
+ #
38
+ def add_negated_restriction(field, restriction_type, value)
39
+ add_restriction(field, restriction_type, value, true)
40
+ end
41
+
42
+ #
43
+ # Add a negated shorthand restriction (see add_shorthand_restriction)
44
+ #
45
+ def add_negated_shorthand_restriction(field, value)
46
+ add_shorthand_restriction(field, value, true)
47
+ end
48
+
49
+ #
50
+ # Add a new conjunction and return it.
51
+ #
52
+ def add_conjunction
53
+ add_component(Conjunction.new)
54
+ end
55
+
56
+ #
57
+ # Add a new disjunction and return it.
58
+ #
59
+ def add_disjunction
60
+ add_component(Disjunction.new)
61
+ end
62
+
63
+ #
64
+ # Add an arbitrary component to the conjunction, and return it.
65
+ # The component must respond to #to_boolean_phrase
66
+ #
67
+ def add_component(component)
68
+ @components << component
69
+ component
70
+ end
71
+
72
+ #
73
+ # Express the connective as a Lucene boolean phrase.
74
+ #
75
+ def to_boolean_phrase #:nodoc:
76
+ unless @components.empty?
77
+ phrase =
78
+ if @components.length == 1
79
+ @components.first.to_boolean_phrase
80
+ else
81
+ component_phrases = @components.map do |component|
82
+ component.to_boolean_phrase
83
+ end
84
+ "(#{component_phrases.join(" #{connector} ")})"
85
+ end
86
+ if negated?
87
+ "-#{phrase}"
88
+ else
89
+ phrase
90
+ end
91
+ end
92
+ end
93
+
94
+ #
95
+ # Connectives can be negated during the process of denormalization that
96
+ # is performed when a disjunction contains a negated component. This
97
+ # method conforms to the duck type for all boolean query components.
98
+ #
99
+ def negated?
100
+ @negated
101
+ end
102
+
103
+ #
104
+ # Returns a new connective that's a negated version of this one.
105
+ #
106
+ def negate
107
+ negated = self.class.new(!negated?)
108
+ @components.each do |component|
109
+ negated.add_component(component)
110
+ end
111
+ negated
112
+ end
113
+ end
114
+
115
+ #
116
+ # Disjunctions combine their components with an OR operator.
117
+ #
118
+ class Disjunction < Abstract
119
+ class <<self
120
+ def inverse
121
+ Conjunction
122
+ end
123
+ end
124
+
125
+ #
126
+ # Express this disjunction as a Lucene boolean phrase
127
+ #
128
+ def to_boolean_phrase
129
+ if @components.any? { |component| component.negated? }
130
+ denormalize.to_boolean_phrase
131
+ else
132
+ super
133
+ end
134
+ end
135
+
136
+ #
137
+ # No-op - this is already a disjunction
138
+ #
139
+ def add_disjunction
140
+ self
141
+ end
142
+
143
+ private
144
+
145
+ def connector
146
+ 'OR'
147
+ end
148
+
149
+ #
150
+ # If a disjunction contains negated components, it must be
151
+ # "denormalized", because the Lucene parser interprets any negated
152
+ # boolean phrase using AND semantics (this isn't a bug, it's just a
153
+ # subtlety of how Lucene parses queries). So, per DeMorgan's law we
154
+ # create a negated conjunction and add to it all of our components,
155
+ # negated themselves, which creates a query whose Lucene semantics are
156
+ # in line with our intentions.
157
+ #
158
+ def denormalize
159
+ denormalized = self.class.inverse.new(!negated?)
160
+ @components.each do |component|
161
+ denormalized.add_component(component.negate)
162
+ end
163
+ denormalized
164
+ end
165
+ end
166
+
167
+ #
168
+ # Conjunctions combine their components with an AND operator.
169
+ #
170
+ class Conjunction < Abstract
171
+ class <<self
172
+ def inverse
173
+ Disjunction
174
+ end
175
+ end
176
+
177
+ def add_conjunction
178
+ self
179
+ end
180
+
181
+ private
182
+
183
+ def connector
184
+ 'AND'
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,14 @@
1
+ module Sunspot
2
+ module Query
3
+ class DateFieldFacet < AbstractFieldFacet
4
+ def to_params
5
+ params = super
6
+ params[:"facet.date"] = [@field.indexed_name]
7
+ params[qualified_param('date.start')] = @field.to_indexed(@options[:time_range].first)
8
+ params[qualified_param('date.end')] = @field.to_indexed(@options[:time_range].last)
9
+ params[qualified_param('date.gap')] = "+#{@options[:time_interval] || 86400}SECONDS"
10
+ params
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,88 @@
1
+ module Sunspot
2
+ module Query
3
+ class Dismax
4
+ attr_writer :minimum_match, :phrase_slop, :query_phrase_slop, :tie
5
+
6
+ def initialize(keywords)
7
+ @keywords = keywords
8
+ @fulltext_fields = {}
9
+ @boost_queries = []
10
+ @highlights = []
11
+ end
12
+
13
+ #
14
+ # The query as Solr parameters
15
+ #
16
+ def to_params
17
+ params = { :q => @keywords }
18
+ params[:fl] = '* score'
19
+ params[:qf] = @fulltext_fields.values.map { |field| field.to_boosted_field }.join(' ')
20
+ params[:defType] = 'dismax'
21
+ if @phrase_fields
22
+ params[:pf] = @phrase_fields.map { |field| field.to_boosted_field }.join(' ')
23
+ end
24
+ unless @boost_queries.empty?
25
+ params[:bq] = @boost_queries.map do |boost_query|
26
+ boost_query.to_boolean_phrase
27
+ end
28
+ end
29
+ if @minimum_match
30
+ params[:mm] = @minimum_match
31
+ end
32
+ if @phrase_slop
33
+ params[:ps] = @phrase_slop
34
+ end
35
+ if @query_phrase_slop
36
+ params[:qs] = @query_phrase_slop
37
+ end
38
+ if @tie
39
+ params[:tie] = @tie
40
+ end
41
+ @highlights.each do |highlight|
42
+ Sunspot::Util.deep_merge!(params, highlight.to_params)
43
+ end
44
+ params
45
+ end
46
+
47
+ #
48
+ # Assign a new boost query and return it.
49
+ #
50
+ def create_boost_query(factor)
51
+ @boost_queries << boost_query = BoostQuery.new(factor)
52
+ boost_query
53
+ end
54
+
55
+ #
56
+ # Add a fulltext field to be searched, with optional boost
57
+ #
58
+ def add_fulltext_field(field, boost = nil)
59
+ @fulltext_fields[field.indexed_name] = TextFieldBoost.new(field, boost)
60
+ end
61
+
62
+ #
63
+ # Add a phrase field for extra boost
64
+ #
65
+ def add_phrase_field(field, boost = nil)
66
+ @phrase_fields ||= []
67
+ @phrase_fields << TextFieldBoost.new(field, boost)
68
+ end
69
+
70
+ #
71
+ # Set highlighting options for the query. If fields is empty, the
72
+ # Highlighting object won't pass field names at all, which means
73
+ # the dismax's :qf parameter will be used by Solr.
74
+ #
75
+ def add_highlight(fields=[], options={})
76
+ @highlights << Highlighting.new(fields, options)
77
+ end
78
+
79
+ #
80
+ # Determine if a given field is being searched. Used by DSL to avoid
81
+ # overwriting boost parameters when injecting defaults.
82
+ #
83
+ def has_fulltext_field?(field)
84
+ @fulltext_fields.has_key?(field.indexed_name)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,9 @@
1
+ module Sunspot
2
+ module Query
3
+ class FieldFacet < AbstractFieldFacet
4
+ def to_params
5
+ super.merge(:"facet.field" => [@field.indexed_name])
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,55 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # A query component that builds parameters for requesting highlights
5
+ #
6
+ class Highlighting #:nodoc:
7
+ def initialize(fields=[], options={})
8
+ @fields = fields
9
+ @options = options
10
+ end
11
+
12
+ #
13
+ # Return Solr highlighting params
14
+ #
15
+ def to_params
16
+ params = {
17
+ :hl => 'on',
18
+ :"hl.simple.pre" => '@@@hl@@@',
19
+ :"hl.simple.post" => '@@@endhl@@@'
20
+ }
21
+ unless @fields.empty?
22
+ params[:"hl.fl"] = @fields.map { |field| field.indexed_name }
23
+ end
24
+ if max_snippets = @options[:max_snippets]
25
+ params.merge!(make_params('snippets', max_snippets))
26
+ end
27
+ if fragment_size = @options[:fragment_size]
28
+ params.merge!(make_params('fragsize', fragment_size))
29
+ end
30
+ if @options[:merge_contiguous_fragments]
31
+ params.merge!(make_params('mergeContiguous', 'true'))
32
+ end
33
+ if @options[:phrase_highlighter]
34
+ params.merge!(make_params('usePhraseHighlighter', 'true'))
35
+ if @options[:require_field_match]
36
+ params.merge!(make_params('requireFieldMatch', 'true'))
37
+ end
38
+ end
39
+ params
40
+ end
41
+
42
+ private
43
+
44
+ def make_params(name, value)
45
+ if @fields.empty?
46
+ { :"hl.#{name}" => value }
47
+ else
48
+ @fields.inject({}) do |hash, field|
49
+ hash.merge!(:"f.#{field.indexed_name}.hl.#{name}" => value)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # This query component generates parameters for LocalSolr geo-radial
5
+ # searches. The LocalSolr API is fairly rigid, so the Local component
6
+ # doesn't have any options - it just takes coordinates and a radius, and
7
+ # generates the appropriate parameters.
8
+ #
9
+ class Local #:nodoc:
10
+ def initialize(coordinates, radius)
11
+ if radius < 1
12
+ raise ArgumentError, "LocalSolr does not seem to support a radius of less than 1 mile."
13
+ end
14
+ @coordinates, @radius = Util::Coordinates.new(coordinates), radius
15
+ end
16
+
17
+ def to_params
18
+ {
19
+ :qt => 'geo',
20
+ :lat => @coordinates.lat,
21
+ :long => @coordinates.lng,
22
+ :radius => @radius
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end