Argh, after all that, I got the syntax wrong. Sorry.
Try Track.search(...).results[:matches]
--
Pat
On 04/06/2009, at 4:25 PM, j0llyr0g3r wrote:
>
> Ah, ok...:-)
>
> But are you sure this is correct?
>
> Because:
>
> Track.search('beat impelling', :order => 'category_name ASC')
> [:matches]
>
> gives me:
>
> TypeError: Symbol as array index
> from (irb):1:in `[]'
> from (irb):1
>
>
> On Jun 4, 4:15 pm, Pat Allan <[email protected]> wrote:
>> Ah, that's just the email client wrapping my code - put it all on one
>> line. Here's another example without the search term (although you
>> should keep that in):
>> Track.search(:order => 'category_name ASC')[:matches]
>>
>> --
>> Pat
>>
>> On 04/06/2009, at 7:51 AM, j0llyr0g3r wrote:
>>
>>
>>
>>> Hey Pat,
>>
>>> sorry, I am not sure what you mean? Is this:
>>
>>>> What's the output of the following?
>>>> Track.search('beat impelling', :order => 'category_name ASC')
>>>> [:matches]
>>
>>> a typo?
>>> Because i read your peepcode and something like "[:matches]" is
>>> nowhere mentioned.....
>>
>>> If not, here is the output in "script/console":
>>
>>>>> Track.search('beat impelling', :order => 'category_name ASC')
>>> => [#<Track id: 1, artist: "foobar", title:
>>> "House", ................
>>>>> [:matches]
>>> => [:matches]
>>
>>> On Jun 4, 12:09 am, Pat Allan <[email protected]> wrote:
>>>> What's the output of the following?
>>>> Track.search('beat impelling', :order => 'category_name ASC')
>>>> [:matches]
>>
>>>> --
>>>> Pat
>>
>>>> On 03/06/2009, at 11:44 AM, j0llyr0g3r wrote:
>>
>>>>> Hi Pat,
>>
>>>>> and thanks for your insights on this issue. I had to tweak your
>>>>> proposal since
>>
>>>>> indexes category.category_translations , :as => :category
>>
>>>>> won't work as category_translations is a 'real' table.
>>
>>>>> I defined my indexes now like that:
>>
>>>>> has category.category_translations.name, :as => :category_name
>>>>> has theme.theme_translations.name, :as => :theme_name
>>
>>>>> I reindexed and restarted sphinx.
>>
>>>>> Unfortunately, it still isn't working:
>>
>>>>> Track.search 'beat', :order => 'category_name asc'
>>
>>>>> now yields a correct resultset, but the sorting still doesn't
>>>>> work.
>>>>> The following example illustrates this:
>>
>>>>>>> Track.search('beat impelling', :order => 'category_name
>>>>>>> ASC').each
>>>>>>> {|f| puts f.category.name}
>>>>> Electro / House
>>>>> Hip Hop
>>>>> Funk
>>>>> Techno / Trance
>>>>> Rock
>>>>> Pop
>>>>> Hip Hop
>>>>> Electro / House
>>>>> Rock
>>>>> Rock
>>>>> Pop
>>
>>>>> "Tailing" my development.log at the same time shows this:
>>
>>>>> Sphinx: beat impelling
>>>>> Sphinx Result: [1, 2, 13, 15, 18, 20, 22, 24, 39, 42, 49]
>>>>> Track Load (4.6ms) SELECT * FROM `tracks` WHERE
>>>>> (`tracks`.`id` IN
>>>>> (1,2,13,15,18,20,22,24,39,42,49))
>>>>> Category Load (3.2ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 4)
>>>>> CategoryTranslation Load (0.9ms) SELECT * FROM
>>>>> `category_translations` WHERE (`category_translations`.category_id
>>>>> = 4
>>>>> AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.6ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 7)
>>>>> CategoryTranslation Load (0.8ms) SELECT * FROM
>>>>> `category_translations` WHERE (`category_translations`.category_id
>>>>> = 7
>>>>> AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.6ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 6)
>>>>> CategoryTranslation Load (0.8ms) SELECT * FROM
>>>>> `category_translations` WHERE (`category_translations`.category_id
>>>>> = 6
>>>>> AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.6ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 16)
>>>>> CategoryTranslation Load (4.4ms) SELECT * FROM
>>>>> `category_translations` WHERE
>>>>> (`category_translations`.category_id =
>>>>> 16 AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (2.7ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 15)
>>>>> CategoryTranslation Load (0.8ms) SELECT * FROM
>>>>> `category_translations` WHERE
>>>>> (`category_translations`.category_id =
>>>>> 15 AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (2.3ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 13)
>>>>> CategoryTranslation Load (0.8ms) SELECT * FROM
>>>>> `category_translations` WHERE
>>>>> (`category_translations`.category_id =
>>>>> 13 AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.5ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 7)
>>>>> CategoryTranslation Load (0.8ms) SELECT * FROM
>>>>> `category_translations` WHERE (`category_translations`.category_id
>>>>> = 7
>>>>> AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.5ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 4)
>>>>> CategoryTranslation Load (0.8ms) SELECT * FROM
>>>>> `category_translations` WHERE (`category_translations`.category_id
>>>>> = 4
>>>>> AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.5ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 15)
>>>>> CategoryTranslation Load (6.6ms) SELECT * FROM
>>>>> `category_translations` WHERE
>>>>> (`category_translations`.category_id =
>>>>> 15 AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.6ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 15)
>>>>> CategoryTranslation Load (1.2ms) SELECT * FROM
>>>>> `category_translations` WHERE
>>>>> (`category_translations`.category_id =
>>>>> 15 AND (`category_translations`.`locale` IN ('en','root')))
>>>>> Category Load (0.6ms) SELECT * FROM `categories` WHERE
>>>>> (`categories`.`id` = 13)
>>>>> CategoryTranslation Load (0.9ms) SELECT * FROM
>>>>> `category_translations` WHERE
>>>>> (`category_translations`.category_id =
>>>>> 13 AND (`category_translations`.`locale` IN ('en','root')))
>>
>>>>> I don't get it, no errors in the logs, but obviously the order-
>>>>> clause
>>>>> is ignored?
>>
>>>>> What am i doing wrong here?
>>
>>>>> On May 31, 8:25 pm, Pat Allan <[email protected]> wrote:
>>>>>> You'll have to do *one* of the two following things:
>>>>>> - Add that column as a sortable field (if you want the contents
>>>>>> indexed for searching)
>>>>>> indexes category.category_translations, :as
>>>>>> => :category, :sortable
>>>>>> => true
>>>>>> - Or, add it as an attribute, which is implicitly sortable, but
>>>>>> not
>>>>>> indexed for searching
>>>>>> has category.category_translations, :as => :category
>>
>>>>>> And then stop Sphinx, re-index and restart Sphinx (rake
>>>>>> thinking_sphinx:rebuild if you're running a new version of TS)
>>>>>> and
>>>>>> then you can search as follows:
>>>>>> Track.search 'beat', :order => :category # or 'category ASC'
>>
>>>>>> The problem with your current approach: Sphinx doesn't use SQL
>>>>>> for
>>>>>> searches. :joins and table names in :order mean nothing to
>>>>>> Sphinx,
>>>>>> and
>>>>>> so it gets confused and returns no results.
>>
>>>>>> Cheers
>>
>>>>>> --
>>>>>> Pat
>>
>>>>>> On 31/05/2009, at 11:04 AM, j0llyr0g3r wrote:
>>
>>>>>>> Hey guys,
>>
>>>>>>> i have a model "track" with the following index structure:
>>
>>>>>>> define_index do
>>>>>>> indexes tags, :sortable => true
>>>>>>> indexes short_description, :sortable => true
>>>>>>> indexes full_description, :sortable => true
>>>>>>> indexes artist, :sortable => true
>>>>>>> indexes title, :sortable => true
>>>>>>> end
>>
>>>>>>> This works fine so far....
>>
>>>>>>> Now, my model "track" has a belongs_to-association to the model
>>>>>>> "category", which in turn has a "has_many"-association to the
>>>>>>> model
>>>>>>> "category_translations" (globalize2)
>>
>>>>>>> So i thought i could do the following query and get back the
>>>>>>> results
>>>>>>> sorted via category-name:
>>
>>>>>>> Track.search 'beat', :order => 'category_translations.name
>>>>>>> ASC', :joins => {:category => :category_translations}
>>
>>>>>>> but this gives me an empty resultset.
>>
>>>>>>> In my log-files i can see the error:
>>
>>>>>>> Sphinx: beat
>>>>>>> Sphinx Result: []
>>>>>>> Sphinx Error: index track_core: sort-by attribute
>>>>>>> 'category_translations' not found
>>
>>>>>>> What do i have to do in order to be able to sort after the
>>>>>>> category-
>>>>>>> name?
>>>>>>> Do i have to index it separately?
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en
-~----------~----~----~----~------~----~------~--~---