Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用elasticsearch rails为多对多关联设置映射_Ruby On Rails_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Many To Many_Denormalization_Mappings - Fatal编程技术网 elasticsearch,many-to-many,denormalization,mappings,Ruby On Rails,elasticsearch,Many To Many,Denormalization,Mappings" /> elasticsearch,many-to-many,denormalization,mappings,Ruby On Rails,elasticsearch,Many To Many,Denormalization,Mappings" />

Ruby on rails 如何使用elasticsearch rails为多对多关联设置映射

Ruby on rails 如何使用elasticsearch rails为多对多关联设置映射,ruby-on-rails,elasticsearch,many-to-many,denormalization,mappings,Ruby On Rails,elasticsearch,Many To Many,Denormalization,Mappings,根据《华尔街日报》的报道,如果我有一家拥有多个分支机构的公司,我想让员工与该分支机构合作,我会这样做 PUT /company { "mappings": { "branch": {}, "employee"{ "_parent": { "type": "branch" } } } } 用户使用RubyonRail也可以实现同样的功能,如下所示 cla

根据《华尔街日报》的报道,如果我有一家拥有多个分支机构的公司,我想让员工与该分支机构合作,我会这样做

PUT /company
{
    "mappings": {
        "branch": {},
        "employee"{
            "_parent": {
                "type": "branch"
            }
        }
    }
}
用户使用RubyonRail也可以实现同样的功能,如下所示

class Branch < ActiveRecord::Base
  include Elasticsearch::Model

  has_many :employees, dependent: :destroy

  index_name 'company'

  mapping do
    indexes :name
  end

  after_commit lambda { __elasticsearch__.index_document  },  on: :create
  after_commit lambda { __elasticsearch__.update_document },  on: :update
  after_commit lambda { __elasticsearch__.delete_document },  on: :destroy
end

class Employee < ActiveRecord::Base
  include Elasticsearch::Model

  belongs_to :branch

  index_name 'company'

  mapping _parent: { type: 'branch' }, _routing: { required: true } do
    indexes :name
    indexes :email
  end

  after_commit lambda { __elasticsearch__.index_document(parent: branch_id)  },  on: :create
  after_commit lambda { __elasticsearch__.update_document(parent: branch_id) },  on: :update
  after_commit lambda { __elasticsearch__.delete_document(parent: branch_id) },  on: :destroy
end

module Searchable
  INDEX_NAME = 'company'

  def create_index!(options={})
    client = Branch.__elasticsearch__.client
    client.indices.delete index: INDEX_NAME rescue nil if options[:force]

    settings = Branch.settings.to_hash.merge Employee.settings.to_hash
    mappings = Branch.mappings.to_hash.merge Employee.mappings.to_hash

    client.indices.create index: INDEX_NAME,
                          body: {
                            settings: settings.to_hash,
                            mappings: mappings.to_hash }
  end

  extend self
end
这很有效

使用者

PUT /my_index/_mapping/blogpost
{
  "blogpost": {
    "properties": {
      "body": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      },
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      },
      "user": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            }
          },
          "id": {
            "type": "long"
          }
        }
      }
    }
  },
  "user": {
    "properties": {
      "dob": {
        "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis",
        "type": "date"
      },
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      },
      "email": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      }
    }
  }
}
class User < ApplicationRecord
  include Elasticsearch::Model
  has_and_belongs_to_many :blogposts, touch: true

  index_name 'blog'

  mapping do
    indexes :name
    indexes :email
  end

  after_commit lambda { __elasticsearch__.index_document  },  on: :create
  after_touch  lambda { __elasticsearch__.index_document  },  on: :touch
  after_commit lambda { __elasticsearch__.update_document },  on: :update
  after_commit lambda { __elasticsearch__.delete_document },  on: :destroy
end
class Blogpost < ApplicationRecord
  include Elasticsearch::Model
  has_and_belongs_to_many :user, touch: true

  index_name 'blog'

  mapping do
    indexes :title
    indexes :body

    indexes :user do
      indexes :id, type: 'long'
      indexes :name, type: 'string' do
        indexes :raw, type: 'keyword', index: 'not_analyzed'
      end
    end
  end

  def as_indexed_json(options={})
    hash = self.as_json()
    hash['user.id'] = self.user.first.id
    hash['user.name'] = self.user.first.name
    hash
  end


  after_commit lambda { __elasticsearch__.index_document  },  on: :create
  after_touch  lambda { __elasticsearch__.index_document  },  on: :touch
  after_commit lambda { __elasticsearch__.update_document },  on: :update
  after_commit lambda { __elasticsearch__.delete_document },  on: :destroy
end
  def create_index!(options={})
    client = User.__elasticsearch__.client
    client.indices.delete index: INDEX_NAME rescue nil if options[:force]

    settings = User.settings.to_hash.merge Blogpost.settings.to_hash
    mappings = User.mappings.to_hash.merge Blogpost.mappings.to_hash

    client.indices.create index: INDEX_NAME,
                          body: {
                              settings: settings.to_hash,
                              mappings: mappings.to_hash }
  end

  def setup
    Searchable.create_index! force: true

    10.times do
      n = Faker::Name.name
      u = User.create name: n,
                      email: Faker::Internet.free_email(n.split(' ').last)

      rand(1..10).times do
        s = Faker::Lorem.sentence
        u.blogposts.create title: s.split(' ').first, body: s
      end
    end

    User.__elasticsearch__.refresh_index!
  end

  extend self
end