I''m having trouble figuring out how to do Ferret queries across multiple tables as you would in a normal SQL call. For example, let''s say I have two ActiveRecord classes, Book, and Author, where Book has a ''description'' field and Author has a ''name'' field, and where Book has a belongs_to relationship with Author (Book belolngs to Author, Author has_many Books). Let''s say I''d like to find all Books with the term ''programming'' in their description that are by people with ''Smith'' in their name. I''d like to do something like this: Book.find_by_contents("books.description:programming AND authors.name:Smith", {}, {:include => :author}) However, this function does not seem to allow the specification of table names. This type of call would be relatively easy in plain SQL, but I''d like to use Ferret for all queries to keep things uniform and to take advantage of its speed. I''ve looked into using the multi_search option also, but can''t figure out how to use it to do even simple joins such as this one. Any help would be greatly appreciated. Thanks. -- Posted via http://www.ruby-forum.com/.
I realized that I wasn''t fully clear in this example...I would want returned all Book objects where the Book description contains the word ''programming'', the Author name contains the word ''Smith'', AND (what I didn''t explicitly say before) the book is by that author. So more informally, I want to find all programming books who are by someone with ''Smith'' in their name. This would amount to the extra join statment of ''books.author_id=author.id''. I''m having a really hard time figure out how to do this in Ferret (using acts_as_ferret). Jon wrote:> I''m having trouble figuring out how to do Ferret queries across multiple > tables as you would in a normal SQL call. For example, let''s say I have > two ActiveRecord classes, Book, and Author, where Book has a > ''description'' field and Author has a ''name'' field, and where Book has a > belongs_to relationship with Author (Book belolngs to Author, Author > has_many Books). Let''s say I''d like to find all Books with the term > ''programming'' in their description that are by people with ''Smith'' in > their name. I''d like to do something like this: > > Book.find_by_contents("books.description:programming AND > authors.name:Smith", > {}, > {:include => :author}) > > However, this function does not seem to allow the specification of table > names. This type of call would be relatively easy in plain SQL, but I''d > like to use Ferret for all queries to keep things uniform and to take > advantage of its speed. I''ve looked into using the multi_search option > also, but can''t figure out how to use it to do even simple joins such as > this one. Any help would be greatly appreciated. Thanks.-- Posted via http://www.ruby-forum.com/.
On 10/19/06, Jon <jon at mywellnet.com> wrote:> I realized that I wasn''t fully clear in this example...I would want > returned all Book objects where the Book description contains the word > ''programming'', the Author name contains the word ''Smith'', AND (what I > didn''t explicitly say before) the book is by that author. So more > informally, I want to find all programming books who are by someone with > ''Smith'' in their name. This would amount to the extra join statment of > ''books.author_id=author.id''. I''m having a really hard time figure out > how to do this in Ferret (using acts_as_ferret). > > Jon wrote: > > I''m having trouble figuring out how to do Ferret queries across multiple > > tables as you would in a normal SQL call. For example, let''s say I have > > two ActiveRecord classes, Book, and Author, where Book has a > > ''description'' field and Author has a ''name'' field, and where Book has a > > belongs_to relationship with Author (Book belolngs to Author, Author > > has_many Books). Let''s say I''d like to find all Books with the term > > ''programming'' in their description that are by people with ''Smith'' in > > their name. I''d like to do something like this: > > > > Book.find_by_contents("books.description:programming AND > > authors.name:Smith", > > {}, > > {:include => :author}) > > > > However, this function does not seem to allow the specification of table > > names. This type of call would be relatively easy in plain SQL, but I''d > > like to use Ferret for all queries to keep things uniform and to take > > advantage of its speed. I''ve looked into using the multi_search option > > also, but can''t figure out how to use it to do even simple joins such as > > this one. Any help would be greatly appreciated. Thanks.Hi Jon, Ferret isn''t a database (yet?[1]). If you need to run queries like this then you should index the author name in the book document and forget about doing joins for the moment. Queries will be a lot faster this way too. I think this is pretty easy in acts as ferret. Just define the method author_names def author_names authors.map{|author| author.name} end And make sure :author_names is in your list of fields to index. Cheers, Dave [1] http://www.mail-archive.com/ferret-talk at rubyforge.org/msg01183.html
Thanks a ton for the quick reply...very much appreciated. And thanks for the great software as well. David Balmain wrote:> On 10/19/06, Jon <jon at mywellnet.com> wrote: >> > I''m having trouble figuring out how to do Ferret queries across multiple >> > {}, >> > {:include => :author}) >> > >> > However, this function does not seem to allow the specification of table >> > names. This type of call would be relatively easy in plain SQL, but I''d >> > like to use Ferret for all queries to keep things uniform and to take >> > advantage of its speed. I''ve looked into using the multi_search option >> > also, but can''t figure out how to use it to do even simple joins such as >> > this one. Any help would be greatly appreciated. Thanks. > > Hi Jon, > > Ferret isn''t a database (yet?[1]). If you need to run queries like-- Posted via http://www.ruby-forum.com/.