$ -weight: 500;">docker run -p 9200:9200 -e "discovery.type=single-node" -weight: 500;">docker.elastic.co/elasticsearch/elasticsearch:8.13.0
-weight: 500;">docker run -p 9200:9200 -e "discovery.type=single-node" -weight: 500;">docker.elastic.co/elasticsearch/elasticsearch:8.13.0
-weight: 500;">docker run -p 9200:9200 -e "discovery.type=single-node" -weight: 500;">docker.elastic.co/elasticsearch/elasticsearch:8.13.0
# Gemfile
gem 'elasticsearch'
gem 'searchkick'
# Gemfile
gem 'elasticsearch'
gem 'searchkick'
# Gemfile
gem 'elasticsearch'
gem 'searchkick'
# app/models/product.rb
class Product < ApplicationRecord searchkick
end
# app/models/product.rb
class Product < ApplicationRecord searchkick
end
# app/models/product.rb
class Product < ApplicationRecord searchkick
end
Product.reindex
Product.reindex
Product.reindex
# app/controllers/products_controller.rb
class ProductsController < ApplicationController def index query = params[:q].presence || "*" @products = Product.search(query, fields: [:name, :description], match: :word_start, misspellings: { edit_distance: 2 } ) end
end
# app/controllers/products_controller.rb
class ProductsController < ApplicationController def index query = params[:q].presence || "*" @products = Product.search(query, fields: [:name, :description], match: :word_start, misspellings: { edit_distance: 2 } ) end
end
# app/controllers/products_controller.rb
class ProductsController < ApplicationController def index query = params[:q].presence || "*" @products = Product.search(query, fields: [:name, :description], match: :word_start, misspellings: { edit_distance: 2 } ) end
end
# app/models/product.rb
class Product < ApplicationRecord searchkick belongs_to :category def search_data { name: name, description: description, price: price, # We can even index associated data! category_name: category.name, in_stock: stock_count > 0 } end
end
# app/models/product.rb
class Product < ApplicationRecord searchkick belongs_to :category def search_data { name: name, description: description, price: price, # We can even index associated data! category_name: category.name, in_stock: stock_count > 0 } end
end
# app/models/product.rb
class Product < ApplicationRecord searchkick belongs_to :category def search_data { name: name, description: description, price: price, # We can even index associated data! category_name: category.name, in_stock: stock_count > 0 } end
end - fields: We tell it to only look at the name and description.
- match: :word_start allows for auto-complete. If they type "lap", it matches "laptop".
- misspellings: This is the magic. If they type "lpatop", Elasticsearch knows they meant "laptop" because the "edit distance" (number of wrong letters) is within our limit. - Add the gem.
- Add searchkick to your model.
- Run Model.reindex.
- Use Model.search("query").