How use Chewy as Elasticsearch in Rest Api project of Ruby on Rails?

372 views Asked by At

I have problem with connect Chewy to my Rest Api app with Ruby on Rails. I have simple Item and controller with base CRUD. 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. Speaking simpler, All changes in my db will be influence to Chewy

My code

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

class Item < ActiveRecord::Base
  update_index('items#item') { self }
end

config/chewy.yml

test:
  host: 'localhost:9250'
  prefix: 'test'
development:
  host: 'localhost:9200'

config/initializers/chewy.rb

Chewy.settings = {host: 'localhost:9200'}

app/controllers/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

Rails.application.routes.draw do
  post '/items', to: 'items#create'
end

My result When I add new Item by Api e.g POstman, I add new Item in DB but Chewy says UndefinedIndex . Can not find index name ..... logs_after_add_item

In Postman I got error : 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 e.g adding new Item or update. I read a few tutorial, and in simple configuration It was enought, when some person wants add some Object in DB and Chewy

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

I will be grateful for your help and hints I'm new to Ruby and Rails :-)

Edit 1

Ok. I found my mistake. I put Chewy directory with item_index.rb in the wrong directory. I put in api not app. 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)>>

logs_error

My 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 .

Can someone say me ,where can be problem? Or maybe I need add something more to code to connect with ElasticSearch? But based in tutorials, I suppose it enough.

Maybe some hints. I will be grateful

1

There are 1 answers

1
Never2Far On BEST ANSWER

I'm not sure that this is the issue, but it looks like as of Chewy version 7.2.0, this syntax:

update_index('items#item') { self }

has become

update_index('items') { self }

See version 7.2.0 in their changelog