Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question