Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Opting for a proper string attribute for determining search results' …

…classes.

However, can't remove class_crc completely - as we're using it for filters in STI situations, and string filters sadly don't work.
  • Loading branch information...
commit a012b211e0b49a4290cad7dbca9c08723c2b942c 1 parent a548361
Pat Allan pat authored
10 lib/thinking_sphinx/class_facet.rb
View
@@ -5,12 +5,16 @@ def name
end
def attribute_name
- "class_crc"
+ Riddle.loaded_version.to_i < 2 ? 'class_crc' : 'sphinx_internal_class'
end
def value(object, attribute_hash)
- crc = attribute_hash['class_crc']
- ThinkingSphinx::Configuration.instance.models_by_crc[crc]
+ if Riddle.loaded_version.to_i < 2
+ crc = attribute_hash['class_crc']
+ ThinkingSphinx::Configuration.instance.models_by_crc[crc]
+ else
+ attribute_hash['sphinx_internal_class']
+ end
end
end
end
1  lib/thinking_sphinx/facet.rb
View
@@ -16,6 +16,7 @@ def self.name_for(facet)
when Facet
facet.name
when String, Symbol
+ return :class if facet.to_s == 'sphinx_internal_class'
facet.to_s.gsub(/(_facet|_crc)$/,'').to_sym
end
end
6 lib/thinking_sphinx/facet_search.rb
View
@@ -29,7 +29,7 @@ def facet_names
names = options[:all_facets] ?
facet_names_for_all_classes : facet_names_common_to_all_classes
- names.delete "class_crc" unless options[:class_facet]
+ names.delete class_facet unless options[:class_facet]
names
end
end
@@ -162,5 +162,9 @@ def facet_from_name(name)
facet.name == name
}
end
+
+ def class_facet
+ Riddle.loaded_version.to_i < 2 ? 'class_crc' : 'sphinx_internal_class'
+ end
end
end
19 lib/thinking_sphinx/search.rb
View
@@ -474,9 +474,12 @@ def add_matching_fields
def match_hash(object)
@results[:matches].detect { |match|
+ class_crc = object.class.name
+ class_crc = object.class.to_crc32 if Riddle.loaded_version.to_i < 2
+
match[:attributes]['sphinx_internal_id'] == object.
primary_key_for_sphinx &&
- match[:attributes]['class_crc'] == object.class.to_crc32
+ match[:attributes][crc_attribute] == class_crc
}
end
@@ -874,7 +877,7 @@ def instances_from_matches
return single_class_results if one_class
groups = results[:matches].group_by { |match|
- match[:attributes]["class_crc"]
+ match[:attributes][crc_attribute]
}
groups.each do |crc, group|
group.replace(
@@ -884,7 +887,7 @@ def instances_from_matches
results[:matches].collect do |match|
groups.detect { |crc, group|
- crc == match[:attributes]["class_crc"]
+ crc == match[:attributes][crc_attribute]
}[1].compact.detect { |obj|
obj.primary_key_for_sphinx == match[:attributes]["sphinx_internal_id"]
}
@@ -896,7 +899,11 @@ def single_class_results
end
def class_from_crc(crc)
- config.models_by_crc[crc].constantize
+ if Riddle.loaded_version.to_i < 2
+ config.models_by_crc[crc].constantize
+ else
+ crc.constantize
+ end
end
def each_with_attribute(attribute, &block)
@@ -949,5 +956,9 @@ def scoped_count
results_count
end
+
+ def crc_attribute
+ Riddle.loaded_version.to_i < 2 ? 'class_crc' : 'sphinx_internal_class'
+ end
end
end
11 lib/thinking_sphinx/source/internal_properties.rb
View
@@ -4,10 +4,15 @@ module InternalProperties
def add_internal_attributes_and_facets
add_internal_attribute :sphinx_internal_id, nil,
@model.primary_key_for_sphinx.to_sym
- add_internal_attribute :class_crc, :integer, crc_column, true
add_internal_attribute :sphinx_deleted, :integer, "0"
-
- add_internal_facet :class_crc
+ add_internal_attribute :class_crc, :integer, crc_column, true
+
+ unless Riddle.loaded_version.to_i < 2
+ add_internal_attribute :sphinx_internal_class, :string, internal_class_column, true
+ add_internal_facet :sphinx_internal_class
+ else
+ add_internal_facet :class_crc
+ end
end
def add_internal_attribute(name, type, contents, facet = false)
9 lib/thinking_sphinx/source/sql.rb
View
@@ -128,6 +128,15 @@ def crc_column
end
end
+ def internal_class_column
+ if @model.table_exists? &&
+ @model.column_names.include?(@model.inheritance_column)
+ adapter.quote_with_table(@model.inheritance_column)
+ else
+ "'#{@model.name}'"
+ end
+ end
+
def type_values
@model.connection.select_values <<-SQL
SELECT DISTINCT #{@model.inheritance_column}
5 spec/spec_helper.rb
View
@@ -50,8 +50,9 @@ def minimal_result_hashes(*instances)
{
:weight => 21,
:attributes => {
- 'sphinx_internal_id' => instance.id,
- 'class_crc' => instance.class.name.to_crc32
+ 'sphinx_internal_id' => instance.id,
+ 'sphinx_internal_class' => instance.class.name,
+ 'class_crc' => instance.class.name.to_crc32
}
}
end
18 spec/thinking_sphinx/facet_search_spec.rb
View
@@ -16,16 +16,22 @@
it "should request all shared facets in a multi-model request by default" do
ThinkingSphinx.stub!(:search => search)
- ThinkingSphinx::FacetSearch.new.facet_names.should == ['class_crc']
+ if Riddle.loaded_version.to_i < 2
+ ThinkingSphinx::FacetSearch.new.facet_names.should == ['class_crc']
+ else
+ ThinkingSphinx::FacetSearch.new.facet_names.should == ['sphinx_internal_class']
+ end
end
it "should request all facets in a multi-model request if specified" do
ThinkingSphinx.stub!(:search => search)
- ThinkingSphinx::FacetSearch.new(
- :all_facets => true
- ).facet_names.should == [
- 'class_crc', 'city_facet', 'state_facet', 'birthday'
- ]
+ names = ThinkingSphinx::FacetSearch.new(:all_facets => true).facet_names
+
+ if Riddle.loaded_version.to_i < 2
+ names.should == ['class_crc', 'city_facet', 'state_facet', 'birthday']
+ else
+ names.should == ['sphinx_internal_class', 'city_facet', 'state_facet', 'birthday']
+ end
end
it "should use the system-set max_matches for limit on facet calls" do
61 spec/thinking_sphinx/index/builder_spec.rb
View
@@ -1,6 +1,9 @@
require 'spec_helper'
describe ThinkingSphinx::Index::Builder do
+ let(:internal_attribute_count) {
+ Riddle.loaded_version.to_i < 2 ? 3 : 4
+ }
describe ".generate without source scope" do
before :each do
@index = ThinkingSphinx::Index::Builder.generate(Person) do
@@ -31,10 +34,10 @@
@source.fields[1].unique_name.should == :last_name
end
- it "should have two attributes alongside the three internal ones" do
- @source.attributes.length.should == 5
- @source.attributes[3].unique_name.should == :birthday
- @source.attributes[4].unique_name.should == :internal_id
+ it "should have two attributes alongside the internal ones" do
+ @source.attributes.length.should == 2 + internal_attribute_count
+ @source.attributes[internal_attribute_count].unique_name.should == :birthday
+ @source.attributes[1 + internal_attribute_count].unique_name.should == :internal_id
end
it "should have one condition" do
@@ -95,8 +98,8 @@
@source.fields.length.should == 1
end
- it "should have one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
it "should set the attribute name to have the _sort suffix" do
@@ -142,8 +145,8 @@
@source.fields.length.should == 1
end
- it "should have one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
it "should set the attribute name to have the _facet suffix" do
@@ -174,8 +177,8 @@
Alpha.sphinx_facets.delete_at(-1)
end
- it "should have just one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have just one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
end
@@ -193,8 +196,8 @@
Person.sphinx_facets.delete_at(-1)
end
- it "should have just one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have just one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
end
@@ -212,8 +215,8 @@
Beta.sphinx_facets.delete_at(-1)
end
- it "should have just one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have just one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
end
@@ -231,8 +234,8 @@
Alpha.sphinx_facets.delete_at(-1)
end
- it "should have just one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have just one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
end
@@ -250,8 +253,8 @@
Person.sphinx_facets.delete_at(-1)
end
- it "should have two attributes alongside the three internal ones" do
- @source.attributes.length.should == 5
+ it "should have two attributes alongside the internal ones" do
+ @source.attributes.length.should == 2 + internal_attribute_count
end
it "should set the facet attribute name to have the _facet suffix" do
@@ -282,8 +285,8 @@
Person.sphinx_facets.delete_at(-1)
end
- it "should have two attributes alongside the three internal ones" do
- @source.attributes.length.should == 5
+ it "should have two attributes alongside the internal ones" do
+ @source.attributes.length.should == 2 + internal_attribute_count
end
it "should set the facet attribute name to have the _facet suffix" do
@@ -321,8 +324,8 @@
@source.fields.length.should == 1
end
- it "should have one attribute alongside the three internal ones" do
- @source.attributes.length.should == 4
+ it "should have one attribute alongside the internal ones" do
+ @source.attributes.length.should == 1 + internal_attribute_count
end
it "should set the attribute name to have the _facet suffix" do
@@ -381,10 +384,10 @@
@source.fields[1].unique_name.should == :last_name
end
- it "should have two attributes alongside the three internal ones" do
- @source.attributes.length.should == 5
- @source.attributes[3].unique_name.should == :birthday
- @source.attributes[4].unique_name.should == :internal_id
+ it "should have two attributes alongside the internal ones" do
+ @source.attributes.length.should == 2 + internal_attribute_count
+ @source.attributes[internal_attribute_count].unique_name.should == :birthday
+ @source.attributes[1 + internal_attribute_count].unique_name.should == :internal_id
end
end
@@ -418,12 +421,12 @@
end
it "should have two attributes alongside the six internal ones" do
- @index.attributes.length.should == 8
+ @index.attributes.length.should == 2 + (internal_attribute_count * 2)
end
- it "should have one attribute in each source alongside the three internal ones" do
+ it "should have one attribute in each source alongside the internal ones" do
@index.sources.each do |source|
- source.attributes.length.should == 4
+ source.attributes.length.should == 1 + internal_attribute_count
end
end
end
25 spec/thinking_sphinx/search_spec.rb
View
@@ -1135,10 +1135,11 @@
@client.stub! :query => {
:matches => [{
:attributes => {
- 'sphinx_internal_id' => @alpha.id,
- 'class_crc' => Alpha.to_crc32,
- '@groupby' => 101,
- '@count' => 5
+ 'sphinx_internal_id' => @alpha.id,
+ 'sphinx_internal_class' => 'Alpha',
+ 'class_crc' => Alpha.to_crc32,
+ '@groupby' => 101,
+ '@count' => 5
}
}]
}
@@ -1172,8 +1173,9 @@
@client.stub! :query => {
:matches => [{
:attributes => {
- 'sphinx_internal_id' => @alpha.id,
- 'class_crc' => Alpha.to_crc32
+ 'sphinx_internal_id' => @alpha.id,
+ 'sphinx_internal_class' => 'Alpha',
+ 'class_crc' => Alpha.to_crc32
}, :weight => 12
}]
}
@@ -1197,11 +1199,12 @@
@client.stub! :query => {
:matches => [{
:attributes => {
- 'sphinx_internal_id' => @alpha.id,
- 'class_crc' => Alpha.to_crc32,
- '@geodist' => 101,
- '@groupby' => 102,
- '@count' => 103
+ 'sphinx_internal_id' => @alpha.id,
+ 'sphinx_internal_class' => 'Alpha',
+ 'class_crc' => Alpha.to_crc32,
+ '@geodist' => 101,
+ '@groupby' => 102,
+ '@count' => 103
}, :weight => 12
}]
}
Please sign in to comment.
Something went wrong with that request. Please try again.