Skip to content

Commit

Permalink
Add boosted_category_id column and add to Algolia index. (#748)
Browse files Browse the repository at this point in the history
This adds a `boosted_category_id` column to Services, which is a
nullable foreign key to the Categories table. This is intended to be
used by the frontend code through Algolia as a possible target of the
optional filters feature of Algolia, allowing us to boost search
rankings for specific services on specific pages.

This column is added to the Algolia index as `boosted_category`, which
holds the string name of the category, and we also add it to the
`attributesForFaceting` list so that it can be used for optional
filters.

Finally, we change the Algolia index ranking order from its default by
placing `geo` after `filters`. This is needed in order for optional
filters to have higher precedence than geolocation.
  • Loading branch information
richardxia authored Aug 15, 2024
1 parent 0296659 commit 3a9c0af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Service < ActiveRecord::Base

belongs_to :resource, required: true, touch: true
belongs_to :program
belongs_to :boosted_category, class_name: 'Category'
has_many :notes, dependent: :destroy
has_many :feedbacks, dependent: :destroy
has_many :textings
Expand Down Expand Up @@ -37,7 +38,11 @@ class Service < ActiveRecord::Base
index_name: "#{Rails.configuration.x.algolia.index_prefix}_services_search",
id: :algolia_id do
# specify the list of attributes available for faceting
attributesForFaceting %i[categories open_times eligibilities associated_sites type]
attributesForFaceting %i[categories open_times eligibilities associated_sites type boosted_category]

# Default has "geo" before "filters", but we need it after if we want
# optional filters to have higher priority than distance.
ranking %w[typo words filters geo proximity attribute exact custom]

# Define attributes used to build an Algolia record
add_attribute :status
Expand Down Expand Up @@ -127,6 +132,10 @@ class Service < ActiveRecord::Base
categories.map(&:name)
end

add_attribute :boosted_category do
boosted_category&.name
end

add_attribute :use_resource_schedule

add_attribute :eligibilities do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240804222349_add_boosted_category_to_services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBoostedCategoryToServices < ActiveRecord::Migration[6.1]
def change
add_reference :services, :boosted_category, foreign_key: { to_table: :categories }
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_02_155441) do
ActiveRecord::Schema.define(version: 2024_08_04_222349) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -418,6 +418,8 @@
t.integer "source_attribution", default: 0
t.text "internal_note"
t.string "short_description"
t.bigint "boosted_category_id"
t.index ["boosted_category_id"], name: "index_services_on_boosted_category_id"
t.index ["contact_id"], name: "index_services_on_contact_id"
t.index ["funding_id"], name: "index_services_on_funding_id"
t.index ["program_id"], name: "index_services_on_program_id"
Expand Down Expand Up @@ -521,6 +523,7 @@
add_foreign_key "schedule_days", "schedules"
add_foreign_key "schedules", "resources"
add_foreign_key "schedules", "services"
add_foreign_key "services", "categories", column: "boosted_category_id"
add_foreign_key "services", "contacts"
add_foreign_key "services", "fundings"
add_foreign_key "services", "programs"
Expand Down

0 comments on commit 3a9c0af

Please sign in to comment.