Okay, the thing about real-time indices is that what you’re referring to within 
an index definition must be methods on your models. This is different to 
SQL-backed indices, where you refer to associations and columns.

So, this code needs to change:

  has network_users(:id), as: :network_users, :type => :integer 
  has network_users.network_id, as: :network_id, :type => :integer
  has id, as: :user_id, :type => :integer

  where "remove_flag = false"

  # For real-time indices, let's make sure we're using the appropriate tenant.
  scope { Apartment::Tenant.switch! tenant.db; User }

The first line needs to refer to a method, not an association + a column. It’s 
an easy enough translation, because a method returning what we want already 
exists due to Rails’ association magic:

  has network_user_ids, as: :network_users, type: :integer, multi: true

You’ll note that I’m adding the :multi option at the end there - because it’s a 
real-time index, we don’t have the database to refer to, hence why we must 
manually specify types for attributes, and also whether any of them are 
multi-value attributes.

The next line isn’t so easy - because you’ll first need to create a method for 
this purpose in your User model:

  def network_ids
    network_users.pluck :network_id
  end

Once you’ve got that, it’s easy enough to fix the attribute:

  has network_ids, type: :integer, multi: true

The third attribute is actually not required, because Thinking Sphinx creates 
an attribute already using the primary key value (id) - the attribute’s name is 
sphinx_internal_id, so you can just use that instead.

And the `where` method does not exist in real-time indices - you’re offering a 
SQL snippet, but real-time indices are understood in a Ruby context. However, 
this is part of the reason why `scope` exists instead - so let’s modify that 
accordingly:

  scope { Apartment::Tenant.switch! tenant.db; User.where(remove_flag: false) }

Putting that all together:

  has network_user_ids, as: :network_users, type: :integer, multi: true
  has network_ids,                          type: :integer, multi: true

  scope { Apartment::Tenant.switch! tenant.db; User.where(remove_flag: false) }

Plus, that method once again to go within your User model:

  def network_ids
    network_users.pluck :network_id
  end

I hope this all makes sense! Give it a shot and see how you go.

— 
Pat

