From 09f0341883bdc9792b6006c4f476a1d005fac0b5 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 15 Jun 2022 18:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E:=20es=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 +++ Gemfile.lock | 13 +++++++++++++ app/models/project.rb | 3 +-- config/configuration.yml.example | 5 +++++ config/initializers/elasticsearch_model.rb | 8 ++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 config/initializers/elasticsearch_model.rb diff --git a/Gemfile b/Gemfile index 713eb860..2dfdc64b 100644 --- a/Gemfile +++ b/Gemfile @@ -128,3 +128,6 @@ gem 'harmonious_dictionary', '~> 0.0.1' gem 'parallel', '~> 1.19', '>= 1.19.1' gem 'letter_avatar' + +gem 'elasticsearch-model', github: 'elastic/elasticsearch-rails', branch: 'main' +gem 'elasticsearch-rails', github: 'elastic/elasticsearch-rails', branch: 'main' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index b1dc2cca..3fd2e9e5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,14 @@ +GIT + remote: https://github.com/elastic/elasticsearch-rails.git + revision: b66fe71cc4b9b10069bf6d39dbbfcc0b117a08a5 + branch: main + specs: + elasticsearch-model (7.2.1) + activesupport (> 3) + elasticsearch (~> 7) + hashie + elasticsearch-rails (7.2.1) + GEM remote: https://gems.ruby-china.com/ specs: @@ -450,6 +461,8 @@ DEPENDENCIES chromedriver-helper deep_cloneable (~> 3.0.0) diffy + elasticsearch-model! + elasticsearch-rails! enumerize faraday (~> 0.15.4) font-awesome-sass (= 4.7.0) diff --git a/app/models/project.rb b/app/models/project.rb index 9c77685f..9b75a94f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -79,9 +79,8 @@ # - - class Project < ApplicationRecord + include Elasticsearch::Model include Matchable include Publicable include Watchable diff --git a/config/configuration.yml.example b/config/configuration.yml.example index 61e66635..dcc1c1bf 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -67,6 +67,11 @@ default: &default domain: '' base_url: '' + es: + url: 'http://localhost:9200' + user: 'elastic' + password: '123456' + production: <<: *default # 中间层地址 diff --git a/config/initializers/elasticsearch_model.rb b/config/initializers/elasticsearch_model.rb new file mode 100644 index 00000000..0e822cfc --- /dev/null +++ b/config/initializers/elasticsearch_model.rb @@ -0,0 +1,8 @@ +require 'elasticsearch/model' + +es_config = Rails.application.config_for(:es) +host_url = es_config["url"] || 'http://localhost:9200/' +user = es_config["user"] || '' +password = es_config["password"] || '' + +Elasticsearch::Model.client = Elasticsearch::Client.new user: user, password: password, host: host_url \ No newline at end of file