[英]How use Chewy as Elasticsearch in Rest Api project of Ruby on Rails?
I have problem with connect Chewy to my Rest Api app with Ruby on Rails.我在使用 Ruby on Rails 将 Chewy 连接到我的 Rest Api 应用程序时遇到问题。 I have simple Item and controller with base CRUD.我有简单的项目和带有基本 CRUD 的 controller。 In this example i add only create method I read a few tutorials but last I used https://www.rubydoc.info/github/toptal/chewy/master I wanted, when I add Item in my db by Api, this Item will be add to Chewy too.在这个例子中我只添加创建方法我读了一些教程但最后我使用了 https://www.rubydoc.info/github/toptal/chewy/master我想要的,当我通过 Api 在我的数据库中添加项目时,这个项目将也可以添加到 Chewy 中。 Speaking simpler, All changes in my db will be influence to Chewy简单来说,我数据库中的所有更改都会影响 Chewy
My code我的代码
app/chewy/items_index.rb app/chewy/items_index.rb
class ItemsIndex < Chewy::Index
settings analysis: {
analyzer: {
name: {
tokenizer: 'keyword',
filter: ['lowercase']
}
}
}
index_scope Item
field :name, analyzer: 'name'
field :description
field :price
field :count
field :created_at
field :updated_at
end
app/models/item.rb应用程序/模型/item.rb
class Item < ActiveRecord::Base
update_index('items#item') { self }
end
config/chewy.yml配置/耐嚼.yml
test:
host: 'localhost:9250'
prefix: 'test'
development:
host: 'localhost:9200'
config/initializers/chewy.rb配置/初始化程序/chewy.rb
Chewy.settings = {host: 'localhost:9200'}
app/controllers/items_controller.rb应用程序/控制器/items_controller.rb
class ItemsController < ApplicationController
def create
@item = Item.new(item_params)
if @item.save
render json: @item
else
render json: @item.errors, status: :unprocessable_entity
end
end
private
def item_params
params.require(:item).permit(:name, :description, :price, :count)
end
end
config/routes.rb配置/routes.rb
Rails.application.routes.draw do
post '/items', to: 'items#create'
end
My result When I add new Item by Api eg POstman, I add new Item in DB but Chewy says UndefinedIndex.我的结果 当我通过 Api 添加新项目时,例如 POstman,我在数据库中添加了新项目,但 Chewy 说 UndefinedIndex。 Can not find index name..... logs_after_add_item找不到索引名称..... logs_after_add_item
In Postman I got error: Internal Server 500 with message postman_response在 Postman 中,我收到错误:Internal Server 500 with message postman_response
So now, I dont have no idea why it doesn't work.所以现在,我不知道为什么它不起作用。 I thought, when I have this line "update_index('items#item') { self }" This will allow Chewy to get changes about eg adding new Item or update.我想,当我有这一行“update_index('items#item') { self }”时,这将允许 Chewy 获得关于添加新项目或更新的更改。 I read a few tutorial, and in simple configuration It was enought, when some person wants add some Object in DB and Chewy我读了一些教程,在简单的配置中就足够了,当有人想在 DB 和 Chewy 中添加一些 Object 时
Maybe I don't understand how it should work.也许我不明白它应该如何工作。 In tutoorials, they didn't have Rest APi or maybe not show how it should work在教程中,他们没有 Rest APi 或者可能没有显示它应该如何工作
I will be grateful for your help and hints I'm new to Ruby and Rails:-)我将非常感谢您的帮助和提示我是 Ruby 和 Rails 的新手:-)
Edit 1编辑 1
Ok.行。 I found my mistake.我发现了我的错误。 I put Chewy directory with item_index.rb in the wrong directory.我将带有 item_index.rb 的 Chewy 目录放在了错误的目录中。 I put in api not app.我输入的是 api 而不是应用程序。 But now I have problem with connection:但是现在我遇到了连接问题:
<Faraday::ConnectionFailed wrapped=<Errno::EADDRNOTAVAIL: Failed to open TCP connection to localhost:9200 (Cannot assign requested address - connect(2) for [::1]:9200)>>
My docker我的 docker
version: "3"
services:
db:
image: postgres
volumes:
- db:/var/lib/postgresql/data
ports:
- "5491:5432"
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
networks:
- appnet
elasticsearch:
image: elasticsearch:7.11.1
container_name: elastisearch
volumes:
- elasticsearch:/var/lib/elasticsearch/data
ports:
- 9200:9200
environment:
- discovery.type=single-node
ulimits:
memlock:
soft: -1
hard: -1
networks:
- appnet
api:
build: ./api
command: bash -c "rm -f /api/tmp/pids/server.pid && foreman start -f /api/Procfile"
environment:
- PORT=3000
- ELASTICSEARCH_URL=http://elasticsearch:9200
volumes:
- ./api:/api
- rails_log:/api/log
ports:
- "3000:3000"
depends_on:
- db
networks:
- appnet
networks:
appnet:
volumes:
db:
elasticsearch:
I put common.networks, set to ELASTICSEARCH_URL=http://elasticsearch:9200.我把common.networks,设置为ELASTICSEARCH_URL=http://elasticsearch:9200。
Can someone say me,where can be problem?有人能告诉我,哪里有问题吗? Or maybe I need add something more to code to connect with ElasticSearch?或者我可能需要在代码中添加更多内容才能连接到 ElasticSearch? But based in tutorials, I suppose it enough.但是基于教程,我想这就足够了。
Maybe some hints.也许一些提示。 I will be grateful我会很感激
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.