-
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
6 changed files
with
139 additions
and
0 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,69 @@ | ||
# Description: | ||
# Include this module in models that should be searchable. Also add the model class | ||
# to the Searchable::MODELS array (we're avoiding metaprogramming to keep track of | ||
# included models since it isn't reliable in lazy loading environments). | ||
# | ||
# 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 | ||
|
||
MODELS = [Link, Post].freeze | ||
|
||
def self.search(query) | ||
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 | ||
|
||
raise "add #{name} to Searchable::MODELS" unless Searchable::MODELS.include?(self) | ||
|
||
after_commit on: :create, if: :should_index? do | ||
__elasticsearch__.index_document | ||
end | ||
|
||
after_commit on: :update, if: :should_index? do | ||
__elasticsearch__.update_document | ||
end | ||
|
||
after_commit on: :destroy do | ||
__elasticsearch__.delete_document | ||
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
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 |