Skip to content

Commit

Permalink
CDC #357 - Updating search module to include "coordinates" field for …
Browse files Browse the repository at this point in the history
…related place records
  • Loading branch information
dleadbetter committed Jan 23, 2025
1 parent a582d5d commit 3cdb2b8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/models/core_data_connector/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Relationship < ApplicationRecord
belongs_to :inverse_related_taxonomy, -> { where(Relationship.arel_table.name => { primary_record_type: Taxonomy.to_s }) }, class_name: Taxonomy.to_s, foreign_key: :primary_record_id, optional: true
belongs_to :inverse_related_work, -> { where(Relationship.arel_table.name => { primary_record_type: Work.to_s }) }, class_name: Work.to_s, foreign_key: :primary_record_id, optional: true

# Place relationships
belongs_to :related_place_with_centroid, -> { merge(Place.with_centroid) }, class_name: Place.to_s, foreign_key: :related_record_id, optional: true
belongs_to :inverse_related_place_with_centroid, -> { merge(Place.with_centroid) }, class_name: Place.to_s, foreign_key: :primary_record_id, optional: true

# Delegates
delegate :project_id, to: :project_model_relationship

Expand Down
47 changes: 40 additions & 7 deletions app/services/core_data_connector/search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def apply_preloads(records, project_model_ids)
project_model_relationship: [:user_defined_fields, primary_model: :project]
],
place_relationships: [
related_record: [
related_place_with_centroid: [
:place_geometry,
:primary_name,
:place_names,
Expand Down Expand Up @@ -150,7 +150,7 @@ def apply_preloads(records, project_model_ids)
project_model_relationship: [:user_defined_fields, related_model: :project]
],
place_related_relationships: [
primary_record: [
inverse_related_place_with_centroid: [
:place_geometry,
:primary_name,
:place_names,
Expand Down Expand Up @@ -186,9 +186,6 @@ def for_search(project_model_ids, &block)
# Base query
query = all_records_by_project_model(project_model_ids)

# Concrete class query
query = search_query(query) if self.respond_to?(:search_query)

query.find_in_batches(batch_size: 1000) do |records|
# Apply the preloads for the current batch
apply_preloads records, project_model_ids
Expand Down Expand Up @@ -408,6 +405,24 @@ def build_event_range(hash)
hash['event_range_facet'] = [min_range, max_range] unless min_range.nil? || max_range.nil?
end

def build_inverse_place_relationship(relationship, hash, projects)
project_model_relationship = relationship.project_model_relationship
key = project_model_relationship.uuid

# Add the related model project to the list of projects
add_project(projects, project_model_relationship.related_model.project)

user_defined = build_user_defined(relationship, project_model_relationship.user_defined_fields)

hash[key] ||= []

hash[key] << relationship
.inverse_related_place_with_centroid
.to_search_json
.merge(user_defined)
.merge({ inverse: true })
end

def build_inverse_relationship(relationship, hash, projects)
project_model_relationship = relationship.project_model_relationship
key = project_model_relationship.uuid
Expand All @@ -426,6 +441,24 @@ def build_inverse_relationship(relationship, hash, projects)
.merge({ inverse: true })
end

def build_place_relationship(relationship, hash, projects)
project_model_relationship = relationship.project_model_relationship
key = project_model_relationship.uuid

# Add the primary model project to the list of projects
add_project(projects, project_model_relationship.primary_model.project)

user_defined = build_user_defined(relationship, project_model_relationship.user_defined_fields)

hash[key] ||= []

hash[key] << relationship
.related_place_with_centroid
.to_search_json
.merge(user_defined)
.merge({ inverse: false })
end

def build_relationship(relationship, hash, projects)
project_model_relationship = relationship.project_model_relationship
key = project_model_relationship.uuid
Expand All @@ -451,7 +484,7 @@ def build_relationships(hash, projects)
media_content_relationships.each { |r| build_relationship(r, hash, projects) }
organization_relationships.each { |r| build_relationship(r, hash, projects) }
person_relationships.each { |r| build_relationship(r, hash, projects) }
place_relationships.each { |r| build_relationship(r, hash, projects) }
place_relationships.each { |r| build_place_relationship(r, hash, projects) }
taxonomy_relationships.each { |r| build_relationship(r, hash, projects) }
work_relationships.each { |r| build_relationship(r, hash, projects) }

Expand All @@ -461,7 +494,7 @@ def build_relationships(hash, projects)
media_content_related_relationships.each { |r| build_inverse_relationship(r, hash, projects) }
organization_related_relationships.each { |r| build_inverse_relationship(r,hash, projects) }
person_related_relationships.each { |r| build_inverse_relationship(r, hash, projects) }
place_related_relationships.each { |r| build_inverse_relationship(r, hash, projects) }
place_related_relationships.each { |r| build_inverse_place_relationship(r, hash, projects) }
taxonomy_related_relationships.each { |r| build_inverse_relationship(r, hash, projects) }
work_related_relationships.each { |r| build_inverse_relationship(r, hash, projects) }
end
Expand Down
10 changes: 1 addition & 9 deletions app/services/core_data_connector/search/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ module Place
def preloads
[:primary_name, :place_names, :place_geometry]
end

def search_query(query)
query.merge(self.with_centroid)
end
end

included do
Expand All @@ -29,12 +25,8 @@ def search_query(query)
end

search_attribute(:coordinates) do
# Since the "geometry_center" attribute is defined in the select statement above, ensure that it exists
# before calling.
next unless self.respond_to?(:geometry_center)

# Return if the "geometry_center" attribute is not defined
next unless geometry_center.present?
next unless self.respond_to?(:geometry_center) && geometry_center.present?

[geometry_center.y, geometry_center.x]
end
Expand Down
18 changes: 8 additions & 10 deletions lib/typesense/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,15 @@ def update
fields = collection['fields']

# Update any fields where the name ends with "_facet" and are not flagged as facetable.
# Update any fields named "coordinates" to type "geopoint"
fields.each do |field|
next unless field['name'].end_with?('_facet') && !field['facet']

collection_schema.update({
fields: [{
name: field['name'],
drop: true
},
field.merge({ facet: true })
]
})
name = field['name']

if name.end_with?('_facet') && !field['facet']
collection_schema.update({ fields: [{ name: name, drop: true }, field.merge({ facet: true })] })
elsif name.include?('coordinates') && !name.include?('geometry') && field['type'] != 'geopoint[]'
collection_schema.update({ fields: [{ name: name, drop: true }, field.merge({ type: 'geopoint[]', sort: true })] })
end
end
end
end
Expand Down

0 comments on commit 3cdb2b8

Please sign in to comment.