Skip to content

Commit be2375d

Browse files
MMXMMX
authored andcommitted
Fix settings.yml, Update rake tasks, Remove client.en.yml.
1 parent d7a3c99 commit be2375d

File tree

6 files changed

+101
-34
lines changed

6 files changed

+101
-34
lines changed

config/locales/client.en.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

config/locales/server.en.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
en:
22
site_settings:
3-
discourse_elasticsearch_enabled: "Enable discourse-elasticsearch plugin"
3+
elasticsearch_enabled: "Enable discourse-elasticsearch plugin test 123"
4+
elasticsearch_autocomplete_enabled: "auto complete js"
5+
elasticsearch_server_ip: "elasticsearch server ip address"
6+
elasticsearch_server_port: "elasticsearch server port"
7+
elasticsearch_discourse_username: "discourse username"

config/settings.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
plugins:
2-
discourse_elasticsearch_enabled:
2+
elasticsearch_enabled:
33
default: true
44
client: true
55
elasticsearch_autocomplete_enabled:
66
default: false
77
client: true
88
elasticsearch_server_ip:
9-
default: ''
9+
default: 'localhost'
1010
client: true
1111
elasticsearch_server_port:
12-
default: ''
12+
default: '9200'
1313
client: true
14-
algolia_discourse_username:
14+
elasticsearch_discourse_username:
1515
default: 'system'
1616
client: false

lib/discourse_elasticsearch/elasticsearch_helper.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def self.index_user(user_id, discourse_event)
1818
return if user.blank? || !guardian.can_see?(user)
1919

2020
user_record = to_user_record(user)
21-
add_elasticsearch_single_info(USERS_INDEX, user_record, user_id)
21+
add_elasticsearch_users(USERS_INDEX, user_record, user_id)
2222
end
2323

2424
def self.to_user_record(user)
@@ -166,7 +166,7 @@ def self.index_tags(tag_names, discourse_event)
166166
tag_names.each do |tag_name|
167167
tag = Tag.find_by_name(tag_name)
168168
if tag && should_index_tag?(tag)
169-
add_elasticsearch_single_info(TAGS_INDEX, to_tag_record(tag), tag.id)
169+
add_elasticsearch_users(TAGS_INDEX, to_tag_record(tag), tag.id)
170170
end
171171
end
172172
end
@@ -175,14 +175,23 @@ def self.should_index_tag?(tag)
175175
tag.topic_count > 0
176176
end
177177

178-
def self.add_elasticsearch_single_info(index_name, record, user_id)
178+
def self.add_elasticsearch_users(index_name, record, user_id)
179179
client = elasticsearch_index(index_name)
180-
client.index index: index_name, id: user_id, body: record
180+
client.index index: index_name, id: user_id, body: record
181181
end
182182

183183
def self.add_elasticsearch_posts(index_name, posts)
184184
client = elasticsearch_index(index_name)
185-
client.index index: index_name, body: posts
185+
posts.each do |post|
186+
client.index index: index_name, body: post
187+
end
188+
end
189+
190+
def self.add_elasticsearch_tags(index_name, tags)
191+
client = elasticsearch_index(index_name)
192+
tags.each do |tag|
193+
client.index index: index_name, body: tag
194+
end
186195
end
187196

188197
def self.elasticsearch_index(index_name)
@@ -192,6 +201,15 @@ def self.elasticsearch_index(index_name)
192201
return client
193202
end
194203

204+
def self.clean_indices(index_name)
205+
client = elasticsearch_index(index_name)
206+
if client.indices.exists? index: index_name
207+
client.indices.delete index: index_name
208+
else
209+
puts "Indices #{index_name} doesn't exist..."
210+
end
211+
end
212+
195213
def self.guardian
196214
Guardian.new(User.find_by(username: SiteSetting.elasticsearch_discourse_username))
197215
end

lib/tasks/discourse_elasticsearch.rake

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,90 @@ end
77
desc "configure elasticsearch index settings"
88
task "elasticsearch:configure" => :environment do
99
elasticsearch_configure_users
10-
#elasticsearch_configure_posts
11-
#elasticsearch_configure_tags
10+
elasticsearch_configure_posts
11+
elasticsearch_configure_tags
1212
end
1313

1414
desc "reindex everything to elasticsearch"
1515
task "elasticsearch:reindex" => :environment do
1616
elasticsearch_reindex_users
17-
# elasticsearch_reindex_posts
18-
# elasticsearch_reindex_tags
17+
elasticsearch_reindex_posts
18+
elasticsearch_reindex_tags
1919
end
2020

21+
desc "reindex users in elasticsearch"
22+
task "elasticsearch:reindex_users" => :environment do
23+
elasticsearch_reindex_users
24+
end
25+
26+
desc "reindex posts in elasticsearch"
27+
task "elasticsearch:reindex_posts" => :environment do
28+
elasticsearch_reindex_posts
29+
end
30+
31+
desc "reindex tags in elasticsearch"
32+
task "elasticsearch:reindex_tags" => :environment do
33+
elasticsearch_reindex_tags
34+
end
2135

