V
V
Vadim2017-08-28 12:37:36
Ruby on Rails
Vadim, 2017-08-28 12:37:36

How to set up elasticsearch for RoR?

Tell me how to properly configure elastic.
sources:

#model Products
class Product < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include ElasticMyAnalyzer

  settings ES_SETTING do
    mappings dynamic: 'true' do
      indexes :title, type: 'string', analyzer: 'my_analyzer', fielddata: true
      indexes :description, type: 'string', analyzer: 'my_analyzer'
      indexes :searching, type: 'boolean'
    end
  end
end

module ElasticMyAnalyzer
  ES_SETTING = {
    index: {
      number_of_shards: 2
    },
    analysis: {
      filter: {
        my_stopwords: {
          type: 'stop',
          stopwords: 'а,без,более,бы,был,была,были,было,быть,в,вам,вас,весь,во,вот,все,всего,всех,вы,где,да,даже,для,до,его,ее,если,есть,еще,же,за,здесь,и,из,или,им,их,к,как,ко,когда,кто,ли,либо,мне,может,мы,на,надо,наш,не,него,нее,нет,ни,них,но,ну,о,об,однако,он,она,они,оно,от,очень,по,под,при,с,со,так,также,такой,там,те,тем,то,того,тоже,той,только,том,ты,у,уже,хотя,чего,чей,чем,что,чтобы,чье,чья,эта,эти,это,я'
        },
        mynGram: {
          type: 'ngram',
          min_gram: 4,
          max_gram: 8
        }
      },
      analyzer: {
        my_analyzer: {
          type: 'custom',
          tokenizer: 'standard',
          filter: [
            'lowercase', 'russian_morphology', 'my_stopwords'
            # 'lowercase', 'russian_morphology', 'my_stopwords', 'mynGram'
          ]
        }
      }
    }
  }
end

class SearchesController < ApplicationController
  def index
    @query = params[:q]
    @response_query = Product.search(search_query(@query)).records
    @response_query = @response_query.paginate(page: params[:page], per_page: 16)
  endx

  def search_query(query)
    {
      query: {
        bool: {
          must: {
            multi_match: {
              query: query,
              fields: ['title', 'description'],
              operator: 'and'
            }
          },
          filter: [
            {
              # term: { searching: true } 
            }
          ]
        }
      },
      sort: [
        {
          title: { order: 'asc' }
        }
      ]
    }
  end
end

as a result, with existing settings: searches only for whole words. outputs max. 10 results. sorts crookedly.
I want a good smart search. configure to highlight the found match in the title, sort correctly alphabetically, display all the results and search from 3 letters - here, in principle, nGram copes (but I don’t quite like how it works, it gives a lot of unnecessary results if you connect the search in description finally tin), so how can you do without it?
and how you can prioritize search by title. those. if there is a match in the title, then do not search in the description?
and finally)) how to tie a live search to all this.
+100 to your karma)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question