nuatt_sunspot 1.1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/History.txt +198 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +239 -0
  4. data/Rakefile +11 -0
  5. data/TODO +13 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-installer +19 -0
  8. data/bin/sunspot-solr +74 -0
  9. data/installer/config/schema.yml +95 -0
  10. data/lib/light_config.rb +40 -0
  11. data/lib/sunspot.rb +529 -0
  12. data/lib/sunspot/adapters.rb +265 -0
  13. data/lib/sunspot/composite_setup.rb +202 -0
  14. data/lib/sunspot/configuration.rb +46 -0
  15. data/lib/sunspot/data_extractor.rb +50 -0
  16. data/lib/sunspot/dsl.rb +5 -0
  17. data/lib/sunspot/dsl/adjustable.rb +47 -0
  18. data/lib/sunspot/dsl/field_query.rb +266 -0
  19. data/lib/sunspot/dsl/fields.rb +113 -0
  20. data/lib/sunspot/dsl/fulltext.rb +243 -0
  21. data/lib/sunspot/dsl/function.rb +14 -0
  22. data/lib/sunspot/dsl/functional.rb +41 -0
  23. data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
  24. data/lib/sunspot/dsl/paginatable.rb +28 -0
  25. data/lib/sunspot/dsl/query_facet.rb +36 -0
  26. data/lib/sunspot/dsl/restriction.rb +25 -0
  27. data/lib/sunspot/dsl/scope.rb +229 -0
  28. data/lib/sunspot/dsl/search.rb +30 -0
  29. data/lib/sunspot/dsl/standard_query.rb +125 -0
  30. data/lib/sunspot/field.rb +192 -0
  31. data/lib/sunspot/field_factory.rb +147 -0
  32. data/lib/sunspot/indexer.rb +131 -0
  33. data/lib/sunspot/installer.rb +31 -0
  34. data/lib/sunspot/installer/library_installer.rb +45 -0
  35. data/lib/sunspot/installer/schema_builder.rb +219 -0
  36. data/lib/sunspot/installer/solrconfig_updater.rb +106 -0
  37. data/lib/sunspot/installer/task_helper.rb +18 -0
  38. data/lib/sunspot/query.rb +10 -0
  39. data/lib/sunspot/query/abstract_field_facet.rb +52 -0
  40. data/lib/sunspot/query/boost_query.rb +24 -0
  41. data/lib/sunspot/query/common_query.rb +85 -0
  42. data/lib/sunspot/query/composite_fulltext.rb +31 -0
  43. data/lib/sunspot/query/connective.rb +191 -0
  44. data/lib/sunspot/query/date_field_facet.rb +14 -0
  45. data/lib/sunspot/query/dismax.rb +127 -0
  46. data/lib/sunspot/query/field_facet.rb +41 -0
  47. data/lib/sunspot/query/filter.rb +38 -0
  48. data/lib/sunspot/query/function_query.rb +52 -0
  49. data/lib/sunspot/query/highlighting.rb +55 -0
  50. data/lib/sunspot/query/local.rb +26 -0
  51. data/lib/sunspot/query/more_like_this.rb +60 -0
  52. data/lib/sunspot/query/more_like_this_query.rb +12 -0
  53. data/lib/sunspot/query/pagination.rb +38 -0
  54. data/lib/sunspot/query/query_facet.rb +16 -0
  55. data/lib/sunspot/query/restriction.rb +262 -0
  56. data/lib/sunspot/query/scope.rb +9 -0
  57. data/lib/sunspot/query/sort.rb +95 -0
  58. data/lib/sunspot/query/sort_composite.rb +33 -0
  59. data/lib/sunspot/query/standard_query.rb +20 -0
  60. data/lib/sunspot/query/text_field_boost.rb +17 -0
  61. data/lib/sunspot/schema.rb +151 -0
  62. data/lib/sunspot/search.rb +9 -0
  63. data/lib/sunspot/search/abstract_search.rb +302 -0
  64. data/lib/sunspot/search/date_facet.rb +35 -0
  65. data/lib/sunspot/search/facet_row.rb +27 -0
  66. data/lib/sunspot/search/field_facet.rb +88 -0
  67. data/lib/sunspot/search/highlight.rb +38 -0
  68. data/lib/sunspot/search/hit.rb +136 -0
  69. data/lib/sunspot/search/more_like_this_search.rb +31 -0
  70. data/lib/sunspot/search/query_facet.rb +62 -0
  71. data/lib/sunspot/search/standard_search.rb +21 -0
  72. data/lib/sunspot/server.rb +152 -0
  73. data/lib/sunspot/session.rb +252 -0
  74. data/lib/sunspot/session_proxy.rb +71 -0
  75. data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
  76. data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
  77. data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
  78. data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
  79. data/lib/sunspot/session_proxy/sharding_session_proxy.rb +215 -0
  80. data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
  81. data/lib/sunspot/setup.rb +366 -0
  82. data/lib/sunspot/text_field_setup.rb +29 -0
  83. data/lib/sunspot/type.rb +340 -0
  84. data/lib/sunspot/util.rb +253 -0
  85. data/lib/sunspot/version.rb +3 -0
  86. data/solr/etc/jetty.xml +214 -0
  87. data/solr/etc/webdefault.xml +379 -0
  88. data/solr/lib/jetty-6.1.3.jar +0 -0
  89. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  90. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  91. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  92. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  93. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  94. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  95. data/solr/solr/conf/admin-extra.html +31 -0
  96. data/solr/solr/conf/elevate.xml +36 -0
  97. data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
  98. data/solr/solr/conf/protwords.txt +21 -0
  99. data/solr/solr/conf/schema.xml +240 -0
  100. data/solr/solr/conf/scripts.conf +24 -0
  101. data/solr/solr/conf/solrconfig.xml +938 -0
  102. data/solr/solr/conf/spellings.txt +2 -0
  103. data/solr/solr/conf/stopwords.es.txt +345 -0
  104. data/solr/solr/conf/stopwords.txt +58 -0
  105. data/solr/solr/conf/synonyms.txt +31 -0
  106. data/solr/solr/lib/lucene-spatial-2.9.1.jar +0 -0
  107. data/solr/solr/lib/solr-spatial-light-0.0.6.jar +0 -0
  108. data/solr/start.jar +0 -0
  109. data/solr/webapps/solr.war +0 -0
  110. data/spec/api/adapters_spec.rb +33 -0
  111. data/spec/api/binding_spec.rb +38 -0
  112. data/spec/api/indexer/attributes_spec.rb +149 -0
  113. data/spec/api/indexer/batch_spec.rb +46 -0
  114. data/spec/api/indexer/dynamic_fields_spec.rb +42 -0
  115. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  116. data/spec/api/indexer/fulltext_spec.rb +43 -0
  117. data/spec/api/indexer/removal_spec.rb +53 -0
  118. data/spec/api/indexer/spec_helper.rb +1 -0
  119. data/spec/api/indexer_spec.rb +14 -0
  120. data/spec/api/query/advanced_manipulation_examples.rb +35 -0
  121. data/spec/api/query/connectives_examples.rb +176 -0
  122. data/spec/api/query/dsl_spec.rb +18 -0
  123. data/spec/api/query/dynamic_fields_examples.rb +165 -0
  124. data/spec/api/query/faceting_examples.rb +399 -0
  125. data/spec/api/query/fulltext_examples.rb +315 -0
  126. data/spec/api/query/function_spec.rb +70 -0
  127. data/spec/api/query/highlighting_examples.rb +225 -0
  128. data/spec/api/query/local_examples.rb +38 -0
  129. data/spec/api/query/more_like_this_spec.rb +140 -0
  130. data/spec/api/query/ordering_pagination_examples.rb +97 -0
  131. data/spec/api/query/scope_examples.rb +256 -0
  132. data/spec/api/query/spec_helper.rb +1 -0
  133. data/spec/api/query/standard_spec.rb +28 -0
  134. data/spec/api/query/text_field_scoping_examples.rb +30 -0
  135. data/spec/api/query/types_spec.rb +20 -0
  136. data/spec/api/search/dynamic_fields_spec.rb +33 -0
  137. data/spec/api/search/faceting_spec.rb +356 -0
  138. data/spec/api/search/highlighting_spec.rb +69 -0
  139. data/spec/api/search/hits_spec.rb +138 -0
  140. data/spec/api/search/results_spec.rb +79 -0
  141. data/spec/api/search/search_spec.rb +23 -0
  142. data/spec/api/search/spec_helper.rb +1 -0
  143. data/spec/api/server_spec.rb +91 -0
  144. data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
  145. data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
  146. data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
  147. data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
  148. data/spec/api/session_proxy/spec_helper.rb +9 -0
  149. data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +50 -0
  150. data/spec/api/session_spec.rb +198 -0
  151. data/spec/api/spec_helper.rb +3 -0
  152. data/spec/api/sunspot_spec.rb +18 -0
  153. data/spec/ext.rb +11 -0
  154. data/spec/helpers/indexer_helper.rb +29 -0
  155. data/spec/helpers/query_helper.rb +38 -0
  156. data/spec/helpers/search_helper.rb +80 -0
  157. data/spec/integration/dynamic_fields_spec.rb +55 -0
  158. data/spec/integration/faceting_spec.rb +238 -0
  159. data/spec/integration/highlighting_spec.rb +22 -0
  160. data/spec/integration/indexing_spec.rb +33 -0
  161. data/spec/integration/keyword_search_spec.rb +317 -0
  162. data/spec/integration/local_search_spec.rb +91 -0
  163. data/spec/integration/more_like_this_spec.rb +43 -0
  164. data/spec/integration/scoped_search_spec.rb +324 -0
  165. data/spec/integration/spec_helper.rb +7 -0
  166. data/spec/integration/stored_fields_spec.rb +10 -0
  167. data/spec/integration/test_pagination.rb +32 -0
  168. data/spec/mocks/adapters.rb +32 -0
  169. data/spec/mocks/blog.rb +3 -0
  170. data/spec/mocks/comment.rb +21 -0
  171. data/spec/mocks/connection.rb +122 -0
  172. data/spec/mocks/mock_adapter.rb +30 -0
  173. data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
  174. data/spec/mocks/mock_record.rb +52 -0
  175. data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
  176. data/spec/mocks/photo.rb +12 -0
  177. data/spec/mocks/post.rb +76 -0
  178. data/spec/mocks/super_class.rb +2 -0
  179. data/spec/mocks/user.rb +8 -0
  180. data/spec/spec_helper.rb +52 -0
  181. data/tasks/gemspec.rake +33 -0
  182. data/tasks/rcov.rake +28 -0
  183. data/tasks/rdoc.rake +27 -0
  184. data/tasks/schema.rake +19 -0
  185. data/tasks/spec.rake +24 -0
  186. data/tasks/todo.rake +4 -0
  187. metadata +355 -0
