0

我有一个 STI 模型,我想用 ElasticSearch 和 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 来创建索引?还是我设置不正确

4

1 回答 1

0

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

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

-弗拉德

于 2013-06-14T21:59:51.863 回答