-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathexport_and_add_rule_to_index.rb
67 lines (56 loc) · 1.69 KB
/
export_and_add_rule_to_index.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'algolia'
require 'dotenv/load'
require 'json'
# Algolia client credentials
ALGOLIA_APP_ID = ENV['ALGOLIA_APP_ID']
ALGOLIA_API_KEY = ENV['ALGOLIA_API_KEY']
ALGOLIA_INDEX_NAME = ENV['ALGOLIA_INDEX_NAME']
# Initialize the client and the index
# https://www.algolia.com/doc/api-client/getting-started/initialize/ruby/?client=ruby#initialize-the-search-client
client = Algolia::Search::Client.create(ALGOLIA_APP_ID, ALGOLIA_API_KEY)
index = client.init_index(ALGOLIA_INDEX_NAME)
# Create a rule
# https://www.algolia.com/doc/api-reference/api-methods/save-rule/?client=ruby
puts "Creating a rule ..."
rule = {
objectID: 'a-rule-id',
conditions: [{
pattern: 'Jimmie',
anchoring: 'is'
}],
consequence: {
params: {
filters: "zip_code = 12345"
}
}
}
# Optionally, to disable the rule
rule['enabled'] = false
# Optionally, to add validity time ranges
rule['validity'] = [
{
from: Time.now.to_i,
until: (DateTime.now + 10).to_time.to_i,
}
]
# Save the Rule.
index.save_rule(rule)
# Save the Rule and wait the end of indexing.
index.save_rule!(rule)
# Save the Rule, and forward it to all replicas of the index.
response = index.save_rule(rule, { forwardToReplicas: true })
# Browse rules
# https://www.algolia.com/doc/api-reference/api-methods/export-rules/
res = index.browse_rules()
# Store rules into an array
rules = res.map {|rule| rule}
if rules.empty?
puts "No rules are configured for your index yet."
else
# Create the json file with all rules
path = "#{ALGOLIA_INDEX_NAME}_rules.json"
File.open(path, 'wb') do |file|
file.write(JSON.generate(rules))
end
puts "Rules export completed"
end