Class: Typesensual::Collection
- Inherits:
-
Object
- Object
- Typesensual::Collection
- Includes:
- StateHelpers
- Defined in:
- lib/typesensual/collection.rb
Constant Summary collapse
- COLLECTION_NAME_PATTERN =
The pattern we use for collection names,
name:env@version
, wherename
is the name of the index,env
is the environment, andversion
is the timestamp of the collection's creation. If a name doesn't follow this pattern,name
collects everything. /^ (?<name>.*?) # the name of the collection cannot include : or @ (?::(?<env>.*?))? # the env is optional, but also cannot include : or @ (?:@(?<version>\d+))? # the version is also optional but must be an integer $/x.freeze
Class Method Summary collapse
-
.create!(collection) ⇒ Collection
Create a new collection using the given collection hash.
Instance Method Summary collapse
-
#create! ⇒ self
Creates the collection in Typesense.
-
#created_at ⇒ Time
The time the collection was created, as tracked by Typesense.
-
#default_sorting_field ⇒ String
The default sorting field for the collection.
-
#delete! ⇒ void
Deletes the collection in Typesense.
-
#enable_nested_fields? ⇒ Boolean
Whether the collection has nested fields enabled.
-
#env ⇒ String
The environment the collection is in, parsed from the Typesensual collection naming scheme.
-
#fields ⇒ Array<Field>
The fields in the collection.
-
#index_name ⇒ String
The name of the index, parsed from the Typesensual collection naming scheme.
-
#initialize(collection_or_name) ⇒ Collection
constructor
A new instance of Collection.
-
#insert_many!(docs, batch_size: 100) ⇒ Array<Hash>
Insert many documents into typesense.
-
#insert_one!(doc) ⇒ Boolean
Insert a single document into typesense.
-
#name ⇒ String
The raw, underlying name of the collection.
-
#num_documents ⇒ Integer
The number of documents in the collection.
-
#reload ⇒ self
Reload the underlying collection data from Typesense.
-
#remove_many!(filter_by:) ⇒ void
Remove multiple documents from typesense based on a filter.
-
#remove_one!(id) ⇒ void
Remove a single document from typesense.
-
#search(query:, query_by:) ⇒ Search
Search for documents in typesense.
-
#symbols_to_index ⇒ Array<String>
Special characters in strings which should be indexed as text.
-
#token_separators ⇒ Array<String>
Additional characters to be treated as separators when indexing text.
- #typesense_collection ⇒ Object
-
#version ⇒ String
The version of the collection, parsed from the Typesensual collection naming scheme.
Constructor Details
#initialize(collection) ⇒ Collection #initialize(name) ⇒ Collection
Returns a new instance of Collection.
41 42 43 44 45 46 47 |
# File 'lib/typesensual/collection.rb', line 41 def initialize(collection_or_name) @collection = if collection_or_name.is_a?(Hash) collection_or_name.deep_stringify_keys else client.collections[collection_or_name].retrieve end end |
Class Method Details
.create!(collection) ⇒ Collection
Create a new collection using the given collection hash
152 153 154 |
# File 'lib/typesensual/collection.rb', line 152 def self.create!(collection) new(collection).tap(&:create!) end |
Instance Method Details
#create! ⇒ self
Creates the collection in Typesense
131 132 133 134 |
# File 'lib/typesensual/collection.rb', line 131 def create! client.collections.create(@collection) self end |
#created_at ⇒ Time
The time the collection was created, as tracked by Typesense
58 59 60 |
# File 'lib/typesensual/collection.rb', line 58 def created_at @created_at ||= Time.strptime(@collection['created_at'].to_s, '%s') end |
#default_sorting_field ⇒ String
The default sorting field for the collection
64 65 66 |
# File 'lib/typesensual/collection.rb', line 64 def default_sorting_field @collection['default_sorting_field'] end |
#delete! ⇒ void
This method returns an undefined value.
Deletes the collection in Typesense
138 139 140 |
# File 'lib/typesensual/collection.rb', line 138 def delete! typesense_collection.delete end |
#enable_nested_fields? ⇒ Boolean
Whether the collection has nested fields enabled
70 71 72 |
# File 'lib/typesensual/collection.rb', line 70 def enable_nested_fields? @collection['enable_nested_fields'] end |
#env ⇒ String
The environment the collection is in, parsed from the Typesensual collection naming scheme.
117 118 119 |
# File 'lib/typesensual/collection.rb', line 117 def env parsed_name['env'] end |
#fields ⇒ Array<Field>
The fields in the collection
76 77 78 79 80 |
# File 'lib/typesensual/collection.rb', line 76 def fields @collection['fields'].map do |field| Field.new(field) end end |
#index_name ⇒ String
The name of the index, parsed from the Typesensual collection naming scheme.
109 110 111 |
# File 'lib/typesensual/collection.rb', line 109 def index_name parsed_name['name'] end |
#insert_many!(docs, batch_size: 100) ⇒ Array<Hash>
Insert many documents into typesense. Notably, the input can be an enumerable
or enumerator, which will be batched into groups of batch_size
and inserted
with the ID of any failed rows being provided in the response.
170 171 172 173 174 175 |
# File 'lib/typesensual/collection.rb', line 170 def insert_many!(docs, batch_size: 100) docs.lazy.each_slice(batch_size).with_object([]) do |slice, failures| results = typesense_collection.documents.import(slice, return_id: true, action: 'upsert') failures.push(*results.reject { |result| result['success'] }) end end |
#insert_one!(doc) ⇒ Boolean
Insert a single document into typesense
160 161 162 |
# File 'lib/typesensual/collection.rb', line 160 def insert_one!(doc) typesense_collection.documents.upsert(doc) end |
#name ⇒ String
The raw, underlying name of the collection
84 85 86 |
# File 'lib/typesensual/collection.rb', line 84 def name @collection['name'] end |
#num_documents ⇒ Integer
The number of documents in the collection
90 91 92 |
# File 'lib/typesensual/collection.rb', line 90 def num_documents @collection['num_documents'] end |
#reload ⇒ self
Reload the underlying collection data from Typesense
51 52 53 54 |
# File 'lib/typesensual/collection.rb', line 51 def reload @collection = client.collections[name].retrieve self end |
#remove_many!(filter_by:) ⇒ void
This method returns an undefined value.
Remove multiple documents from typesense based on a filter
189 190 191 |
# File 'lib/typesensual/collection.rb', line 189 def remove_many!(filter_by:) typesense_collection.documents.delete(filter_by: filter_by) end |
#remove_one!(id) ⇒ void
This method returns an undefined value.
Remove a single document from typesense
181 182 183 |
# File 'lib/typesensual/collection.rb', line 181 def remove_one!(id) typesense_collection.documents[id.to_s].delete end |
#search(query:, query_by:) ⇒ Search
Search for documents in typesense
198 199 200 201 202 203 204 |
# File 'lib/typesensual/collection.rb', line 198 def search(query:, query_by:) Search.new( collection: self, query: query, query_by: query_by ) end |
#symbols_to_index ⇒ Array<String>
Special characters in strings which should be indexed as text
96 97 98 |
# File 'lib/typesensual/collection.rb', line 96 def symbols_to_index @collection['symbols_to_index'] end |
#token_separators ⇒ Array<String>
Additional characters to be treated as separators when indexing text
102 103 104 |
# File 'lib/typesensual/collection.rb', line 102 def token_separators @collection['token_separators'] end |
#typesense_collection ⇒ Object
206 207 208 |
# File 'lib/typesensual/collection.rb', line 206 def typesense_collection @typesense_collection ||= client.collections[name] end |
#version ⇒ String
The version of the collection, parsed from the Typesensual collection naming scheme.
125 126 127 |
# File 'lib/typesensual/collection.rb', line 125 def version parsed_name['version'] end |