使用轮胎的模块的映射和设置

时间:2012-10-16 06:44:22

标签: elasticsearch ruby-on-rails-2 tire

我想从另一个模块添加特定模型的设置和映射。那可能吗?如果是,请帮助我如何做到这一点。我试过这个:

文章模型:

class Article < ActiveRecord::Base
  include SearchArticle
end

新模块:

module SearchArticle

  include Tire::Model::Search
  settings :analyzer => {
            :custom_analyzer => {
              'tokenizer' => 'uax_url_email'
            }
    } do
    mapping do
      indexes :name, :boost => 10
      indexes :email, :analyzer => 'custom_analyzer', :boost => 10
    end
  end

end

但索引没有被创建。我收到以下错误。

[2012-10-16 11:42:38,023][WARN ][cluster.metadata         ] [Inza] [search_article] failed to create
org.elasticsearch.index.mapper.MapperParsingException: mapping [search_article]
at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:262)
at org.elasticsearch.cluster.service.InternalClusterService$2.run(InternalClusterService.java:208)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

Caused by: org.elasticsearch.index.mapper.MapperParsingException: Analyzer [custom_analyzer] not found for field [email]
at org.elasticsearch.index.mapper.core.TypeParsers.parseField(TypeParsers.java:76)
at org.elasticsearch.index.mapper.core.StringFieldMapper$TypeParser.parse(StringFieldMapper.java:124)
at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parseProperties(ObjectMapper.java:261)
at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parse(ObjectMapper.java:217)
at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:161)
at org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:364)
at org.elasticsearch.index.mapper.MapperService.add(MapperService.java:186)
at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:259)
... 4 more

2 个答案:

答案 0 :(得分:3)

是的,这是可能的 - 但不是我在问题中的方式。以下是轮胎宝石所有者Karmi

建议的答案

如需完整答案,请参阅this

module Searchable

  def self.included(base)

    p "Included in #{base}"

    base.class_eval do
      include Tire::Model::Search

      tire do
        mapping do
          indexes :title,   type: 'string', analyzer: 'snowball'
        end
      end
    end
  end
end

class Article < ActiveRecord::Base
  include Searchable
end

答案 1 :(得分:0)

module SearchArticle
  include Tire::Model::Search
  include ActiveSupport::Concern

  included do
    settings :analyzer => {
                :custom_analyzer => {
                  'tokenizer' => 'uax_url_email'
                }
        } do
        mapping do
          indexes :name, :boost => 10
          indexes :email, :analyzer => 'custom_analyzer', :boost => 10
        end
     end
  end
end