Rails elasticsearch-persistence 怎么改变默认配置?

ghn645568344 · 2016年12月05日 · 最后由 kooglezhang 回复于 2016年12月06日 · 2258 次阅读

RT,想在单独运行一个 ruby 文件 用 elasticsearch-persistence 执行数据增删改查 代码如下

# encoding: utf-8
require 'active_record'
require 'will_paginate'
require 'mechanize'
require 'elasticsearch'
# require 'elasticsearch/rails'
# require 'elasticsearch/model'
require 'elasticsearch/persistence'
require 'elasticsearch/persistence/model'
ActiveRecord::Base.establish_connection(
。。。。。。。

class User < ActiveRecord::Base #newbaichebao_items
end

class EsUser
    include Elasticsearch::Persistence::Model

  # include Elasticsearch::Persistence::Repository
  # def initialize(options={})
  #   index  options[:index] || 'rails'
  #   client Elasticsearch::Client.new url: options[:url], log: options[:log]
  #
  # end
    attribute :user_id,  String
    attribute :phone,  String
    attribute :name,  String
    attribute :registration_time,  String
    attribute :registered_location ,  String
    attribute :channel,  String
    attribute :fraction,  Integer
    attribute :number_of_terms ,  Integer
    attribute :last_login_time ,  String
end
def main
  repository = Elasticsearch::Persistence::Repository.new do
    # Configure the Elasticsearch client
    client Elasticsearch::Client.new url: '10.797.26.99:9200', log: true
    index :rails
    # type  :my_note
  end
    # str = Mechanize.new
    puts a = User.count / 1000 + 1
    # puts EsUser.find()
  # for i in 1..a
    #   puts ">>>>>>>>>>>>>>>>>#{i}"
    #   users = User.paginate(:page => i, :per_page => 1000)
  User.find_each do |user|
      puts user.name
      fen = LicenseLogging.where(:user_id=>user.id.to_s,:name=>"credit_indicator").blank? ? 0 : LicenseLogging.where(:user_id=>user.id.to_s,:name=>"credit_indicator").last.remarks
      es = EsUser.new
      es.user_id = "#{ids}"
      es.phone = user.name
      es.registration_time = user.created_at.to_s
      es.registered_location = user.reg_location.to_s
      es.channel = user.port_type
      es.fraction = fen.to_i
      es.number_of_terms = num
      es.last_login_time = user.updated_at.to_s
      p es
      es.save
  end
    # end
end

main

但是始终改不了默认的本地连接。。。😭

/home/yangmi/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:879:in `initialize': Network is unreachable - connect(2) for "localhost" port 9200 (Faraday::ConnectionFailed)

请教大家了🙏

你这个是是要执行定时任务 rake 么 你的网络连接有问题呀,你确定本地任务启动了吗

#1 楼 @kooglezhang 不是要链接本地的,要链接另一台

需要 登录 后方可回复, 如果你还没有账号请 注册新账号