ruby-on-rails - 如何创建父子映射?

标签 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

模型用户资料:批量处理37个文档(共1000个):
正在处理...:100%|||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||| ||||||||时间:0:00:01
处理37。成功37。跳过0。失败0。

模型分配:批量处理36个文档,共1000个:
rake 中止!:0%| |预计到达时间:-:-:-
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 => flex:import

最佳答案

在映射中,您必须指定“父”字段。只有这样,ES才能将您要索引的“_parent”字段中的ID链接到相应的索引类型。尝试将其作为模板(用适合您的索引的变量代替)。它只是将信息添加到映射中,而不删除现有信息:

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

然后尝试再次索引。

关于ruby-on-rails - 如何创建父子映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636450/

相关文章:

ruby-on-rails - Mongoid,查找名称包含字符串的模型

ruby-on-rails - 使用 Rails 4 对象将 PSQL 查询转换为工作

ruby-on-rails - 如何使用地理编码器计算两点之间的公里距离

java - 从 Java 应用程序登录到 ELK,无需解析日志

ruby-on-rails - Rails 3 级深度嵌套资源

ruby-on-rails - has_and_belongs_to_many 验证,以便用户不能多次申请车间

elasticsearch - Elasticsearch 插件可以有多个 Module 功能吗

elasticsearch - 如何在 Elasticsearch 中指定的所有字段中搜索所有字段?

javascript - Elasticsearch 数据表

elasticsearch - 带有字段用户名的elasticsearch查询:停止工作