view · edit · sidebar · attach · print · history

Searching in ODBA via Ruby and an Index

<< ConditionIndex | Index | TransparentHashAccess >>

Requirements: all Indices are tested with postgresql only. For postgresql, you need to:

  • extend your table with tsearch2
 psql -U postgres test < $POSTGRESQL/contrib/tsearch2.sql 
  • ensure your test user has read-access to the tsearch_tables
 cat <<EOS | psql -U postgres test
 grant all on pg_ts_cfg to test;
 grant all on pg_ts_cfgmap to test;
 grant all on pg_ts_dict to test;
 grant all on pg_ts_parser to test;

The Example-Code:

 #!/usr/bin/env ruby

 require 'odba'
 require 'odba/index_definition'

 class User
  attr_accessor :first_name, :last_name, :vita
  include ODBA::Persistable
  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  end
  def to_s
    "#@first_name #@last_name"
  end
 end

 index_def = YAML.load <<-EOD
 --- !ruby/object:ODBA::IndexDefinition
 index_name: 'users'
 origin_klass: 'User'
 target_klass: 'User'
 resolve_search_term: vita
 resolve_target: ''
 resolve_origin: ''
 fulltext: true
 dictionary: default
 EOD

 ODBA.storage.dbi = DBI.connect('DBI:pg:test', 'test', '')
 ODBA.cache.setup
 begin
   ODBA.cache.drop_index('users')
 rescue
 end
 ODBA.cache.create_index(index_def, Object)

 father = User.new('Johann Sebastian', 'Bach')
 father.vita = <<-EOS # from: http://en.wikipedia.org/wiki/Bach
 At the age of 14, Sebastian was awarded a choral scholarship, with his older
 school friend, Georg Erdmann, to study at the prestigious St Michael's
 School in Lüneburg, not far from Hamburg, the largest city in Germany. This
 involved a long journey with his friend, probably partly on foot and partly by
 coach. His two years there appear to have been critical in exposing him to a
 wider palette of European culture than he would have experienced in Thuringia.
 In addition to singing a cappella in the choir, it is likely that he played
 the School's three-manual organ and harpsichords. He probably learned French
 and Italian, and received a thorough grounding in theology, Latin, history,
 geography and physics. He would have come into contact with sons of noblemen
 from northern Germany sent to the highly selective school to prepare for
 careers in diplomacy, government and the military. It is likely that he had
 significant contact with organists in Lüneburg, in particular Georg Böhm, and
 visited several of those in Hamburg, such as Reincken and Bruhns. Through
 these musicians, he probably gained access to the largest instruments he had
 played. It is also likely that he became acquainted with the music of the
 North German tradition, especially the work of Dieterich Buxtehude, with music
 manuscripts from further afield, and with treatises on music theory that were
 in the possession of these men.
 EOS
 father.odba_store
 son = User.new('Carl Philipp Emanuel', 'Bach')
 son.vita = <<-EOS # from: http://en.wikipedia.org/wiki/Carl_Philipp_Emanuel_Bach
 In 1768 Bach succeeded Georg Philipp Telemann as Capellmeister at Hamburg, and
 in consequence of his new office began to turn his attention more towards
 church music. Next year he produced his oratorio Die Israeliten in der Wüste,
 a composition remarkable not only for its great beauty but for the resemblance
 of its plan to that of Felix Mendelssohn's Elijah, and between 1769 and 1788
 added over twenty settings of the Passion, and some seventy cantatas,
 litanies, motets, and other liturgical pieces. At the same time, his genius
 for instrumental composition was further stimulated by the career of Joseph
 Haydn. He died in Hamburg on December 14, 1788.
 EOS
 son.odba_store

 puts ODBA.cache.retrieve_from_index('users', 'telemann')

 # -> Carl Philipp Emanuel Bach


 puts ODBA.cache.retrieve_from_index('users', 'buxtehude')

 # -> Johann Sebastian Bach


 puts ODBA.cache.retrieve_from_index('users', 'haydn')

 # -> Carl Philipp Emanuel Bach


 puts ODBA.cache.retrieve_from_index('users', 'lüneburg')

 # -> Johann Sebastian Bach
view · edit · sidebar · attach · print · history
Page last modified on March 14, 2011, at 05:28 PM