|
7 | 7 | desc "configure elasticsearch index settings" |
8 | 8 | task "elasticsearch:configure" => :environment do |
9 | 9 | elasticsearch_configure_users |
10 | | - #elasticsearch_configure_posts |
11 | | - #elasticsearch_configure_tags |
| 10 | + elasticsearch_configure_posts |
| 11 | + elasticsearch_configure_tags |
12 | 12 | end |
13 | 13 |
|
14 | 14 | desc "reindex everything to elasticsearch" |
15 | 15 | task "elasticsearch:reindex" => :environment do |
16 | 16 | elasticsearch_reindex_users |
17 | | - # elasticsearch_reindex_posts |
18 | | - # elasticsearch_reindex_tags |
| 17 | + elasticsearch_reindex_posts |
| 18 | + elasticsearch_reindex_tags |
19 | 19 | end |
20 | 20 |
|
| 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 |
21 | 35 |
|
22 | 36 | 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) |
32 | 39 | puts "[Finished] Successfully configured users index in Elasticsearch" |
33 | 40 | end |
34 | 41 |
|
| 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 | + |
35 | 54 | def elasticsearch_reindex_users |
36 | 55 |
|
37 | 56 | puts "[Starting] Pushing users to Elasticsearch" |
38 | | - user_records = [] |
39 | 57 | User.all.each do |user| |
40 | 58 | #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, '') |
42 | 61 | puts user_record |
43 | 62 | 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" |
48 | 96 | end |
0 commit comments