Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hannahwhy/337873 to your computer and use it in GitHub Desktop.

Select an option

Save hannahwhy/337873 to your computer and use it in GitHub Desktop.
From 5d01e863ced6160fc09956259f573988e4e21ddc Mon Sep 17 00:00:00 2001
From: David Yip <[email protected]>
Date: Fri, 19 Mar 2010 12:05:29 -0500
Subject: [PATCH] Use a text? predicate for determining whether a field type is suitable for full-text search.
Previously, class matching on Type::TextType was used, which not only
prohibited custom definitions of full-text search field types, but
also resulted in unexpected behavior when using subclasses of
Type::TextType.
Specifically, fields using subclasses of Type::TextType would have their
types changed to Type::TextType in their corresponding factories, which
would cause the wrong field type to be used in Sunspot and ultimately
Solr.
---
sunspot/lib/sunspot/field.rb | 4 ++--
sunspot/lib/sunspot/field_factory.rb | 4 ++--
sunspot/lib/sunspot/setup.rb | 17 +++++++++++++----
sunspot/lib/sunspot/type.rb | 12 ++++++++++++
4 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/sunspot/lib/sunspot/field.rb b/sunspot/lib/sunspot/field.rb
index fe57c90..c8c52b8 100644
--- a/sunspot/lib/sunspot/field.rb
+++ b/sunspot/lib/sunspot/field.rb
@@ -98,8 +98,8 @@ module Sunspot
class FulltextField < Field #:nodoc:
attr_reader :default_boost
- def initialize(name, options = {})
- super(name, Type::TextType.instance, options)
+ def initialize(name, type, options = {})
+ super(name, type, options)
@multiple = true
@boost = options.delete(:boost)
@default_boost = options.delete(:default_boost)
diff --git a/sunspot/lib/sunspot/field_factory.rb b/sunspot/lib/sunspot/field_factory.rb
index 1420a29..e6cb1fb 100644
--- a/sunspot/lib/sunspot/field_factory.rb
+++ b/sunspot/lib/sunspot/field_factory.rb
@@ -36,8 +36,8 @@ module Sunspot
raise ArgumentError, "Invalid field name #{name}: only letters, numbers, and underscores are allowed."
end
@field =
- if type.is_a?(Type::TextType)
- FulltextField.new(name, options)
+ if type.text?
+ FulltextField.new(name, type, options)
else
AttributeField.new(name, type, options)
end
diff --git a/sunspot/lib/sunspot/setup.rb b/sunspot/lib/sunspot/setup.rb
index 1982974..f3377c7 100644
--- a/sunspot/lib/sunspot/setup.rb
+++ b/sunspot/lib/sunspot/setup.rb
@@ -26,10 +26,19 @@ module Sunspot
def add_field_factory(name, type, options = {}, &block)
stored = options[:stored]
field_factory = FieldFactory::Static.new(name, type, options, &block)
- @field_factories[field_factory.signature] = field_factory
- @field_factories_cache[field_factory.name] = field_factory
- if stored
- @stored_field_factories_cache[field_factory.name] << field_factory
+
+ if !type.text?
+ @field_factories[field_factory.signature] = field_factory
+ @field_factories_cache[field_factory.name] = field_factory
+ if stored
+ @stored_field_factories_cache[field_factory.name] << field_factory
+ end
+ else
+ @text_field_factories[name] = field_factory
+ @text_field_factories_cache[field_factory.name] = field_factory
+ if stored
+ @stored_field_factories_cache[field_factory.name] << field_factory
+ end
end
end
diff --git a/sunspot/lib/sunspot/type.rb b/sunspot/lib/sunspot/type.rb
index 0473f29..7475f96 100644
--- a/sunspot/lib/sunspot/type.rb
+++ b/sunspot/lib/sunspot/type.rb
@@ -15,6 +15,10 @@ module Sunspot
# +cast+::
# Convert a Solr string representation of a value into the appropriate
# Ruby type.
+ # +accepts_dynamic?+::
+ # Whether or not this field type can be used to define dynamic fields.
+ # +text?+::
+ # Whether or not this field type can be used for full-text search.
#
module Type
class AbstractType #:nodoc:
@@ -28,6 +32,10 @@ module Sunspot
def accepts_dynamic?
true
end
+
+ def text?
+ false
+ end
end
#
@@ -53,6 +61,10 @@ module Sunspot
def accepts_dynamic?
false
end
+
+ def text?
+ true
+ end
end
#
--
1.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment