Skip to content

Commit

Permalink
CDC #198 - Updating to use "uuid" value for Typesense document ID; Up…
Browse files Browse the repository at this point in the history
…dating indexing process to use "emplace" action instead of "upsert"; Updating indexing process to delete records from the index not included in the batch
  • Loading branch information
dleadbetter committed May 1, 2024
1 parent 6d73ee1 commit 036c4b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ bundle exec rake typesense:create -- -h host -p port -r protocol -a api_key -c c
bundle exec rake typesense:delete -- -h host -p port -r protocol -a api_key -c collection_name
```

#### Add documents to a collection
#### Index documents into a collection
```bash
bundle exec rake typesense:index -- -h host -p port -r protocol -a api_key -c collection_name -m model_ids
```

**Note:** This task expects the entire collection to be indexed. Any records not included in the batch will be removed from the index.

#### Update a collection
```bash
bundle exec rake typesense:update -- -h host -p port -r protocol -a api_key -c collection_name
```

**Note**: This task was added as a workaround for an issue in Typesense indexing nested facetable fields using auto-detection schema. This task should be run _after_ the indexing process to update the "facet" attribute on any fields that should be facetable.
**Note:** This task was added as a workaround for an issue in Typesense indexing nested facetable fields using auto-detection schema. This task should be run _after_ the indexing process to update the "facet" attribute on any fields that should be facetable.


## Public API
Expand Down
8 changes: 3 additions & 5 deletions app/services/core_data_connector/search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,11 @@ def search_attributes
.where(ProjectModelRelationship.arel_table.name => { allow_inverse: true })
}, as: :related_record, class_name: Relationship.to_s

# Include the ID attributes as a string by default
search_attribute(:id) { uuid }
search_attribute(:record_id) { id.to_s }
search_attribute :uuid

# Include the ID attribute as a string by default
search_attribute(:record_id) do
id.to_s
end

# Uses the specified attributes to create a JSON object. We'll skip relationships by default as to
# not create an infinite loop while serializing related records.
def to_search_json(skip_relationships = true)
Expand Down
11 changes: 9 additions & 2 deletions lib/typesense/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,23 @@ def index(project_model_ids)
hash
end

# Append a unique import_id to all of the documents indexed in this batch
import_id = DateTime.now.to_i
import_attributes = { import_id: import_id }

# Iterate over the keys and query the records belonging to each project model
model_classes.keys.each do |model_class|
klass = model_class.constantize
ids = model_classes[model_class]

klass.for_search(ids) do |records|
documents = records.map { |r| r.to_search_json(false) }
collection.documents.import(documents, action: 'upsert')
documents = records.map { |r| r.to_search_json(false).merge(import_attributes) }
collection.documents.import(documents, action: 'emplace')
end
end

# Delete any records from the index not included in this batch
collection.documents.delete(filter_by: "import_id:!=#{import_id}")
end

def update
Expand Down

0 comments on commit 036c4b1

Please sign in to comment.