带有Tire的ElasticSearch不包括带有STI模型的自定义分析器

时间:2013-03-15 04:58:17

标签: ruby-on-rails elasticsearch tire sti

我有一个STI模型,我希望可以使用ElasticSearch和Tire进行搜索。我遇到的问题是当Tire创建映射时,似乎忽略了我的第二个模型的自定义分析器。以下是我的模型示例。

class Account < ActiveRecord::Base
  attr_accessible :name, :type

  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire.settings :analysis => {
          :analyzer => {
          "custom_search_analyzer" => {
              "tokenizer" => "keyword",
              "filter" => "lowercase"
          },
          "custom_index_analyzer" => {
              "tokenizer" => "keyword",
              "filter" => ["lowercase","substring"]
          }
      },
      :filter => {
          :substring => {
              "type" => "nGram",
              "min_gram" => 1,
              "max_gram" => 20
          }
      }
  } do
    mapping do
      indexes :id, :type => 'integer', :include_in_all => false
      indexes :name, :type => 'string', :search_analyzer => :custom_search_analyzer,     :index_analyzer=>:custom_index_analyzer
    end
  end

  def to_indexed_json
    hash = {}
    hash[:id] = id
    hash[:name] = name
    hash.to_json
  end
end

class StandardAccount < Account
  tire.index_name 'accounts'
end

class SuperAccount < Account
  tire.index_name 'accounts'
end

当我通过轮胎创建索引时,无论是使用rake任务还是通过创建模型,它都会创建映射,但对于继承的模型,它不会将自定义分析器应用于它们。如果我使用

查看映射
curl -XGET 'http://127.0.0.1:9200/accounts/_mapping?pretty=1'  

我明白了:

{
  "accounts" : {
    "account" : {
      "properties" : {
        "id" : {
          "type" : "integer",
          "include_in_all" : false
        },
        "name" : {
          "type" : "string",
          "index_analyzer" : "custom_index_analyzer",
          "search_analyzer" : "custom_search_analyzer"
        }
      }
    },
    "standard_account" : { 
      "properties" : {
        "id" : {
          "type" : "long"
        }
        "name" : {
          "type" : "string"
        }
      }
    },
    "super_account" : {
      "properties" : {
        "id" : {
          "type" : "long"
        }
        "name" : {
          "type" : "string"
        }
      }
    }
  }
}

即使我将映射声明移动到继承的类,它似乎只是创建的第一个模型来获取额外的选项。我可以通过ElasticSearch手动创建索引但是想知道是否有办法用Tire做到这一点?或者我的设置不正确

1 个答案:

答案 0 :(得分:0)

您可能已经找到了答案,但我认为这可能对您或其他有同样问题的人有所帮助:

https://stackoverflow.com/a/13660263/1401343

-Vlad