如何创建父子映射?

时间:2013-12-17 14:14:39

标签: ruby-on-rails elasticsearch

我从Tire迁移到Flex 基本搜索和索引同步工作 我认为模型中的flex.parent线会自动创建_parent映射,但它会崩溃 我无法找到任何父/子演示项目。

Flex.yml:

settings:
  number_of_shards: 5
  number_of_replicas: 1
  # analysis:
    # analyzer:
    # tokenizer:
  mappings:
    userprofile:
      startdatef:
        type: 'date'
        format: 'dateOptionalTime'
        fields:
          index: 'not_analyzed'
          untouched:
            type: 'date'
            index: 'not_analyzed'
    orgunit:
      org_name:
        type: 'string'
        index: 'analyzed'
        search_analyzer: orgunit_name_search
        index_analyzer: orgunit_name_index
      untouched:
        type: 'string'
        index: 'not_analyzed'

父模型:

class Userprofile < ActiveRecord::Base
  include Flex::ModelIndexer
  include Flex::Model
  flex.sync self
  has_many :assignments,
    -> { order(startdate: :desc) }, dependent: :restrict_with_exception

  module Search
    include Flex::Scopes
    flex.context = Userprofile
    scope :alla, query([])
  end

  # rubocop:disable all
  def flex_source
    {
      id: id,
      fullname: fullname,
      firstname: firstname,
      lastname: lastname,
      pnr: pnr,
      gender: gender,
      asscount: asscount,
      created_at: created_at,
      updated_at: updated_at,
      user_id: user_id,
      creator_id: creator_id,
    }
  end
  # rubocop:enable all
end

儿童模特:

class Assignment < ActiveRecord::Base
  include Flex::ModelIndexer
  include Flex::Model
  flex.parent :userprofile, 'userprofile' => 'assignment' # This makes indexing break
  flex.sync self, :userprofile
  belongs_to :userprofile, counter_cache: true, touch: true

  module Search
    include Flex::Scopes
    flex.context = Assignment
    scope :alla, query([])
  end

  def flex_source
    {
      # _parent_id: userprofile_id,
      userprofile_id: userprofile_id,
      created_at: created_at,
      updated_at: updated_at
    }
  end
end

rake flex:import


Model Userprofile:批量处理37个文档: 处理...:100%||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||时间:0:00:01 已处理37.成功37.跳过0.失败0。


模型分配:批量处理36个文档: 耙子流产!:0%| | ETA: - : - : - activerecord-4.0.2 / lib / active_record / relation / batches.rb:75:in find_in_batches' 400: {"error":"ElasticSearchIllegalArgumentException[Can't specify parent if no parent field has been configured]","status":400} flex-1.0.6/lib/flex/template.rb:54:in do_render' ... 任务:TOP =&gt;挠性:进口

1 个答案:

答案 0 :(得分:5)

在您的映射中,您必须指定&#34; parent&#34;领域。只有那时ES才能链接&#34; _parent&#34;您要索引到相应索引类型的字段。尝试将其作为模板(替换适合您索引的变量)。它只是将信息添加到映射中,而不是删除现有的信息:

curl -XPUT 'http://localhost:9200/$yourIndex/$yourChildTypeName/_mapping' -d '
{
      "$yourChildTypeName" : {
         "_parent" : {
            "type" : "$yourParentTypeName"
         }
} '

然后再次尝试索引。