1

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

文章型号:

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
4

2 回答 2

3

是的,这是可能的——但不是我在问题中所做的那样。这是轮胎宝石所有者Karmi提出的答案

有关完整答案,请参阅

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
于 2012-10-17T02:51:49.237 回答
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
于 2012-10-16T12:04:16.103 回答