-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Elasticsearch with elasticsearch-rails #2
Open
dbackeus
wants to merge
1
commit into
before-elastic
Choose a base branch
from
elasticsearch-rails
base: before-elastic
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class SearchController < ApplicationController | ||
def index | ||
@results = Searchable.search(params[:query]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class IndexerJob < ApplicationJob | ||
def perform(operation, klass, id) | ||
klass = klass.constantize | ||
|
||
case operation | ||
when "create" | ||
model = klass.find_by_id(id) | ||
return unless model | ||
|
||
model.__elasticsearch__.index_document | ||
when /update/ | ||
model = klass.find_by_id(id) | ||
return unless model | ||
|
||
model.__elasticsearch__.update_document | ||
when /delete/ | ||
begin | ||
klass.__elasticsearch__.client.delete(index: klass.index_name, id: id) | ||
rescue Elasticsearch::Transport::Transport::Errors::NotFound # rubocop:disable Lint/SuppressedException | ||
end | ||
else | ||
raise ArgumentError, "Unknown operation '#{operation}'" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Description: | ||
# Include this module in models that should be searchable. | ||
# | ||
# Models including the concern are expected to implement 'title' and 'searchable_content' | ||
# for indexing. Title will be given higher priority compared to content. | ||
# | ||
# After adding and indexing your models. Search across all models can be performed via | ||
# the Searchable.search method. | ||
|
||
module Searchable | ||
extend ActiveSupport::Concern | ||
|
||
mattr_accessor :models | ||
self.models = [] | ||
|
||
def self.search(query) | ||
return [] if query.blank? | ||
|
||
search_definition = { | ||
query: { | ||
multi_match: { | ||
query: query, | ||
fields: ["title^2", "content"], | ||
}, | ||
}, | ||
} | ||
|
||
Elasticsearch::Model.search(search_definition, models).records | ||
end | ||
|
||
def as_indexed_json(_options = {}) | ||
{ | ||
title: title, | ||
content: searchable_content, | ||
} | ||
end | ||
|
||
def should_index? | ||
true | ||
end | ||
|
||
def searchable_content | ||
raise NotImplementedError | ||
end | ||
|
||
included do | ||
include Elasticsearch::Model | ||
|
||
Searchable.models << self | ||
|
||
after_commit on: :create, if: :should_index? do | ||
IndexerJob.perform_later("create", self.class.name, id) | ||
end | ||
|
||
after_commit on: :update, if: :should_index? do | ||
IndexerJob.perform_later("update", self.class.name, id) | ||
end | ||
|
||
after_commit on: :destroy do | ||
IndexerJob.perform_later("delete", self.class.name, id) | ||
end | ||
|
||
settings index: { number_of_shards: 1 } do | ||
mappings dynamic: "false" do | ||
indexes :title, analyzer: "english", boost: 2 | ||
indexes :content, analyzer: "english" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
class Post < ApplicationRecord | ||
include Searchable | ||
|
||
validates_presence_of :title | ||
validates_presence_of :body | ||
|
||
private | ||
|
||
def searchable_content | ||
body | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<h1>Search</h1> | ||
|
||
<%= form_with url: search_path, method: :get do |form| %> | ||
<%= form.label :query %> | ||
<%= form.text_field :query, value: params[:query], autofocus: true %> | ||
<%= form.submit "Search" %> | ||
<% end %> | ||
|
||
<% if @results.present? %> | ||
<h2>Results</h2> | ||
<ul> | ||
<% @results.each do |result| %> | ||
<li><%= link_to result.title, result %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# https://github.com/elastic/elasticsearch-ruby/issues/1429#issuecomment-958162468 | ||
module Elasticsearch | ||
class Client | ||
def verify_with_version_or_header(*_args) | ||
@verified = true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE: The implementation of this Job was based on the implementation suggested in the
elasticsearch-model
gem: https://github.com/elastic/elasticsearch-rails/tree/main/elasticsearch-model#asynchronous-callbacksI would not have written this with an
operation
argument andcase
statement if I did it from scratch.