前提
ruby 3.1.2 rails 7.0.3 elasticsearch 7.17.4
railsからElasticsearchを利用したく、Elasticsearch本体とgemをインストールしました。
実現したいこと
rails コンソールからアプリ側に入れたデータをElasticsearchから検索、取得したい。
発生している問題・エラーメッセージ
Elasticsearchで検索を行うためモデル.import()しましたが0件となりました。
どうすればElasticsearchにデータが入れられるのでしょうか?
何か確認すべき所などあれば教えて下さい。
irb(main):017:0> Article.import Article Load (0.1ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ? [["LIMIT", 1000]] => 0
ですがこの通りデータはアプリ側に入ってます。
irb(main):018:0> Article.all Article Load (0.2ms) SELECT "articles".* FROM "articles" => [#<Article:0x000000010822f020 id: 1, title: "Quick brown fox", body: nil, created_at: Wed, 25 Jan 2023 07:44:40.563667000 UTC +00:00, updated_at: Wed, 25 Jan 2023 07:44:40.563667000 UTC +00:00>, #<Article:0x000000010822ef58 id: 2, title: "Fast black dogs", body: nil, created_at: Wed, 25 Jan 2023 07:44:48.009504000 UTC +00:00, updated_at: Wed, 25 Jan 2023 07:44:48.009504000 UTC +00:00>, #<Article:0x000000010822ee90 id: 3, title: "Swift green frogs", body: nil, created_at: Wed, 25 Jan 2023 07:44:52.745768000 UTC +00:00, updated_at: Wed, 25 Jan 2023 07:44:52.745768000 UTC +00:00>, #<Article:0x000000010822edc8 id: 4, title: "hoge piyo", body: nil, created_at: Thu, 26 Jan 2023 02:11:09.350065000 UTC +00:00, updated_at: Thu, 26 Jan 2023 02:11:09.350065000 UTC +00:00>] irb(main):019:0>
elasctic側にimport出来ていないために検索結果が取得出来ないのかなと予想してます。
irb(main):019:0> Article.__elasticsearch__.search 'hoge' => #<Elasticsearch::Model::Response::Response:0x00000001084fcca8 @klass= [PROXY] Article(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime), @search= #<Elasticsearch::Model::Searching::SearchRequest:0x00000001084fce38 @definition={:index=>"articles", :type=>nil, :q=>"hoge"}, @klass= [PROXY] Article(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime), @options={}>> irb(main):020:0> res = Article.search 'hoge' test => nil irb(main):021:0>
該当のソースコード
/Gemfile
ruby
1gem 'elasticsearch-model' 2gem 'elasticsearch-rails'
/app/models/article.rb
ruby
1require 'elasticsearch/model' 2 3class Article < ActiveRecord::Base 4 include Elasticsearch::Model 5 include Searchable 6end 7
/app/models/concerns/searchable.rb
ruby
1module Searchable 2 extend ActiveSupport::Concern 3 4 included do 5 include Elasticsearch::Model 6 7 mapping do 8 # ... 9 end 10 11 def self.search(query) 12 # ... 13 14 puts "test" 15 end 16 end 17end
試したこと
elastic searchの起動確認
% curl http://localhost:9200 { "name" : "peronoMac-mini.local", "cluster_name" : "elasticsearch_pero", "cluster_uuid" : "NiCENitKx3dWw", "version" : { "number" : "7.17.4", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411", "build_date" : "2022-05-18T18:04:20.964345128Z", "build_snapshot" : false, "lucene_version" : "8.11.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
railsにデータがあること
irb(main):018:0> Article.all Article Load (0.2ms) SELECT "articles".* FROM "articles" => [#<Article:0x000000010822f020 id: 1, title: "Quick brown fox", body: nil, created_at: Wed, 25 Jan 2023 07:44:40.563667000 UTC +00:00, updated_at: Wed, 25 Jan 2023 07:44:40.563667000 UTC +00:00>, #<Article:0x000000010822ef58 id: 2, title: "Fast black dogs", body: nil, created_at: Wed, 25 Jan 2023 07:44:48.009504000 UTC +00:00, updated_at: Wed, 25 Jan 2023 07:44:48.009504000 UTC +00:00>, #<Article:0x000000010822ee90 id: 3, title: "Swift green frogs", body: nil, created_at: Wed, 25 Jan 2023 07:44:52.745768000 UTC +00:00, updated_at: Wed, 25 Jan 2023 07:44:52.745768000 UTC +00:00>, #<Article:0x000000010822edc8 id: 4, title: "hoge piyo", body: nil, created_at: Thu, 26 Jan 2023 02:11:09.350065000 UTC +00:00, updated_at: Thu, 26 Jan 2023 02:11:09.350065000 UTC +00:00>] irb(main):019:0>
追記
elastic側を一つずつ確認中
今回のことに関係あるか分かりませんがステータスがyellowでした
curl http://localhost:9200/_cluster/health?pretty { "cluster_name" : "elasticsearch_pero", "status" : "yellow", "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 4, "active_shards" : 4, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 1, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 80.0 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。