-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Elasticsearch indexing with elasticsearch-(model|rails)
- Loading branch information
Showing
13 changed files
with
191 additions
and
3 deletions.
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