2236
def elasticsearch_configure_users
23-
puts "[Starting] Pushing users index settings to Elasticsearch"
24-
# DiscourseElasticsearch::ElasticsearchHelper.elasticsearch_index(
25-
# DiscourseElasticsearch::ElasticsearchHelper::USERS_INDEX).set_settings(
26-
# "searchableAttributes" => ["unordered(username)", "unordered(name)"],
27-
# "attributesToHighlight" => [:username, :name],
28-
# "attributesToRetrieve" => [:username, :name, :url, :avatar_template, :likes_received, :days_visited],
29-
# "customRanking" => ["desc(likes_received)", "desc(days_visited)"],
30-
# "removeWordsIfNoResults" => "allOptional"
31-
# )
37+
puts "[Starting] Cleaning users index to Elasticsearch"
38+
DiscourseElasticsearch::ElasticsearchHelper.clean_indices(DiscourseElasticsearch::ElasticsearchHelper::USERS_INDEX)
3239
puts "[Finished] Successfully configured users index in Elasticsearch"
3340
end
3441

42+
def elasticsearch_configure_posts
43+
puts "[Starting] Cleaning posts index to Elasticsearch"
44+
DiscourseElasticsearch::ElasticsearchHelper.clean_indices(DiscourseElasticsearch::ElasticsearchHelper::POSTS_INDEX)
45+
puts "[Finished] Successfully configured posts index in Elasticsearch"
46+
end
47+
48+
def elasticsearch_configure_tags
49+
puts "[Starting] Cleaning tags index to Elasticsearch"
50+
DiscourseElasticsearch::ElasticsearchHelper.clean_indices(DiscourseElasticsearch::ElasticsearchHelper::TAGS_INDEX)
51+
puts "[Finished] Successfully configured tags index in Elasticsearch"
52+
end
53+
3554
def elasticsearch_reindex_users
3655

3756
puts "[Starting] Pushing users to Elasticsearch"
38-
user_records = []
3957
User.all.each do |user|
4058
#user_records << DiscourseElasticsearch::ElasticsearchHelper.to_user_record(user)
41-
user_record = DiscourseElasticsearch::ElasticsearchHelper.to_user_record(user)
59+
puts user.id
60+
user_record = DiscourseElasticsearch::ElasticsearchHelper.index_user(user.id, '')
4261
puts user_record
4362
end
44-
# puts "[Progress] Gathered users from Discourse"
45-
# DiscourseElasticsearch::ElasticsearchHelper.elasticsearch_index(
46-
# DiscourseElasticsearch::ElasticsearchHelper::USERS_INDEX).add_objects(user_records)
47-
# puts "[Finished] Successfully pushed #{user_records.length} users to Algolia"
63+
end
64+
65+
def elasticsearch_reindex_posts
66+
puts "[Starting] Pushing posts to Elasticsearch"
67+
post_records = []
68+
Post.all.includes(:user, :topic).each do |post|
69+
if DiscourseElasticsearch::ElasticsearchHelper.should_index_post?(post)
70+
post_records << DiscourseElasticsearch::ElasticsearchHelper.to_post_records(post)
71+
end
72+
end
73+
post_records.flatten!
74+
puts "[Progress] Gathered posts from Discourse"
75+
post_records.each_slice(100) do |slice|
76+
DiscourseElasticsearch::ElasticsearchHelper.add_elasticsearch_posts(
77+
DiscourseElasticsearch::ElasticsearchHelper::POSTS_INDEX, slice.flatten)
78+
puts "[Progress] Pushed #{slice.length} post records to Elasticsearch"
79+
end
80+
puts "[Finished] Successfully pushed #{post_records.length} posts to Elasticsearch"
81+
end
82+
83+
84+
def elasticsearch_reindex_tags
85+
puts "[Starting] Pushing tags to Elasticsearch"
86+
tag_records = []
87+
Tag.all.each do |tag|
88+
if DiscourseElasticsearch::ElasticsearchHelper.should_index_tag?(tag)
89+
tag_records << DiscourseElasticsearch::ElasticsearchHelper.to_tag_record(tag)
90+
end
91+
end
92+
puts "[Progress] Gathered tags from Discourse"
93+
DiscourseElasticsearch::ElasticsearchHelper.add_elasticsearch_tags(
94+
DiscourseElasticsearch::ElasticsearchHelper::TAGS_INDEX, tag_records)
95+
puts "[Finished] Successfully pushed #{tag_records.length} tags to Elasticsearch"
4896
end

plugin.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# name: discourse-elasticsearch
22
# about:
3-
# version: 0.1
3+
# version: 0.2
44
# authors: imMMX
55
# url: https://github.com/imMMX
66

@@ -15,7 +15,7 @@
1515
register_asset "stylesheets/common/discourse-elasticsearch.scss"
1616

1717

18-
enabled_site_setting :discourse_elasticsearch_enabled
18+
enabled_site_setting :elasticsearch_enabled
1919

2020
PLUGIN_NAME ||= "discourse-elasticsearch".freeze
2121

0 commit comments

Comments
 (0)