@@ -0,0 +1,106 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'nokogiri'
4
+
5
+ module Sunspot
6
+ class Installer
7
+ class SolrconfigUpdater
8
+ include TaskHelper
9
+
10
+ CONFIG_FILES = %w(solrconfig.xml elevate.xml spellings.txt stopwords.txt synonyms.txt stopwords.es.txt)
11
+
12
+ class <<self
13
+ def execute(solrconfig_path, options = {})
14
+ new(solrconfig_path, options).execute
15
+ end
16
+ end
17
+
18
+ def initialize(solrconfig_path, options)
19
+ @force = !!options[:force]
20
+ unless @force || File.exist?(solrconfig_path)
21
+ abort("#{File.expand_path(@solrconfig_path)} doesn't exist." +
22
+ " Are you sure you're pointing to a SOLR_HOME?")
23
+ end
24
+ @solrconfig_path = solrconfig_path
25
+ @verbose = !!options[:verbose]
26
+ end
27
+
28
+ def execute
29
+ if @force
30
+ config_dir = File.dirname(@solrconfig_path)
31
+ FileUtils.mkdir_p(config_dir)
32
+ CONFIG_FILES.each do |file|
33
+ source_path =
34
+ File.join(File.dirname(__FILE__), '..', '..', '..', 'solr', 'solr', 'conf', file)
35
+ FileUtils.cp(source_path, config_dir)
36
+ say("Copied default #{file} to #{config_dir}")
37
+ end
38
+ else
39
+ @document = File.open(@solrconfig_path) do |f|
40
+ Nokogiri::XML(
41
+ f, nil, nil,
42
+ Nokogiri::XML::ParseOptions::DEFAULT_XML |
43
+ Nokogiri::XML::ParseOptions::NOBLANKS
44
+ )
45
+ end
46
+ @root = @document.root
47
+ maybe_create_spatial_component
48
+ maybe_add_spatial_component_to_standard_handler
49
+ maybe_add_more_like_this_handler
50
+ original_path = "#{@solrconfig_path}.orig"
51
+ FileUtils.cp(@solrconfig_path, original_path)
52
+ say("Saved backup of original to #{original_path}")
53
+ File.open(@solrconfig_path, 'w') do |file|
54
+ @document.write_to(
55
+ file,
56
+ :indent => 2
57
+ )
58
+ end
59
+ say("Wrote solrconfig to #{@solrconfig_path}")
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def maybe_create_spatial_component
66
+ if @root.xpath('searchComponent[@name="spatial"]').any?
67
+ say('Spatial search component already defined')
68
+ else
69
+ say('Defining spatial search component')
70
+ search_component_node =
71
+ Nokogiri::XML::Node.new('searchComponent', @document)
72
+ search_component_node['name'] = 'spatial'
73
+ search_component_node['class'] =
74
+ 'me.outofti.solrspatiallight.SpatialQueryComponent'
75
+ @root << search_component_node
76
+ end
77
+ end
78
+
79
+ def maybe_add_spatial_component_to_standard_handler
80
+ standard_handler_node =
81
+ @root.xpath('requestHandler[@name="standard"]').first
82
+ last_components_node =
83
+ standard_handler_node.xpath('arr[@name="last-components"]').first ||
84
+ add_element(standard_handler_node, 'arr', 'name' => 'last-components')
85
+ if last_components_node.xpath('str[normalize-space()="spatial"]').any?
86
+ say('Spatial search component already in standard search handler')
87
+ else
88
+ say('Adding spatial search component into standard search handler')
89
+ add_element(last_components_node, 'str').content = 'spatial'
90
+ end
91
+ end
92
+
93
+ def maybe_add_more_like_this_handler
94
+ unless @root.xpath('requestHandler[@name="/mlt"]').first
95
+ mlt_node = add_element(
96
+ @root, 'requestHandler',
97
+ :name => '/mlt', :class => 'solr.MoreLikeThisHandler'
98
+ )
99
+ defaults_node = add_element(mlt_node, 'lst', :name => 'defaults')
100
+ add_element(defaults_node, 'str', :name => 'mlt.mintf').content = '1'
101
+ add_element(defaults_node, 'str', :name => 'mlt.mindf').content = '2'
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,18 @@
1
+ module Sunspot
2
+ class Installer
3
+ module TaskHelper
4
+ def say(message)
5
+ if @verbose
6
+ STDOUT.puts(message)
7
+ end
8
+ end
9
+
10
+ def add_element(node, name, attributes = {})
11
+ new_node = Nokogiri::XML::Node.new(name, @document)
12
+ attributes.each_pair { |name, value| new_node[name.to_s] = value }
13
+ node << new_node
14
+ new_node
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ %w(filter abstract_field_facet connective boost_query date_field_facet dismax
2
+ field_facet highlighting local pagination restriction common_query
3
+ standard_query more_like_this more_like_this_query query_facet scope sort
4
+ sort_composite text_field_boost function_query composite_fulltext).each do |file|
5
+ require(File.join(File.dirname(__FILE__), 'query', file))
6
+ end
7
+ module Sunspot
8
+ module Query #:nodoc:all
9
+ end
10
+ end
@@ -0,0 +1,52 @@
1
+ module Sunspot
2
+ module Query
3
+ class AbstractFieldFacet
4
+ include RSolr::Char
5
+
6
+ def initialize(field, options)
7
+ @field, @options = field, options
8
+ end
9
+
10
+ def to_params
11
+ params = {
12
+ :facet => 'true',
13
+ }
14
+ case @options[:sort]
15
+ when :count
16
+ params[qualified_param('sort')] = 'true'
17
+ when :index
18
+ params[qualified_param('sort')] = 'false'
19
+ when nil
20
+ else
21
+ raise(
22
+ ArgumentError,
23
+ "#{@options[:sort].inspect} is not an allowed value for :sort. Allowed options are :count and :index"
24
+ )
25
+ end
26
+ if @options[:limit]
27
+ params[qualified_param('limit')] = @options[:limit].to_i
28
+ end
29
+ if @options[:prefix]
30
+ params[qualified_param('prefix')] = escape(@options[:prefix].to_s)
31
+ end
32
+ params[qualified_param('mincount')] =
33
+ case
34
+ when @options[:minimum_count] then @options[:minimum_count].to_i
35
+ when @options[:zeros] then 0
36
+ else 1
37
+ end
38
+ params
39
+ end
40
+
41
+ private
42
+
43
+ def qualified_param(param)
44
+ :"f.#{key}.facet.#{param}"
45
+ end
46
+
47
+ def key
48
+ @key ||= @options[:name] || @field.indexed_name
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,24 @@
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
+ if @boost.is_a?(FunctionQuery)
17
+ "#{@boost}"
18
+ else
19
+ "#{super}^#{@boost}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,85 @@
1
+ module Sunspot
2
+ module Query #:nodoc:
3
+ class CommonQuery
4
+ def initialize(types)
5
+ @scope = Scope.new
6
+ @sort = SortComposite.new
7
+ @components = [@scope, @sort]
8
+ if types.length == 1
9
+ @scope.add_restriction(TypeField.instance, Restriction::EqualTo, types.first)
10
+ else
11
+ @scope.add_restriction(TypeField.instance, Restriction::AnyOf, types)
12
+ end
13
+ end
14
+
15
+ def solr_parameter_adjustment=(block)
16
+ @parameter_adjustment = block
17
+ end
18
+
19
+ def add_sort(sort)
20
+ @sort << sort
21
+ end
22
+
23
+ def add_field_facet(facet)
24
+ @components << facet
25
+ facet
26
+ end
27
+
28
+ def add_query_facet(facet)
29
+ @components << facet
30
+ facet
31
+ end
32
+
33
+ def add_function(function)
34
+ @components << function
35
+ function
36
+ end
37
+
38
+ def paginate(page, per_page)
39
+ if @pagination
40
+ @pagination.page = page
41
+ @pagination.per_page = per_page
42
+ else
43
+ @components << @pagination = Pagination.new(page, per_page)
44
+ end
45
+ end
46
+
47
+ def to_params
48
+ params = {}
49
+ @components.each do |component|
50
+ Sunspot::Util.deep_merge!(params, component.to_params)
51
+ end
52
+ @parameter_adjustment.call(params) if @parameter_adjustment
53
+ params[:q] ||= '*:*'
54
+ params
55
+ end
56
+
57
+ def [](key)
58
+ to_params[key.to_sym]
59
+ end
60
+
61
+ def page
62
+ @pagination.page if @pagination
63
+ end
64
+
65
+ def per_page
66
+ @pagination.per_page if @pagination
67
+ end
68
+
69
+
70
+ private
71
+
72
+ #
73
+ # If we have a single fulltext query, merge is normally. If there are
74
+ # multiple nested queries, serialize them as `_query_` subqueries.
75
+ #
76
+ def merge_fulltext(params)
77
+ return nil if @fulltexts.empty?
78
+ return Sunspot::Util.deep_merge!(params, @fulltexts.first.to_params) if @fulltexts.length == 1
79
+ subqueries = @fulltexts.map {|fulltext| fulltext.to_subquery }.join(' ')
80
+ Sunspot::Util.deep_merge!(params, {:q => subqueries})
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,31 @@
1
+ module Sunspot
2
+ module Query
3
+ class CompositeFulltext
4
+ def initialize
5
+ @components = []
6
+ end
7
+
8
+ def add(keywords)
9
+ @components << dismax = Dismax.new(keywords)
10
+ dismax
11
+ end
12
+
13
+ def to_params
14
+ case @components.length
15
+ when 0
16
+ {}
17
+ when 1
18
+ @components.first.to_params
19
+ else
20
+ to_subqueries
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def to_subqueries
27
+ { :q => @components.map { |dismax| dismax.to_subquery }.join(' ') }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,191 @@
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
+ include Filter
9
+
10
+ def initialize(negated = false) #:nodoc:
11
+ @negated = negated
12
+ @components = []
13
+ end
14
+
15
+ #
16
+ # Add a restriction to the connective.
17
+ #
18
+ def add_restriction(field, restriction_type, value, negated = false)
19
+ add_component(restriction_type.new(field, value, negated))
20
+ end
21
+
22
+ #
23
+ # Add a shorthand restriction; the restriction type is determined by
24
+ # the value.
25
+ #
26
+ def add_shorthand_restriction(field, value, negated = false)
27
+ restriction_type =
28
+ case value
29
+ when Array then Restriction::AnyOf
30
+ when Range then Restriction::Between
31
+ else Restriction::EqualTo
32
+ end
33
+ add_restriction(field, restriction_type, value, negated)
34
+ end
35
+
36
+ #
37
+ # Add a negated restriction. The added restriction will match all
38
+ # documents who do not match the terms of the restriction.
39
+ #
40
+ def add_negated_restriction(field, restriction_type, value)
41
+ add_restriction(field, restriction_type, value, true)
42
+ end
43
+
44
+ #
45
+ # Add a negated shorthand restriction (see add_shorthand_restriction)
46
+ #
47
+ def add_negated_shorthand_restriction(field, value)
48
+ add_shorthand_restriction(field, value, true)
49
+ end
50
+
51
+ #
52
+ # Add a new conjunction and return it.
53
+ #
54
+ def add_conjunction
55
+ add_component(Conjunction.new)
56
+ end
57
+
58
+ #
59
+ # Add a new disjunction and return it.
60
+ #
61
+ def add_disjunction
62
+ add_component(Disjunction.new)
63
+ end
64
+
65
+ #
66
+ # Add an arbitrary component to the conjunction, and return it.
67
+ # The component must respond to #to_boolean_phrase
68
+ #
69
+ def add_component(component)
70
+ @components << component
71
+ component
72
+ end
73
+
74
+ #
75
+ # Express the connective as a Lucene boolean phrase.
76
+ #
77
+ def to_boolean_phrase #:nodoc:
78
+ unless @components.empty?
79
+ phrase =
80
+ if @components.length == 1
81
+ @components.first.to_boolean_phrase
82
+ else
83
+ component_phrases = @components.map do |component|
84
+ component.to_boolean_phrase
85
+ end
86
+ "(#{component_phrases.join(" #{connector} ")})"
87
+ end
88
+ if negated?
89
+ "-#{phrase}"
90
+ else
91
+ phrase
92
+ end
93
+ end
94
+ end
95
+
96
+ #
97
+ # Connectives can be negated during the process of denormalization that
98
+ # is performed when a disjunction contains a negated component. This
99
+ # method conforms to the duck type for all boolean query components.
100
+ #
101
+ def negated?
102
+ @negated
103
+ end
104
+
105
+ #
106
+ # Returns a new connective that's a negated version of this one.
107
+ #
108
+ def negate
109
+ negated = self.class.new(!negated?)
110
+ @components.each do |component|
111
+ negated.add_component(component)
112
+ end
113
+ negated
114
+ end
115
+ end
116
+
117
+ #
118
+ # Disjunctions combine their components with an OR operator.
119
+ #
120
+ class Disjunction < Abstract
121
+ class <<self
122
+ def inverse
123
+ Conjunction
124
+ end
125
+ end
126
+
127
+ #
128
+ # Express this disjunction as a Lucene boolean phrase
129
+ #
130
+ def to_boolean_phrase
131
+ if @components.any? { |component| component.negated? }
132
+ denormalize.to_boolean_phrase
133
+ else
134
+ super
135
+ end
136
+ end
137
+
138
+ #
139
+ # No-op - this is already a disjunction
140
+ #
141
+ def add_disjunction
142
+ self
143
+ end
144
+
145
+ private
146
+
147
+ def connector
148
+ 'OR'
149
+ end
150
+
151
+ #
152
+ # If a disjunction contains negated components, it must be
153
+ # "denormalized", because the Lucene parser interprets any negated
154
+ # boolean phrase using AND semantics (this isn't a bug, it's just a
155
+ # subtlety of how Lucene parses queries). So, per DeMorgan's law we
156
+ # create a negated conjunction and add to it all of our components,
157
+ # negated themselves, which creates a query whose Lucene semantics are
158
+ # in line with our intentions.
159
+ #
160
+ def denormalize
161
+ denormalized = self.class.inverse.new(!negated?)
162
+ @components.each do |component|
163
+ denormalized.add_component(component.negate)
164
+ end
165
+ denormalized
166
+ end
167
+ end
168
+
169
+ #
170
+ # Conjunctions combine their components with an AND operator.
171
+ #
172
+ class Conjunction < Abstract
173
+ class <<self
174
+ def inverse
175
+ Disjunction
176
+ end
177
+ end
178
+
179
+ def add_conjunction
180
+ self
181
+ end
182
+
183
+ private
184
+
185
+ def connector
186
+ 'AND'
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end