Skip to content

Instantly share code, notes, and snippets.

@thomasn
Created August 29, 2009 15:05
Show Gist options
  • Save thomasn/177501 to your computer and use it in GitHub Desktop.
Save thomasn/177501 to your computer and use it in GitHub Desktop.
... -- see http://github.com/thomasn/forgetmenot/tree/master for full listing
class Contact < ActiveRecord::Base
...
define_index do
indexes first_name, :sortable => true
indexes last_name, :sortable => true
indexes email, :sortable => true
indexes notes
# Add DynamicAttribute fields to index. Note that some Sphinx docs use the term "dynamic attributes" with a different meaning.
Contact.create_attributes # FIXME obsolete?
DynamicAttribute.find(:all).each do |da|
cmd = "(SELECT string_value FROM dynamic_attribute_values WHERE (dynamic_attribute_values.contact_id = " +
"contacts.id" +
" AND dynamic_attribute_values.dynamic_attribute_id = " +
"#{da.id}" +
") LIMIT 1)"
indexes "#{cmd}", :as => "#{da.name}".to_sym
end
set_property :enable_star => 1
set_property :min_infix_len => 3
set_property :delta => true
# attributes - used for sorting results etc:
# has created_at, updated_at # FIXME add timestamp migration
end
development:
port: 3312
searchd_file_path: sphinx_development_index
test:
port: 3313
searchd_file_path: sphinx_test_index
production:
port: 3314
searchd_file_path: sphinx_production_index
$ RAILS_ENV=development rake ts:rebuild
(in /home/thomasn/dev/forgetmenot/forgetmenot)
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 8; use 'listen' instead.
stop: succesfully sent SIGTERM to pid 5416
Stopped search daemon (pid 5416).
Generating Configuration to /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 8; use 'listen' instead.
indexing index 'contact_core'...
collected 1811 docs, 0.1 MB
collected 0 attr values
sorted 0.0 Mvalues, 100.0% done
sorted 0.3 Mhits, 100.0% done
total 1811 docs, 104941 bytes
total 9.277 sec, 11311 bytes/sec, 195.20 docs/sec
indexing index 'contact_delta'...
collected 0 docs, 0.0 MB
collected 0 attr values
sorted 0.0 Mvalues, nan% done
total 0 docs, 0 bytes
total 0.051 sec, 0 bytes/sec, 0.00 docs/sec
distributed index 'contact' can not be directly indexed; skipping.
total 15 reads, 0.005 sec, 156.7 kb/call avg, 0.3 msec/call avg
total 19 writes, 0.016 sec, 292.6 kb/call avg, 0.8 msec/call avg
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 8; use 'listen' instead.
listening on 127.0.0.1:3312
Started successfully (pid 5498).
#############################################################################
$ RAILS_ENV=test rake ts:rebuild
(in /home/thomasn/dev/forgetmenot/forgetmenot)
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf line 8; use 'listen' instead.
stop: succesfully sent SIGTERM to pid 5450
Stopped search daemon (pid 5450).
Generating Configuration to /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf line 8; use 'listen' instead.
indexing index 'contact_core'...
collected 4 docs, 0.0 MB
collected 0 attr values
sorted 0.0 Mvalues, 100.0% done
sorted 0.0 Mhits, 100.0% done
total 4 docs, 117 bytes
total 0.052 sec, 2225 bytes/sec, 76.08 docs/sec
indexing index 'contact_delta'...
collected 0 docs, 0.0 MB
collected 0 attr values
sorted 0.0 Mvalues, nan% done
total 0 docs, 0 bytes
total 0.046 sec, 0 bytes/sec, 0.00 docs/sec
distributed index 'contact' can not be directly indexed; skipping.
total 15 reads, 0.000 sec, 6.6 kb/call avg, 0.0 msec/call avg
total 16 writes, 0.000 sec, 0.7 kb/call avg, 0.0 msec/call avg
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/test.sphinx.conf line 8; use 'listen' instead.
listening on 127.0.0.1:3313
Started successfully (pid 5506).
#############################################################################
$ RAILS_ENV=development script/console
Loading development environment (Rails 2.3.2)
>> c = Contact.new :first_name => 'Hieronymous', :last_name => 'Bosch'
=> #<Contact id: nil, first_name: "Hieronymous", last_name: "Bosch", email: nil, phone_number: nil, title: nil, work_phone: nil, mobile_phone: nil, home_phone: nil, other_phone: nil, fax: nil, do_not_email: nil, do_not_phone: nil, notes: nil, address_id: nil, address2_id: nil, lead_source_id: nil, delta: true>
>> c.save!
Sphinx 0.9.9-rc2 (r1785)
Copyright (c) 2001-2009, Andrew Aksyonoff
using config file '/home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf'...
WARNING: key 'address' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 7; use 'listen' instead.
WARNING: key 'port' is deprecated in /home/thomasn/dev/forgetmenot/forgetmenot/config/development.sphinx.conf line 8; use 'listen' instead.
indexing index 'contact_delta'...
collected 1 docs, 0.0 MB
collected 0 attr values
sorted 0.0 Mvalues, 100.0% done
sorted 0.0 Mhits, 100.0% done
total 1 docs, 16 bytes
total 0.034 sec, 464 bytes/sec, 29.04 docs/sec
total 14 reads, 0.000 sec, 4.6 kb/call avg, 0.0 msec/call avg
total 12 writes, 0.000 sec, 0.2 kb/call avg, 0.0 msec/call avg
rotating indices: succesfully sent SIGHUP to searchd (pid=5498).
=> true
>> Contact.search('Bosch')
=> [#<Contact id: 2036, first_name: "Hieronymous", last_name: "Bosch", email: nil, phone_number: nil, title: nil, work_phone: nil, mobile_phone: nil, home_phone: nil, other_phone: nil, fax: nil, do_not_email: nil, do_not_phone: nil, notes: nil, address_id: nil, address2_id: nil, lead_source_id: nil, delta: true>]
#############################################################################
$ RAILS_ENV=test script/console
Loading test environment (Rails 2.3.2)
>> c = Contact.new :first_name => 'Hieronymous', :last_name => 'Bosch'
=> #<Contact id: nil, first_name: "Hieronymous", last_name: "Bosch", email: nil, phone_number: nil, title: nil, work_phone: nil, mobile_phone: nil, home_phone: nil, other_phone: nil, fax: nil, do_not_email: nil, do_not_phone: nil, notes: nil, address_id: nil, address2_id: nil, lead_source_id: nil, delta: true>
>> c.save!
=> true
>> Contact.search('Bosch')
=> []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment