Skip to content

Commit c0b86f4

Browse files
committed
plugins/algolia: update algolia gem to latest version, use batch api for creating index objects
Signed-off-by: Ruby Iris Juric <[email protected]>
1 parent e3c30c9 commit c0b86f4

File tree

5 files changed

+166
-151
lines changed

5 files changed

+166
-151
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gem 'jekyll', '>= 3.0.3'
2020
gem 'jekyll-paginate'
2121
gem 'bundler', '>= 1.7.9'
2222
gem 'nokogiri', '>= 1.6.3.1'
23-
gem 'algoliasearch', '>= 1.6.1'
23+
gem 'algolia', '>= 3.22.0'
2424
gem 'htmlentities', '>= 4.3.2'
2525
gem 'rubyzip', '>=1.1.6'
2626
gem 'dotenv', '>= 0.11.1'

Gemfile.lock

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ GEM
33
specs:
44
addressable (2.8.7)
55
public_suffix (>= 2.0.2, < 7.0)
6-
algoliasearch (1.27.5)
7-
httpclient (~> 2.8, >= 2.8.3)
8-
json (>= 1.5.1)
6+
algolia (3.22.0)
7+
base64 (>= 0.2.0, < 1)
8+
faraday (>= 1.0.1, < 3.0)
9+
faraday-net_http_persistent (>= 0.15, < 3)
10+
net-http-persistent
911
ast (2.4.3)
1012
base64 (0.3.0)
1113
bigdecimal (3.2.2)
1214
colorator (1.1.0)
1315
concurrent-ruby (1.3.5)
16+
connection_pool (2.5.3)
1417
csv (3.3.5)
1518
diff-lcs (1.6.2)
1619
docile (1.4.1)
@@ -20,6 +23,15 @@ GEM
2023
http_parser.rb (~> 0)
2124
eventmachine (1.2.7)
2225
execjs (2.10.0)
26+
faraday (2.13.2)
27+
faraday-net_http (>= 2.0, < 3.5)
28+
json
29+
logger
30+
faraday-net_http (3.4.1)
31+
net-http (>= 0.5.0)
32+
faraday-net_http_persistent (2.3.1)
33+
faraday (~> 2.5)
34+
net-http-persistent (>= 4.0.4, < 5)
2335
ffi (1.17.2)
2436
ffi (1.17.2-aarch64-linux-gnu)
2537
ffi (1.17.2-aarch64-linux-musl)
@@ -62,8 +74,6 @@ GEM
6274
googlestaticmap (1.2.6)
6375
htmlentities (4.3.4)
6476
http_parser.rb (0.8.0)
65-
httpclient (2.9.0)
66-
mutex_m
6777
i18n (1.14.7)
6878
concurrent-ruby (~> 1.0)
6979
jekyll (4.4.1)
@@ -101,9 +111,13 @@ GEM
101111
listen (3.9.0)
102112
rb-fsevent (~> 0.10, >= 0.10.3)
103113
rb-inotify (~> 0.9, >= 0.9.10)
114+
logger (1.7.0)
104115
mercenary (0.4.0)
105116
mini_portile2 (2.8.9)
106-
mutex_m (0.3.0)
117+
net-http (0.6.0)
118+
uri
119+
net-http-persistent (4.0.6)
120+
connection_pool (~> 2.2, >= 2.2.4)
107121
newrelic_rpm (9.19.0)
108122
nokogiri (1.18.8)
109123
mini_portile2 (~> 2.8.2)
@@ -216,6 +230,7 @@ GEM
216230
uglifier (4.2.1)
217231
execjs (>= 0.3.0, < 3)
218232
unicode-display_width (2.6.0)
233+
uri (1.0.3)
219234
webrick (1.9.1)
220235

221236
PLATFORMS
@@ -240,7 +255,7 @@ PLATFORMS
240255
x86_64-linux-musl
241256

242257
DEPENDENCIES
243-
algoliasearch (>= 1.6.1)
258+
algolia (>= 3.22.0)
244259
bundler (>= 1.7.9)
245260
dotenv (>= 0.11.1)
246261
googlestaticmap (>= 1.2.2)

plugins/generator_algolia.rb

Lines changed: 111 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
require 'htmlentities'
16-
require 'algoliasearch'
16+
require 'algolia'
1717
require 'slugize'
1818
require 'dotenv'
1919
require 'securerandom'
@@ -34,9 +34,9 @@ def generate(site)
3434

3535
@prefix = config['prefix']
3636
@random_code = random_code
37-
Algolia.init(application_id: config['app_id'],
38-
api_key: config['api_key'])
39-
@indexes = setup_indexes
37+
@client = Algolia::SearchClient.create(config['app_id'], config['api_key'])
38+
39+
setup_indexes
4040
generate_all
4141
end
4242

@@ -77,177 +77,178 @@ def get_config
7777
end
7878

7979
def generate_all
80-
generate_blog_posts
81-
generate_guides
82-
generate_documentation
83-
generate_none_guide_guides
84-
generate_other
80+
requests = [].concat(generate_blog_posts,
81+
generate_guides,
82+
generate_documentation,
83+
generate_none_guide_guides,
84+
generate_other).compact
85+
86+
@client.multiple_batch(
87+
Algolia::Search::BatchParams.new(requests: requests)
88+
)
8589
end
8690

8791
def random_code
8892
SecureRandom.hex
8993
end
9094

9195
def setup_indexes
92-
indexes = {}
9396
@site.data['search_indexes'].each do |name, properties|
94-
index = Algolia::Index.new(@prefix + name)
9597
unless properties['settings'].nil?
96-
index.set_settings(properties['settings'])
98+
@client.set_settings("#{@prefix}#{name}", Algolia::Search::IndexSettings.new(properties['settings']))
9799
end
98-
indexes[name] = index
99100
end
100-
indexes
101101
end
102102

103103
def generate_documentation
104-
return if @site.data['docs'].nil?
104+
return [] if @site.data['docs'].nil?
105105

106-
documents = @site.data['docs'][:symbols].map do |item|
106+
@site.data['docs'][:symbols].map do |item|
107107
next if item[:language] == 'c_preview'
108108

109-
if item[:summary].nil? || item[:summary].strip.length == 0
109+
if item[:summary].nil? || item[:summary].strip.empty?
110110
Jekyll.logger.warn(
111111
'Search Warning:',
112112
"There was no summary for the symbol '#{item[:name]}' in #{item[:language]}."
113113
)
114114
end
115-
{
116-
'objectID' => item[:url],
117-
'title' => item[:name],
118-
'splitTitle' => item[:name].split(/(?=[A-Z])/).join(' '),
119-
'url' => item[:url],
120-
'summary' => item[:summary],
121-
'kind' => item[:kind],
122-
'language' => item[:language],
123-
'type' => 'documentation',
124-
'ranking' => doc_language_rank[item[:language]] * 1000,
125-
'randomCode' => @random_code
126-
}
115+
116+
Algolia::Search::MultipleBatchRequest.new(
117+
action: 'addObject',
118+
index_name: "#{@prefix}documentation",
119+
body: {
120+
'objectID' => item[:url],
121+
'title' => item[:name],
122+
'splitTitle' => item[:name].split(/(?=[A-Z])/).join(' '),
123+
'url' => item[:url],
124+
'summary' => item[:summary],
125+
'kind' => item[:kind],
126+
'language' => item[:language],
127+
'type' => 'documentation',
128+
'ranking' => doc_language_rank[item[:language]] * 1000,
129+
'randomCode' => @random_code
130+
}
131+
)
127132
end.compact
128-
@indexes['documentation'].save_objects(documents)
129133
end
130134

131135
def generate_blog_posts
132-
documents = []
133-
@site.posts.docs.each do | post |
136+
@site.posts.docs.flat_map do |post|
134137
# Calculate the age of the post so we can prioritise newer posts
135138
# over older ones.
136139
# NOTE: post.date is actually a Time object, despite its name
137140
age = (Time.now - post.date).round
138141
author = post.data['author']
139142

140-
post.get_sections.each do | section |
143+
post.get_sections.map do |section|
141144
# Ignore sections without any contents.
142-
if section[:contents].strip.size == 0
143-
next
144-
end
145-
146-
if section[:title].nil?
147-
url = post.url
148-
else
149-
url = post.url + '#' + section[:title].slugize
150-
end
151-
152-
document = {
153-
'objectID' => url,
154-
'title' => post.data['title'],
155-
'sectionTitle' => section[:title],
156-
'url' => url,
157-
'urlDisplay' => post.url,
158-
'author' => author,
159-
'content' => HTMLEntities.new.decode(section[:contents]),
160-
'posted' => post.date,
161-
'age' => age,
162-
'type' => 'blog post',
163-
'randomCode' => @random_code
164-
}
165-
documents << document
145+
next if section[:contents].strip.empty?
146+
147+
url = section[:title].nil? ? post.url : "#{post.url}##{section[:title].slugize}"
148+
149+
Algolia::Search::MultipleBatchRequest.new(
150+
action: 'addObject',
151+
index_name: "#{@prefix}blog-posts",
152+
body: {
153+
'objectID' => url,
154+
'title' => post.data['title'],
155+
'sectionTitle' => section[:title],
156+
'url' => url,
157+
'urlDisplay' => post.url,
158+
'author' => author,
159+
'content' => HTMLEntities.new.decode(section[:contents]),
160+
'posted' => post.date,
161+
'age' => age,
162+
'type' => 'blog post',
163+
'randomCode' => @random_code
164+
}
165+
)
166166
end
167167
end
168-
@indexes['blog-posts'].save_objects(documents)
169168
end
170169