> On 13 May 2015, at 10:25 pm, Frank H <[email protected]> wrote:
> 
> Haha it stopped by “user”~
> i know it might be incorrect that line~ (*≧∇≦*)
> please help~ very appreciate~ 
> 
> Current_tenant = Apartment::Tenant.current
> 
> # Each Tenant instance is tied to a tenant in this example.
> Tenant.find_each do |tenant|
> # Switch to the appropriate Apartment tenant.
> Apartment::Tenant.switch! tenant.db
> 
> #ThinkingSphinx::Index.define :user, :with => :active_record, :delta => true 
> do
> ThinkingSphinx::Index.define(
> :user,
> # with: :active_record,
> # delta: true,
> name: "user_#{tenant.db}",
> offset_as: "user_#{tenant.db}".to_sym,
> with: :real_time )do
> # fields
> indexes [first_name, last_name], as: :full_name #, sortable: true
> 
> # attributes
> has created_at, updated_at, :type => :timestamp
> has network_users(:id), as: :network_users, :type => :integer 
> has network_users.network_id, as: :network_id, :type => :integer
> has id, as: :user_id, :type => :integer
> 
> where "remove_flag = false"
> 
> # For real-time indices, let's make sure we're using the appropriate tenant.
> scope { Apartment::Tenant.switch! tenant.db; User }
> end
> end if Tenant.table_exists?
> 
> # Switching back to the original tenant - this is useful in the development
> # environment, as indices (and thus, this file) can be reloaded, and we don't
> # want to always leave our app on our last tenant.
> Apartment::Tenant.switch! current_tenant
> 
> Pat Allan於 2015年5月13日星期三 UTC+8下午6時59分53秒寫道:
> It looks like you have more than one index defined - you’ve shared your Topic 
> index, but what about the one for Conversation? And any others?
> 
> — 
> Pat
> 
> On 13 May 2015, at 8:51 pm, Frank H <[email protected]> wrote:
> 
> btw~ (=゚ω゚)ノ
> 
> indexer
> {
> }
> 
> searchd
> {
>   listen = 127.0.0.1:9312:mysql41
>   log = /home/frank/Dropbox/projects/testing/log/development.searchd.log
>   query_log = 
> /home/frank/Dropbox/projects/testing/log/development.searchd.query.log
>   pid_file = /home/frank/Dropbox/projects/testing/log/development.sphinx.pid
>   workers = threads
>   binlog_path = /home/frank/Dropbox/projects/testing/log
> }
> 
> index conversation_tenant1_core
> {
>   type = rt
>   path = 
> /home/frank/Dropbox/projects/testing/db/sphinx/development/conversation_tenant1_core
>   docinfo = extern
>   charset_type = utf-8
>   charset_table = 0..9, U+27,etc
>   min_prefix_len = 1
>   enable_star = 1
>   ngram_len = 1
>   ngram_chars = U+3400, etc
>   rt_field = sphinx_internal_class_name
>   rt_field = post_content
>   rt_field = topic_name
>   rt_field = title
>   rt_attr_uint = sphinx_deleted
>   rt_attr_uint = network_id
>   rt_attr_uint = user_id
>   rt_attr_uint = current_user_id
>   rt_attr_uint = group_id
>   rt_attr_bigint = sphinx_internal_id
>   rt_attr_timestamp = created_at
>   rt_attr_timestamp = updated_at
>   rt_attr_string = sphinx_internal_class
> }
> 
> 
> Frank H於 2015年5月13日星期三 UTC+8下午6時41分51秒寫道:
> sure~ (btw there was a date format i had no choice to map it into a string xD)
> lots of gratitude~ ◑ω◐ 
> 
> current_tenant = Apartment::Tenant.current
> 
> # Each Tenant instance is tied to a tenant in this example.
> Tenant.find_each do |tenant|
> # Switch to the appropriate Apartment tenant.
> Apartment::Tenant.switch! tenant.db
> 
> #ThinkingSphinx::Index.define :topic, :with => :active_record, :delta => true 
> do
> ThinkingSphinx::Index.define(
> :topic,
> # with: :active_record,
> # delta: true,
> name: "topic_#{tenant.db}",
> offset_as: "topic_#{tenant.db}".to_sym,
> with: :real_time )do
> 
> # fields
> indexes name #, sortable: true
> 
> # attributes
> has network_id, :type => :integer
> has created_at, updated_at, :type => :timestamp
> 
> # For real-time indices, let's make sure we're using the appropriate tenant.
> scope { Apartment::Tenant.switch! tenant.db; Topic }
> end
> end if Tenant.table_exists?
> 
> # Switching back to the original tenant - this is useful in the development
> # environment, as indices (and thus, this file) can be reloaded, and we don't
> # want to always leave our app on our last tenant.
> Apartment::Tenant.switch! current_tenant
> 
> Pat Allan於 2015年5月13日星期三 UTC+8下午6時33分46秒寫道:
> Can you share your current index definition?
> 
> — 
> Pat
> 
> On 13 May 2015, at 8:30 pm, Frank H <[email protected]> wrote:
> 
> btw i've changed the strings into correct type~ (integer and timestamp) 
> (=^ェ^=)
> 
> Frank H於 2015年5月13日星期三 UTC+8下午6時29分05秒寫道:
> dear Pat&All~ btw
> i'm almost there but here comes another error~
> 
> TypeError: can't convert ThinkingSphinx::ActiveRecord::Column to String 
> (ThinkingSphinx::ActiveRecord::Column#to_str gives 
> ThinkingSphinx::ActiveRecord::Column) 
> (line 5)
> 
> and attachment is where my error came from~ pls help~ very appreciate~~ 
> (*≧∇≦*)
> 
> Pat Allan於 2015年5月12日星期二 UTC+8下午4時26分14秒寫道:
> Have you got callbacks in the models you’re indexing, to ensure the data is 
> being saved to Sphinx?
> 
> And have you run `rake ts:regenerate` to get all the indices set up and 
> existing records indexed?
> 
> — 
> Pat
> 
> On 12 May 2015, at 6:01 pm, Frank H <[email protected]> wrote:
> 
> excuse me~ 
> i figured out select from index name is formal~
> it's Sphinx Query not MySQL query xD
> but now here comes another issue~
> i found that i'm indexing nothing~ always 0 result~~
> 
> Frank H於 2015年5月11日星期一 UTC+8下午7時49分44秒寫道:
> dear Pat&All how can i define a index whose name is different from it's table 
> name by real_time~ appreciate~ ◑ω◐
> i've worked hard to approach this:
> http://www.rubydoc.info/github/pat/thinking-sphinx/ThinkingSphinx/Index#define-class_method
> i'm using the apartment to do the multitenancy~ my code is as following~ 
> sorry for disturbing~ have a good one~ d(-_^)
> 
> current_tenant = Apartment::Tenant.current
> 
> # Each Tenant instance is tied to a tenant in this example.
> 
> Tenant.find_each do |tenant|
> 
>   # Switch to the appropriate Apartment tenant.
> 
>   Apartment::Tenant.switch tenant.db
> 
>  
>   ThinkingSphinx::Index.define(
> 
>   :topic,
> 
>   #  with: :active_record,
> 
>   # delta: true,
> 
>   name: "topic_#{tenant.db}",
> 
>   table_name: "topic",(wanted)  
> 
>   offset_as: "topic_#{tenant.db}".to_sym,
> 
>   with: :real_time )do
> 
>  
>     # fields
> 
>     indexes name, sortable: true
> 
>  
>     # attributes
> 
>     has network_id,created_at, updated_at, :type => :string
> 
>  
>     # For real-time indices, let's make sure we're using the appropriate 
> tenant.
> 
>     scope { Apartment::Tenant.switch tenant.db; Topic }
> 
>   end
> 
> end if Tenant.table_exists?
> 
>  
> # Switching back to the original tenant - this is useful in the development
> 
> # environment, as indices (and thus, this file) can be reloaded, and we don't
> 
> # want to always leave our app on our last tenant.
> 
> Apartment::Tenant.switch current_tenant
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/thinking-sphinx.
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> You received this message because
> ...
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/thinking-sphinx.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/d/optout.

Reply via email to