思考Sphinx ts:configure在升级后在sphinx.conf中生成无效的SQL

时间:2014-02-11 10:26:34

标签: sql ruby-on-rails thinking-sphinx

我正在将Rails应用从3.2.6升级到3.2.16。该应用程序使用Thinking Sphinx,在我们的公司模型中,我们曾经使用以下define_index块

define_index do
    indexes :reg_no, :sortable => true
    indexes :org_no, :sortable => true
    indexes :name, :sortable => true
    indexes :classification_code
    indexes address(:address1), :as => :address1
    indexes address(:address2), :as => :address2
    indexes address(:address3), :as => :address3
    indexes address(:city), :as => :city
    indexes address.postal_area(:code), :as => :postal_code
    indexes address.postal_area(:name), :as => :postal_area_name
    indexes address.country(:name), :as => :country_name
    indexes address.country(:iso2), :as => :country_iso2
    indexes address.country(:iso3), :as => :country_iso3

    has end_date
    set_property :delta => :delayed
 end

生成了有效的development.sphinx.conf。

我已将索引定义提取到indices / company_index.rb中,如升级文档中所述:

ThinkingSphinx::Index.define :company, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
    indexes :reg_no, :sortable => true
    indexes :org_no, :sortable => true
    indexes :name, :sortable => true
    indexes :classification_code
    indexes address(:address1), :as => :address1
    indexes address(:address2), :as => :address2
    indexes address(:address3), :as => :address3
    indexes address(:city), :as => :city


    indexes address.postal_area(:code), :as => :postal_code
    indexes address.postal_area(:name), :as => :postal_area_name
    indexes address.country(:name), :as => :country_name
    indexes address.country(:iso2), :as => :country_iso2
    indexes address.country(:iso3), :as => :country_iso3
    indexes address.country(:iso3), :as => :country_iso3

    has end_date
    set_property :delta => :delayed 
end

在升级之前,应用程序使用了Thinking Sphinx版本2.0.13和Sphinx 0.99,现在更新为使用3.1.0和2.1.4版本。

ts:configure生成development.sphinx.conf:除此之外还包含以下行:

sql_query = SELECT SQL_NO_CACHE
        `companies`.`id` * 3 + 0 AS `id`,
        `companies`.`reg_no` AS `reg_no`,
        `companies`.`org_no` AS `org_no`,
        `companies`.`name` AS `name`,
        `companies`.`classification_code` AS `classification_code`,
        addresses.`address1` AS `address1`,
        addresses.`address2` AS `address2`,
        addresses.`address3` AS `address3`, 
        addresses.`city` AS `city`,
        AS `postal_code`,  AS `postal_area_name`,  AS `country_name`,  AS `country_iso2`,  AS `country_iso3`,  AS `country_iso3`, `companies`.`id` AS `sphinx_internal_id`, 'Company' AS `sphinx_internal_class`, 0 AS `sphinx_deleted`,
        UNIX_TIMESTAMP(`companies`.`end_date`) AS `end_date`
    FROM `companies`
    LEFT OUTER JOIN `addresses` ON `addresses`.`id` = `companies`.`preferred_address_id`
    WHERE (`companies`.`id` BETWEEN $start AND $end)
    GROUP BY
        `companies`.`id`,`companies`.`reg_no`, `companies`.`org_no`, `companies`.`name`, `companies`.`classification_code`,
        addresses.`address1`, addresses.`address2`, addresses.`address3`,
        addresses.`city`, , , , , , , `companies`.`id`, `companies`.`end_date`
    ORDER BY NULL

似乎更深层次的关联会生成无效的SQL。我已经阅读了文档但我找不到任何可以暗示我做错的事情。

1 个答案:

答案 0 :(得分:0)

我不确定这是否是原因,但您可以尝试更改此内容:

indexes address.postal_area(:code), :as => :postal_code
indexes address.postal_area(:name), :as => :postal_area_name
indexes address.country(:name), :as => :country_name
indexes address.country(:iso2), :as => :country_iso2
indexes address.country(:iso3), :as => :country_iso3
indexes address.country(:iso3), :as => :country_iso3

对此:

indexes address.postal_area.code, :as => :postal_code
indexes address.postal_area.name, :as => :postal_area_name
indexes address.country.name, :as => :country_name
indexes address.country.iso2, :as => :country_iso2
indexes address.country.iso3, :as => :country_iso3
indexes address.country.iso3, :as => :country_iso3

除此之外,如果生成的SQL中缺少列,则通常意味着关联或列名称错误。

另外:通过set_property设置增量无效 - 但您也可以在索引顶部正确设置。这与你所看到的问题无关。