Wish the search method for elasticsearch is working on rails

■The environment

・MacOS
・RailsServer
・Ruby 2.4.1
・Ruby on Rails 5.1.7
・MySQL
・Elasticsearch 7.10.2-SNAPSHOT
・kuromoji

・elasticsearch (7.4.0)
・elasticsearch-api (7.4.0)
・elasticsearch-model (7.1.0 80822d6)
・elasticsearch-rails (7.1.0 80822d6)
・elasticsearch-transport (7.4.0)

■My wish

Hi I'm japanese.
My wish is that read faster on database to use elasticsearch on rails.
Now i'm trouble with search method on rails.

I will explain the current settings.
First, I think the record on elastic search already input on following below.

curl 'localhost:9200/_cat/indices?v'
health status index                  uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   es_project_development Fpu_adXWT9Gtw7KZTh0aDw   1   1       6804            0      3.7mb          3.7mb

Second, the setting of index and mapping for elasticsearch are the following below.

/models/concerns/project_searchable.rb

module ProjectSearchable
    extend ActiveSupport::Concern

    included do
      include Elasticsearch::Model

      index_name "es_project_#{Rails.env}"

      settings do
        mappings dynamic: 'false' do
          indexes :id,          type: 'integer'
          indexes :title,   type: 'text', analyzer: 'kuromoji'
          indexes :contents,   type: 'text', analyzer: 'kuromoji'
          indexes :industry,      type: 'text'
...

      def as_indexed_json(*)
        attributes
          .symbolize_keys
          .slice(:id, :title, :contents, :industry, ...)
      end
    end

    class_methods do
      def create_index!
        client = __elasticsearch__.client
        client.indices.delete index: self.index_name rescue nil
        client.indices.create(index: self.index_name,
          body: {
            settings: self.settings.to_hash,
            mappings: self.mappings.to_hash
          })
      end

      def es_search(query)
        __elasticsearch__.search({
          query: {
            multi_match: {
               fields: %w(id title contents industry ...),
               type: 'cross_fields',
              query: query,
               operator: 'and'
            }
          }
        })
      end
    end
  end

Third, the view and the controller are the following below.

/views/top/index.html.slim

= form_tag search_path, {:method=>"get"}
    table border="0"
        tr
            td
                = text_field_tag 'keyword[name]', nil, class: 'write'
            td
                input.push type="submit" value=""
...
/controllers/projects_controller.rb

    def search
...
        @keyword = params.dig('keyword', 'name')

        params = ''
        connection = '?'

    ...

            if @keyword.present?
                params = "#{params}#{connection}keyword=#{@keyword}"
                connection = '&'
            end

    ...

Last, ProjectSearchable is included in models/project.rb.

/models/project.rb
class Project < ApplicationRecord
    include ProjectSearchable
...

■The problem

On rails console, i typed command

Project.es_search('Game')

Response was following.
※Game is included in the record

#<Elasticsearch::Model::Response::Response:0x007fc1a85621a8
@klass=[PROXY] Project (call 'Project.connection' to establish a connection),
@search=
#<Elasticsearch::Model::Searching::SearchRequest:0x007fc1a8562248
@definition=
{:index=>"es_project_development",
:type=>nil,
:body=>
{:query=>
{:multi_match=>
{:fields=>
["id",
"title",
"contents",
"industry",
"required",
...
"comment"],
:type=>"cross_fields",
:query=>"Game",
:operator=>"and"}}}},
@klass=[PROXY] Project (call 'Project.connection' to establish a connection),
@options={}>>

I think elasticsearch doesn't work.
My opinion of this problem is in search method for elasticsearch.
But i don't have enough knowledge about that.

I really need your help.
Thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.