Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script for exporting, creating and modifying rules #53

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ruby/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ALGOLIA_APP_ID=
ALGOLIA_API_KEY=
ALGOLIA_INDEX_NAME=
ALGOLIA_INDEX_NAME=
43 changes: 17 additions & 26 deletions ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
GEM
remote: https://rubygems.org/
specs:
algolia (2.1.1)
faraday (>= 0.15, < 2.0)
algolia (2.3.4)
faraday (>= 0.15, < 3)
faraday-net_http_persistent (>= 0.15, < 3)
multi_json (~> 1.0)
net-http-persistent
connection_pool (2.2.5)
dotenv (2.7.6)
faraday (1.8.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0.1)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
connection_pool (2.4.1)
dotenv (3.1.0)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
faraday-net_http_persistent (2.1.0)
faraday (~> 2.5)
net-http-persistent (~> 4.0)
multi_json (1.15.0)
multipart-post (2.1.1)
net-http-persistent (4.0.1)
net-http (0.4.1)
uri
net-http-persistent (4.0.2)
connection_pool (~> 2.2)
ruby2_keywords (0.0.5)
uri (0.13.0)

PLATFORMS
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
19 changes: 19 additions & 0 deletions ruby/backup_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Install the API client: https://www.algolia.com/doc/api-client/getting-started/install/ruby/?client=ruby
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)

# Retrieve records
puts 'Retrieving records...'
records = index.browse({})['hits']
puts "#{records.length} records retrieved"
73 changes: 73 additions & 0 deletions ruby/rules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Install the API client: https://www.algolia.com/doc/api-client/getting-started/install/ruby/?client=ruby
require 'dotenv/load'
require 'algolia'
# require 'date'

# Get your Algolia Application ID and (admin) API key from the dashboard: https://www.algolia.com/account/api-keys
# and choose a name for your index. Add these environment variables to a `.env` file:
ALGOLIA_APP_ID = ENV['ALGOLIA_APP_ID']
ALGOLIA_API_KEY = ENV['ALGOLIA_API_KEY']
ALGOLIA_INDEX_NAME = ENV['ALGOLIA_INDEX_NAME']

# Start the API client
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/
client = Algolia::Search::Client.create(ALGOLIA_APP_ID, ALGOLIA_API_KEY)

# Create an index (or connect to it, if an index with the name `ALGOLIA_INDEX_NAME` already exists)
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index
index = client.init_index(ALGOLIA_INDEX_NAME)

# Exporting the rules
# https://www.algolia.com/doc/api-reference/api-methods/export-rules/#examples
print "Original rules:\n"
all_rules = index.browse_rules()
all_rules.each { |rule| puts rule }
print "\n"

# Adding a new rule
# https://www.algolia.com/doc/api-reference/api-methods/save-rule/?client=php
rule_id = 'a-rule-id'
print "Adding new rule: #{rule_id}\n"

rule = {
objectID: rule_id,
conditions: [{
pattern: 'dress',
anchoring: 'contains',
# Uncomment line 38 if the pattern should match plurals, synonyms, and typos.
# alternatives: true
}],
consequence: {
params: {
filters: 'subCategory:Dress'
}
}
}

# Uncomment line 48 to turn the rule off
# rule['enabled'] = false

# Uncomment lines 51 - 56 to add valid time ranges (also uncomment `require 'date'` on line 4)
# 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 forward it to all replicas of the index.
# index.save_rule(rule, { forwardToReplicas: true })

print "#{rule_id} added successfully\n"
print "\n"

# Exporting the modified rules
# https://www.algolia.com/doc/api-reference/api-methods/export-rules/
print "Modified rules:\n"
all_modified_rules = index.browse_rules()
all_modified_rules.each { |rule| puts rule }
print "\n"