# Wish the search method for elasticsearch is working on rails

**URL:** <https://discuss.elastic.co/t/wish-the-search-method-for-elasticsearch-is-working-on-rails/268620>\
**Category:** Elasticsearch\
**Created:** [March 29, 2021, 1:56am UTC](https://discuss.elastic.co/t/wish-the-search-method-for-elasticsearch-is-working-on-rails/268620 "2021-03-29T01:56:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeppokoNeet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/heppokoneet/32/86212_2.png) [@HeppokoNeet](https://discuss.elastic.co/u/HeppokoNeet)\
**Post date:** [March 29, 2021, 1:56am UTC](https://discuss.elastic.co/t/wish-the-search-method-for-elasticsearch-is-working-on-rails/268620/1 "2021-03-29T01:56:46Z")

</div>

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

```auto
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.

```auto
/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.

```auto
/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=""
...

```

```auto
/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.

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

```

■The problem

On rails console, i typed command

```auto
Project.es_search('Game')

```

Response was following.  
※Game is included in the record

```auto
#<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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 26, 2021, 1:57am UTC](https://discuss.elastic.co/t/wish-the-search-method-for-elasticsearch-is-working-on-rails/268620/2 "2021-04-26T01:57:21Z")

</div>

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