171170
def generate_guides
172-
documents = []
173-
return if @site.collections['guides'].nil?
171+
return [] if @site.collections['guides'].nil?
174172

175-
@site.collections['guides'].docs.each do | guide |
173+
@site.collections['guides'].docs.flat_map do |guide|
176174
group = @site.data['guides'][guide.data['guide_group']]
177175
unless group.nil? || group['subgroups'].nil? || guide.data['guide_subgroup'].nil?
178176
subgroup = group.nil? ? '' : group['subgroups'][guide.data['guide_subgroup']]
179177
end
180178

181-
guide.get_sections.each do | section |
182-
url = guide.url
183-
unless section[:title].nil?
184-
url = url + '#' + section[:title].slugize
185-
end
186-
187-
document = {
188-
'objectID' => url,
189-
'title' => guide.data['title'],
190-
'sectionTitle' => section[:title],
191-
'url' => url,
192-
'urlDisplay' => guide.url,
193-
'content' => HTMLEntities.new.decode(section[:contents]),
194-
'group' => group.nil? ? '' : group['title'],
195-
'subgroup' => subgroup.nil? ? '' : subgroup['title'],
196-
'type' => 'guide',
197-
'randomCode' => @random_code
198-
}
199-
documents << document
179+
guide.get_sections.map do |section|
180+
url = section[:title].nil? ? guide.url : "#{guide.url}##{section[:title].slugize}"
181+
182+
Algolia::Search::MultipleBatchRequest.new(
183+
action: 'addObject',
184+
index_name: "#{@prefix}guides",
185+
body: {
186+
'objectID' => url,
187+
'title' => guide.data['title'],
188+
'sectionTitle' => section[:title],
189+
'url' => url,
190+
'urlDisplay' => guide.url,
191+
'content' => HTMLEntities.new.decode(section[:contents]),
192+
'group' => group.nil? ? '' : group['title'],
193+
'subgroup' => subgroup.nil? ? '' : subgroup['title'],
194+
'type' => 'guide',
195+
'randomCode' => @random_code
196+
}
197+
)
200198
end
201199
end
202-
203-
@indexes['guides'].save_objects(documents)
204200
end
205201

206202
def generate_none_guide_guides
207-
documents = []
208203
gs_pages = @site.pages.select { |page| page.data['search_index'] }
209-
gs_pages.each do |page|
210-
page.get_sections.each do |section|
211-
url = page.url
212-
url = url + '#' + section[:title].slugize unless section[:title].nil?
213-
document = {
214-
'objectID' => url,
215-
'title' => page.data['title'],
216-
'sectionTitle' => section[:title],
217-
'url' => url,
218-
'urlDisplay' => page.url,
219-
'content' => HTMLEntities.new.decode(section[:contents]),
220-
'group' => page.data['search_group'],
221-
'subgroup' => page.data['sub_group'],
222-
'type' => 'not-guide',
223-
'randomCode' => @random_code
224-
}
225-
documents << document
204+
205+
gs_pages.flat_map do |page|
206+
page.get_sections.map do |section|
207+
url = section[:title].nil? ? page.url : "#{page.url}##{section[:title].slugize}"
208+
209+
Algolia::Search::MultipleBatchRequest.new(
210+
action: 'addObject',
211+
index_name: "#{@prefix}guides",
212+
body: {
213+
'objectID' => url,
214+
'title' => page.data['title'],
215+
'sectionTitle' => section[:title],
216+
'url' => url,
217+
'urlDisplay' => page.url,
218+
'content' => HTMLEntities.new.decode(section[:contents]),
219+
'group' => page.data['search_group'],
220+
'subgroup' => page.data['sub_group'],
221+
'type' => 'not-guide',
222+
'randomCode' => @random_code
223+
}
224+
)
226225
end
227226
end
228-
@indexes['guides'].save_objects(documents)
229227
end
230228

231229
def generate_other
232-
documents = @site.data['search-other'].map do |other|
233-
{
230+
@site.data['search-other'].map do |other|
231+
Algolia::Search::MultipleBatchRequest.new(
232+
action: 'addObject',
233+
index_name: "#{@prefix}other",
234+
body: {
234235
'objectID' => other['id'],
235236
'title' => other['title'],
236237
'url' => other['url'],
237238
'content' => other['description'],
238239
'randomCode' => @random_code
239-
}
240+
}
241+
)
240242
end
241-
@indexes['other'].save_objects(documents)
242243
end
243244

244245
def doc_language_rank
245246
{
246-
'c' => 10,
247-
'rockyjs' => 9,
248-
'pebblekit_js' => 8,
249-
'pebblekit_android' => 6,
250-
'pebblekit_ios' => 4
247+
'c' => 10,
248+
'rockyjs' => 9,
249+
'pebblekit_js' => 8,
250+
'pebblekit_android' => 6,
251+
'pebblekit_ios' => 4
251252
}
252253
end
253254
end

0 commit comments

Comments
 (0)