Skip to content

Commit

Permalink
elasticsearch for topics (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
morr committed Jun 18, 2017
1 parent d8a6116 commit 72266be
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/models/topic.rb
Expand Up @@ -5,6 +5,7 @@ class Topic < ApplicationRecord
include Commentable
include Moderatable
include Viewable
include ElasticsearchConcern

NEWS_WALL = /[\r\n]*\[wall[\s\S]+\[\/wall\]\Z/

Expand Down
19 changes: 19 additions & 0 deletions app/services/elasticsearch/data/topic.rb
@@ -0,0 +1,19 @@
class Elasticsearch::Data::Topic < Elasticsearch::Data::DataBase
name_fields %i[name]
data_fields %i[locale forum_id]
track_changes_fields %i[name forum_id]

private

def name
fix Topics::TopicViewFactory.new(true, true).build(@entry).topic_title
end

def locale
@entry.locale
end

def forum_id
@entry.forum_id
end
end
25 changes: 25 additions & 0 deletions app/services/elasticsearch/query/topic.rb
@@ -0,0 +1,25 @@
class Elasticsearch::Query::Topic < Elasticsearch::Query::QueryBase
method_object %i[phrase locale forum_id limit]

private

def query
{
bool: {
must: [name_fields_query, locale_query, forum_id_query]
}
}
end

def locale_query
{ term: { locale: @locale } }
end

def forum_id_query
{ term: { forum_id: @forum_id } }
end

def cache_key
super + [@locale, @forum_id]
end
end
4 changes: 2 additions & 2 deletions app/services/elasticsearch/reindex.rb
Expand Up @@ -3,10 +3,10 @@ class Elasticsearch::Reindex

CACHE_KEY = 'elastic_reindex'

TYPES = %i[anime manga ranobe character person user club collection]
TYPES = %i[anime manga ranobe character person user club collection topic]

def call
@types.each do |type|
Array(@types).each do |type|
respond_to?("fill_#{type}") ? send("fill_#{type}") : fill_type(type)
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/workers/elasticsearch/create.rb
Expand Up @@ -7,12 +7,13 @@ def perform entry_id, entry_type
end

def sync entry
type = entry.class.name.pluralize.downcase
klass = entry.is_a?(Topic) ? Topic : entry.class
type = klass.name.pluralize.downcase

Elasticsearch::Client.instance.send(
self.class::METHOD,
"#{INDEX}_#{type}/#{type}/#{entry.id}",
"Elasticsearch::Data::#{entry.class.name}".constantize.call(entry)
"Elasticsearch::Data::#{klass.name}".constantize.call(entry)
)
end
end
5 changes: 5 additions & 0 deletions config/app/elasticsearch.yml
Expand Up @@ -169,3 +169,8 @@
properties:
name:
<<: *name_properties
:topics:
<<: *default_type_mappings
properties:
name:
<<: *name_properties
2 changes: 2 additions & 0 deletions spec/models/topic_spec.rb
Expand Up @@ -290,4 +290,6 @@
end
end
end

it_behaves_like :elasticsearch_concern, :anime
end
21 changes: 21 additions & 0 deletions spec/services/elasticsearch/query/topic_spec.rb
@@ -0,0 +1,21 @@
describe Elasticsearch::Query::Topic do
let(:service) do
Elasticsearch::Query::Topic.new(
phrase: phrase,
locale: locale,
forum_id: forum_id,
limit: limit
)
end

describe '#call', :vcr do
let(:phrase) { 'kai' }
let(:locale) { 'ru' }
let(:forum_id) { 1 }
let(:limit) { 10 }

subject { service.call }

it { is_expected.to have(limit).items }
end
end
30 changes: 30 additions & 0 deletions spec/vcr_cassettes/Elasticsearch_Query_Topic/_call/1_1_1.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72266be

Please sign in to comment.