Search code examples
ruby-on-railsrubythinking-sphinx

how to add chinese_directory config to thinking-sphinx configuration


I am use thinking-sphinx 3.1.1, i have a modified sphinx version support chinese character search. it has a special config item must put in configuration chinese_dictionary: "/usr/local/sphinx-for-chinese/etc/xdict"

i have add "chinese_dictionay" to thinking_sphinx.yml this is my config:

thinking_sphinx.yml:

development: chinese_dictionary: "/usr/local/sphinx-for-chinese/etc/xdict" thread_stack: 1M

when i generate the configuration only thread_stack has in the developments.sphinx.conf, the chinese_directory has not.

I have search a argly solution, it must edit by hand. first generate the config, rake ts:configure , put the chinese_dictionary to developments.sphinx.conf by hand, then rake ts:index INDEX_ONLY=true, i do not like this way.

I think it can modify the thinking-sphinx gems by add a file to rails initializers directory. i have look the thking-sphinx gem, but i can not understand how to modify

import: the chinese_dictionary: "/usr/local/sphinx-for-chinese/etc/xdict" must under the index, this is right config:

index article_core
{
    type = plain
    path = ~Documents/Project/blog/db/sphinx/development/article_core
    docinfo = extern
    charset_type = utf-8
    source = article_core_0
    chinese_dictionary = /usr/local/sphinx-for-chinese/etc/xdict
}

index article
{
    type = distributed
    local = article_core
}

Solution

  • It's not particularly elegant, but this should do the trick (put it in an initializer):

    module ChineseDirectoryAccessor
      attr_accessor :chinese_dictionary
    end
    
    Riddle::Configuration::Index.include ChineseDirectoryAccessor
    Riddle::Configuration::RealtimeIndex.include ChineseDirectoryAccessor
    
    Riddle::Configuration::IndexSettings.module_eval do
      singleton_class.send :alias_method, :old_settings, :settings
    
      def self.settings
        old_settings + [:chinese_dictionary]
      end
    end
    

    It makes sure Riddle (which is used for Sphinx configuration) is aware of the new setting.