nxa-sunspot 0.10.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +153 -0
- data/LICENSE +18 -0
- data/README.rdoc +191 -0
- data/Rakefile +9 -0
- data/TODO +14 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +40 -0
- data/bin/sunspot-solr +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot.rb +488 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +188 -0
- data/lib/sunspot/configuration.rb +56 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl.rb +4 -0
- data/lib/sunspot/dsl/field_query.rb +150 -0
- data/lib/sunspot/dsl/fields.rb +100 -0
- data/lib/sunspot/dsl/fulltext.rb +228 -0
- data/lib/sunspot/dsl/query.rb +162 -0
- data/lib/sunspot/dsl/query_facet.rb +36 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +225 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/field.rb +181 -0
- data/lib/sunspot/field_factory.rb +144 -0
- data/lib/sunspot/indexer.rb +133 -0
- data/lib/sunspot/query.rb +9 -0
- data/lib/sunspot/query/abstract_field_facet.rb +43 -0
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +189 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +88 -0
- data/lib/sunspot/query/field_facet.rb +9 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/local.rb +27 -0
- data/lib/sunspot/query/pagination.rb +38 -0
- data/lib/sunspot/query/query.rb +86 -0
- data/lib/sunspot/query/query_facet.rb +16 -0
- data/lib/sunspot/query/restriction.rb +254 -0
- data/lib/sunspot/query/scope.rb +9 -0
- data/lib/sunspot/query/sort.rb +105 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/text_field_boost.rb +15 -0
- data/lib/sunspot/schema.rb +147 -0
- data/lib/sunspot/search.rb +216 -0
- data/lib/sunspot/search/date_facet.rb +35 -0
- data/lib/sunspot/search/facet_row.rb +27 -0
- data/lib/sunspot/search/field_facet.rb +44 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +117 -0
- data/lib/sunspot/search/query_facet.rb +62 -0
- data/lib/sunspot/session.rb +236 -0
- data/lib/sunspot/setup.rb +323 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +204 -0
- data/lib/sunspot/util.rb +210 -0
- data/solr/etc/jetty.xml +212 -0
- data/solr/etc/webdefault.xml +379 -0
- data/solr/lib/jetty-6.1.3.jar +0 -0
- data/solr/lib/jetty-util-6.1.3.jar +0 -0
- data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
- data/solr/solr/conf/elevate.xml +36 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +64 -0
- data/solr/solr/conf/solrconfig.xml +725 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
- data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
- data/solr/solr/lib/jsr108-0.01.jar +0 -0
- data/solr/solr/lib/locallucene.jar +0 -0
- data/solr/solr/lib/localsolr.jar +0 -0
- data/solr/start.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/spec/api/adapters_spec.rb +33 -0
- data/spec/api/indexer/attributes_spec.rb +110 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
- data/spec/api/indexer/fixed_fields_spec.rb +57 -0
- data/spec/api/indexer/fulltext_spec.rb +43 -0
- data/spec/api/indexer/removal_spec.rb +46 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +4 -0
- data/spec/api/query/adjust_params_spec.rb +37 -0
- data/spec/api/query/connectives_spec.rb +176 -0
- data/spec/api/query/dsl_spec.rb +12 -0
- data/spec/api/query/dynamic_fields_spec.rb +149 -0
- data/spec/api/query/faceting_spec.rb +296 -0
- data/spec/api/query/fulltext_spec.rb +281 -0
- data/spec/api/query/highlighting_spec.rb +225 -0
- data/spec/api/query/local_spec.rb +62 -0
- data/spec/api/query/ordering_pagination_spec.rb +95 -0
- data/spec/api/query/scope_spec.rb +266 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/text_field_scoping_spec.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +27 -0
- data/spec/api/search/faceting_spec.rb +242 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +67 -0
- data/spec/api/search/results_spec.rb +52 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/session_spec.rb +198 -0
- data/spec/api/spec_helper.rb +1 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +13 -0
- data/spec/helpers/search_helper.rb +78 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +188 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/indexing_spec.rb +7 -0
- data/spec/integration/keyword_search_spec.rb +245 -0
- data/spec/integration/local_search_spec.rb +56 -0
- data/spec/integration/scoped_search_spec.rb +303 -0
- data/spec/integration/spec_helper.rb +7 -0
- data/spec/integration/stored_fields_spec.rb +10 -0
- data/spec/integration/test_pagination.rb +32 -0
- data/spec/mocks/adapters.rb +32 -0
- data/spec/mocks/blog.rb +3 -0
- data/spec/mocks/comment.rb +19 -0
- data/spec/mocks/connection.rb +106 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_record.rb +48 -0
- data/spec/mocks/photo.rb +11 -0
- data/spec/mocks/post.rb +75 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +68 -0
- data/tasks/gemspec.rake +42 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +22 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- data/templates/schema.xml.erb +36 -0
- metadata +319 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
module Sunspot
|
2
|
+
class Search
|
3
|
+
class FacetRow
|
4
|
+
attr_reader :value, :count
|
5
|
+
attr_writer :instance #:nodoc:
|
6
|
+
|
7
|
+
def initialize(value, count, facet) #:nodoc:
|
8
|
+
@value, @count, @facet = value, count, facet
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Return the instance referenced by this facet row. Only valid for field
|
13
|
+
# facets whose fields are defined with the :references key.
|
14
|
+
#
|
15
|
+
def instance
|
16
|
+
if !defined?(@instance)
|
17
|
+
@facet.populate_instances
|
18
|
+
end
|
19
|
+
@instance
|
20
|
+
end
|
21
|
+
|
22
|
+
def inspect
|
23
|
+
"<Sunspot::Search::FacetRow:#{value.inspect} (#{count})>"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Sunspot
|
2
|
+
class Search
|
3
|
+
class FieldFacet < QueryFacet
|
4
|
+
alias_method :field_name, :name
|
5
|
+
|
6
|
+
def initialize(field, search, options) #:nodoc:
|
7
|
+
super(field.name, search, options)
|
8
|
+
@field = field
|
9
|
+
end
|
10
|
+
|
11
|
+
def rows
|
12
|
+
@rows ||=
|
13
|
+
begin
|
14
|
+
rows = super
|
15
|
+
has_query_facets = !rows.empty?
|
16
|
+
if @search.facet_response['facet_fields']
|
17
|
+
if data = @search.facet_response['facet_fields'][@field.indexed_name]
|
18
|
+
data.each_slice(2) do |value, count|
|
19
|
+
rows << FacetRow.new(@field.cast(value), count, self)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
sort_rows!(rows) if has_query_facets
|
24
|
+
rows
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def populate_instances #:nodoc:
|
29
|
+
if reference = @field.reference
|
30
|
+
values_hash = rows.inject({}) do |hash, row|
|
31
|
+
hash[row.value] = row
|
32
|
+
hash
|
33
|
+
end
|
34
|
+
instances = Adapters::DataAccessor.create(Sunspot::Util.full_const_get(reference)).load_all(
|
35
|
+
values_hash.keys
|
36
|
+
)
|
37
|
+
instances.each do |instance|
|
38
|
+
values_hash[Adapters::InstanceAdapter.adapt(instance).id].instance = instance
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Sunspot
|
2
|
+
class Search
|
3
|
+
#
|
4
|
+
# A Highlight represents a single highlighted fragment of text from a
|
5
|
+
# document. Depending on the highlighting parameters used for search, there
|
6
|
+
# may be more than one Highlight object for a given field in a given result.
|
7
|
+
#
|
8
|
+
class Highlight
|
9
|
+
HIGHLIGHT_MATCHER = /@@@hl@@@(.*?)@@@endhl@@@/ #:nodoc:
|
10
|
+
|
11
|
+
#
|
12
|
+
# The name of the field in which the highlight appeared.
|
13
|
+
#
|
14
|
+
attr_reader :field_name
|
15
|
+
|
16
|
+
def initialize(field_name, highlight) #:nodoc:
|
17
|
+
@field_name = field_name.to_sym
|
18
|
+
@highlight = highlight.to_s.strip
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Returns the highlighted text with formatting according to the template given in &block.
|
23
|
+
# When no block is given, <em> and </em> are used to surround the highlight.
|
24
|
+
#
|
25
|
+
# ==== Example
|
26
|
+
#
|
27
|
+
# search.highlights(:body).first.format { |word| "<strong>#{word}</strong>" }
|
28
|
+
#
|
29
|
+
def format(&block)
|
30
|
+
block ||= proc { |word| "<em>#{word}</em>" }
|
31
|
+
@highlight.gsub(HIGHLIGHT_MATCHER) do
|
32
|
+
block.call(Regexp.last_match[1])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
alias_method :formatted, :format
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module Sunspot
|
2
|
+
class Search
|
3
|
+
#
|
4
|
+
# Hit objects represent the raw information returned by Solr for a single
|
5
|
+
# document. As well as the primary key and class name, hit objects give
|
6
|
+
# access to stored field values, keyword relevance score, and geographical
|
7
|
+
# distance (for geographical search).
|
8
|
+
#
|
9
|
+
class Hit
|
10
|
+
SPECIAL_KEYS = Set.new(%w(id type score)) #:nodoc:
|
11
|
+
|
12
|
+
#
|
13
|
+
# Primary key of object associated with this hit, as string.
|
14
|
+
#
|
15
|
+
attr_reader :primary_key
|
16
|
+
#
|
17
|
+
# Class name of object associated with this hit, as string.
|
18
|
+
#
|
19
|
+
attr_reader :class_name
|
20
|
+
#
|
21
|
+
# Keyword relevance score associated with this result. Nil if this hit
|
22
|
+
# is not from a keyword search.
|
23
|
+
#
|
24
|
+
attr_reader :score
|
25
|
+
#
|
26
|
+
# For geographical searches, this is the distance between the search
|
27
|
+
# centerpoint and the document's location. Otherwise, it's nil.
|
28
|
+
#
|
29
|
+
attr_reader :distance
|
30
|
+
|
31
|
+
attr_writer :instance #:nodoc:
|
32
|
+
|
33
|
+
def initialize(raw_hit, highlights, search) #:nodoc:
|
34
|
+
@class_name, @primary_key = *raw_hit['id'].match(/([^ ]+) (.+)/)[1..2]
|
35
|
+
@score = raw_hit['score']
|
36
|
+
@distance = raw_hit['geo_distance'].to_f if raw_hit['geo_distance']
|
37
|
+
@search = search
|
38
|
+
@stored_values = raw_hit
|
39
|
+
@stored_cache = {}
|
40
|
+
@highlights = highlights
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Returns all highlights for this hit when called without parameters.
|
45
|
+
# When a field_name is provided, returns only the highlight for this field.
|
46
|
+
#
|
47
|
+
def highlights(field_name = nil)
|
48
|
+
if field_name.nil?
|
49
|
+
highlights_cache.values.flatten
|
50
|
+
else
|
51
|
+
highlights_cache[field_name.to_sym]
|
52
|
+
end || []
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Retrieve stored field value. For any attribute field configured with
|
57
|
+
# :stored => true, the Hit object will contain the stored value for
|
58
|
+
# that field. The value of this field will be typecast according to the
|
59
|
+
# type of the field.
|
60
|
+
#
|
61
|
+
# ==== Parameters
|
62
|
+
#
|
63
|
+
# field_name<Symbol>::
|
64
|
+
# The name of the field for which to retrieve the stored value.
|
65
|
+
#
|
66
|
+
def stored(field_name)
|
67
|
+
@stored_cache[field_name.to_sym] ||= stored_value(field_name)
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# Retrieve the instance associated with this hit. This is lazy-loaded, but
|
72
|
+
# the first time it is called on any hit, all the hits for the search will
|
73
|
+
# load their instances using the adapter's #load_all method.
|
74
|
+
#
|
75
|
+
def instance
|
76
|
+
if @instance.nil?
|
77
|
+
@search.populate_hits!
|
78
|
+
end
|
79
|
+
@instance
|
80
|
+
end
|
81
|
+
|
82
|
+
def inspect #:nodoc:
|
83
|
+
"#<Sunspot::Search::Hit:#{@class_name} #{@primary_key}>"
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def setup
|
89
|
+
@setup ||= Sunspot::Setup.for(@class_name)
|
90
|
+
end
|
91
|
+
|
92
|
+
def highlights_cache
|
93
|
+
@highlights_cache ||=
|
94
|
+
begin
|
95
|
+
cache = {}
|
96
|
+
if @highlights
|
97
|
+
@highlights.each_pair do |indexed_field_name, highlight_strings|
|
98
|
+
field_name = indexed_field_name.sub(/_[a-z]+$/, '').to_sym
|
99
|
+
cache[field_name] = highlight_strings.map do |highlight_string|
|
100
|
+
Highlight.new(field_name, highlight_string)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
cache
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def stored_value(field_name)
|
109
|
+
setup.stored_fields(field_name).each do |field|
|
110
|
+
if value = @stored_values[field.indexed_name]
|
111
|
+
return field.cast(value)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Sunspot
|
2
|
+
class Search
|
3
|
+
class QueryFacet
|
4
|
+
RequestedFacet = Struct.new(:label, :boolean_phrase) #:nodoc:
|
5
|
+
|
6
|
+
attr_reader :name
|
7
|
+
|
8
|
+
def initialize(name, search, options) #:nodoc:
|
9
|
+
@name, @search, @options = name, search, options
|
10
|
+
@requested_facets = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def rows
|
14
|
+
@rows ||=
|
15
|
+
begin
|
16
|
+
data = @search.facet_response['facet_queries']
|
17
|
+
rows = []
|
18
|
+
minimum_count =
|
19
|
+
case
|
20
|
+
when @options[:minimum_count] then @options[:minimum_count]
|
21
|
+
when @options[:zeros] then 0
|
22
|
+
else 1
|
23
|
+
end
|
24
|
+
@requested_facets.each do |requested_facet|
|
25
|
+
count = data[requested_facet.boolean_phrase] || 0
|
26
|
+
if count >= minimum_count
|
27
|
+
rows << FacetRow.new(requested_facet.label, count, self)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
sort_rows!(rows)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_row(label, boolean_phrase) #:nodoc:
|
35
|
+
@requested_facets << RequestedFacet.new(label, boolean_phrase)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def sort_rows!(rows)
|
41
|
+
case @options[:sort] || (:count if @options[:limit])
|
42
|
+
when :count
|
43
|
+
rows.sort! { |lrow, rrow| rrow.count <=> lrow.count }
|
44
|
+
when :index
|
45
|
+
rows.sort! do |lrow, rrow|
|
46
|
+
if lrow.respond_to?(:<=>)
|
47
|
+
lrow.value <=> rrow.value
|
48
|
+
elsif lrow.respond_to?(:first) && rrow.respond_to?(:first) && lrow.first.respond_to?(:<=>)
|
49
|
+
lrow.first.value <=> rrow.first.value
|
50
|
+
else
|
51
|
+
lrow.value.to_s <=> rrow.value.to_s
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
if @options[:limit]
|
56
|
+
rows.slice!(@options[:limit], rows.length)
|
57
|
+
end
|
58
|
+
rows
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,236 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# A Sunspot session encapsulates a connection to Solr and a set of
|
4
|
+
# configuration choices. Though users of Sunspot may manually instantiate
|
5
|
+
# Session objects, in the general case it's easier to use the singleton
|
6
|
+
# stored in the Sunspot module. Since the Sunspot module provides all of
|
7
|
+
# the instance methods of Session as class methods, they are not documented
|
8
|
+
# again here.
|
9
|
+
#
|
10
|
+
class Session
|
11
|
+
class <<self
|
12
|
+
attr_writer :connection_class #:nodoc:
|
13
|
+
|
14
|
+
#
|
15
|
+
# For testing purposes
|
16
|
+
#
|
17
|
+
def connection_class #:nodoc:
|
18
|
+
@connection_class ||= RSolr
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Sunspot::Configuration object for this session
|
24
|
+
#
|
25
|
+
attr_reader :config
|
26
|
+
|
27
|
+
#
|
28
|
+
# Sessions are initialized with a Sunspot configuration and a Solr
|
29
|
+
# connection. Usually you will want to stick with the default arguments
|
30
|
+
# when instantiating your own sessions.
|
31
|
+
#
|
32
|
+
def initialize(config = Configuration.build, connection = nil, master_connection = nil)
|
33
|
+
@config = config
|
34
|
+
yield(@config) if block_given?
|
35
|
+
@connection = connection
|
36
|
+
@master_connection = master_connection
|
37
|
+
@deletes = @adds = 0
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# See Sunspot.new_search
|
42
|
+
#
|
43
|
+
def new_search(*types)
|
44
|
+
types.flatten!
|
45
|
+
if types.empty?
|
46
|
+
raise(ArgumentError, "You must specify at least one type to search")
|
47
|
+
end
|
48
|
+
setup =
|
49
|
+
if types.length == 1
|
50
|
+
Setup.for(types.first)
|
51
|
+
else
|
52
|
+
CompositeSetup.for(types)
|
53
|
+
end
|
54
|
+
Search.new(connection, setup, Query::Query.new(types), @config)
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# See Sunspot.search
|
59
|
+
#
|
60
|
+
def search(*types, &block)
|
61
|
+
search = new_search(*types)
|
62
|
+
search.build(&block) if block
|
63
|
+
search.execute!
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# See Sunspot.index
|
68
|
+
#
|
69
|
+
def index(*objects)
|
70
|
+
objects.flatten!
|
71
|
+
@adds += objects.length
|
72
|
+
indexer.add(objects)
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# See Sunspot.index!
|
77
|
+
#
|
78
|
+
def index!(*objects)
|
79
|
+
index(*objects)
|
80
|
+
commit
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# See Sunspot.commit
|
85
|
+
#
|
86
|
+
def commit
|
87
|
+
@adds = @deletes = 0
|
88
|
+
master_connection.commit
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
# See Sunspot.remove
|
93
|
+
#
|
94
|
+
def remove(*objects)
|
95
|
+
objects.flatten!
|
96
|
+
@deletes += objects.length
|
97
|
+
objects.each do |object|
|
98
|
+
indexer.remove(object)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# See Sunspot.remove!
|
104
|
+
#
|
105
|
+
def remove!(*objects)
|
106
|
+
remove(*objects)
|
107
|
+
commit
|
108
|
+
end
|
109
|
+
|
110
|
+
#
|
111
|
+
# See Sunspot.remove_by_id
|
112
|
+
#
|
113
|
+
def remove_by_id(clazz, id)
|
114
|
+
class_name =
|
115
|
+
if clazz.is_a?(Class)
|
116
|
+
clazz.name
|
117
|
+
else
|
118
|
+
clazz.to_s
|
119
|
+
end
|
120
|
+
indexer.remove_by_id(class_name, id)
|
121
|
+
end
|
122
|
+
|
123
|
+
#
|
124
|
+
# See Sunspot.remove_by_id!
|
125
|
+
#
|
126
|
+
def remove_by_id!(clazz, id)
|
127
|
+
remove_by_id(clazz, id)
|
128
|
+
commit
|
129
|
+
end
|
130
|
+
|
131
|
+
#
|
132
|
+
# See Sunspot.remove_all
|
133
|
+
#
|
134
|
+
def remove_all(*classes)
|
135
|
+
classes.flatten!
|
136
|
+
if classes.empty?
|
137
|
+
@deletes += 1
|
138
|
+
Indexer.remove_all(master_connection)
|
139
|
+
else
|
140
|
+
@deletes += classes.length
|
141
|
+
classes.each { |clazz| indexer.remove_all(clazz) }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
# See Sunspot.remove_all!
|
147
|
+
#
|
148
|
+
def remove_all!(*classes)
|
149
|
+
remove_all(*classes)
|
150
|
+
commit
|
151
|
+
end
|
152
|
+
|
153
|
+
#
|
154
|
+
# See Sunspot.dirty?
|
155
|
+
#
|
156
|
+
def dirty?
|
157
|
+
(@deletes + @adds) > 0
|
158
|
+
end
|
159
|
+
|
160
|
+
#
|
161
|
+
# See Sunspot.commit_if_dirty
|
162
|
+
#
|
163
|
+
def commit_if_dirty
|
164
|
+
commit if dirty?
|
165
|
+
end
|
166
|
+
|
167
|
+
#
|
168
|
+
# See Sunspot.delete_dirty?
|
169
|
+
#
|
170
|
+
def delete_dirty?
|
171
|
+
@deletes > 0
|
172
|
+
end
|
173
|
+
|
174
|
+
#
|
175
|
+
# See Sunspot.commit_if_delete_dirty
|
176
|
+
#
|
177
|
+
def commit_if_delete_dirty
|
178
|
+
commit if delete_dirty?
|
179
|
+
end
|
180
|
+
|
181
|
+
#
|
182
|
+
# See Sunspot.batch
|
183
|
+
#
|
184
|
+
def batch
|
185
|
+
indexer.start_batch
|
186
|
+
yield
|
187
|
+
indexer.flush_batch
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
#
|
193
|
+
# Retrieve the Solr connection for this session, creating one if it does not
|
194
|
+
# already exist.
|
195
|
+
#
|
196
|
+
# ==== Returns
|
197
|
+
#
|
198
|
+
# Solr::Connection:: The connection for this session
|
199
|
+
#
|
200
|
+
def connection
|
201
|
+
@connection ||=
|
202
|
+
begin
|
203
|
+
connection = self.class.connection_class.connect(
|
204
|
+
:url => config.solr.url
|
205
|
+
)
|
206
|
+
connection
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
#
|
211
|
+
# Retrieve the Solr connection to the master for this session, creating one
|
212
|
+
# if it does not already exist.
|
213
|
+
#
|
214
|
+
# ==== Returns
|
215
|
+
#
|
216
|
+
# Solr::Connection:: The connection for this session
|
217
|
+
#
|
218
|
+
def master_connection
|
219
|
+
@master_connection ||=
|
220
|
+
begin
|
221
|
+
if config.master_solr.url && config.master_solr.url != config.solr.url
|
222
|
+
master_connection = self.class.connection_class.new(
|
223
|
+
RSolr::Connection::NetHttp.new(:url => config.master_solr.url)
|
224
|
+
)
|
225
|
+
master_connection
|
226
|
+
else
|
227
|
+
connection
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def indexer
|
233
|
+
@indexer ||= Indexer.new(master_connection)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|