Class: Typesensual::Collection

Inherits:
Object
  • Object
show all
Includes:
StateHelpers
Defined in:
lib/typesensual/collection.rb

Constant Summary collapse

COLLECTION_NAME_PATTERN =

The pattern we use for collection names, name:env@version, where name is the name of the index, env is the environment, and version 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

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Collection #initialize(name) ⇒ Collection

Returns a new instance of Collection.

Overloads:

  • #initialize(collection) ⇒ Collection

    Initialize a new collection from a Typesense collection hash

    Parameters:

    • collection (Hash)

      the Typesense collection hash

      • created_at [Integer] the timestamp of the collection's creation
      • default_sorting_field [String] the default sorting field
      • enable_nested_fields [Boolean] whether nested fields are enabled
      • fields [Array] the fields in the collection
      • name [String] the name of the collection
      • num_documents [Integer] the number of documents in the collection
      • symbols_to_index [String] the symbols to index
      • token_separators [String] the token separators
  • #initialize(name) ⇒ Collection

    Initialize a new collection, loading info from Typesense

    Parameters:

    • name (String)

      the name of the collection

    Raises:

    • (Typesense::Error::ObjectNotFound)

      if the collection doesn't exist



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

Parameters:

  • collection (Hash)

    the Typesense collection hash

    • default_sorting_field [String] the default sorting field
    • enable_nested_fields [Boolean] whether nested fields are enabled
    • fields [Array] the fields in the collection
    • name [String] the name of the collection
    • symbols_to_index [String] the symbols to index
    • token_separators [String] the token separators

Returns:



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

Returns:

  • (self)


131
132
133
134
# File 'lib/typesensual/collection.rb', line 131

def create!
  client.collections.create(@collection)
  self
end

#created_atTime

The time the collection was created, as tracked by Typesense

Returns:

  • (Time)

    the time the collection was created



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_fieldString

The default sorting field for the collection

Returns:

  • (String)

    the default sorting field



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

Returns:

  • (Boolean)

    whether nested fields are enabled



70
71
72
# File 'lib/typesensual/collection.rb', line 70

def enable_nested_fields?
  @collection['enable_nested_fields']
end

#envString

The environment the collection is in, parsed from the Typesensual collection naming scheme.

Returns:

  • (String)

    the environment the collection is in

See Also:



117
118
119
# File 'lib/typesensual/collection.rb', line 117

def env
  parsed_name['env']
end

#fieldsArray<Field>

The fields in the collection

Returns:

  • (Array<Field>)

    the field information



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_nameString

The name of the index, parsed from the Typesensual collection naming scheme.

Returns:

  • (String)

    the name of the index

See Also:



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.

Parameters:

  • docs (Enumerable<Hash>)

    the documents to insert

Returns:

  • (Array<Hash>)

    any failed insertions



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

Parameters:

  • doc (Hash)

    the document to insert

Returns:

  • (Boolean)

    if the document was inserted successfully



160
161
162
# File 'lib/typesensual/collection.rb', line 160

def insert_one!(doc)
  typesense_collection.documents.upsert(doc)
end

#nameString

The raw, underlying name of the collection

Returns:

  • (String)

    the name of the collection



84
85
86
# File 'lib/typesensual/collection.rb', line 84

def name
  @collection['name']
end

#num_documentsInteger

The number of documents in the collection

Returns:

  • (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

#reloadself

Reload the underlying collection data from Typesense

Returns:

  • (self)


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

Parameters:

  • filter_by (String)

    the filter to use to remove documents



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

Parameters:

  • id (String)

    the ID of the document to remove



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

Parameters:

  • query (String)

    the query to search for

  • query_by (String)

    the fields to search by

Returns:

  • (Search)

    the search object



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_indexArray<String>

Special characters in strings which should be indexed as text

Returns:

  • (Array<String>)

    the symbols to index



96
97
98
# File 'lib/typesensual/collection.rb', line 96

def symbols_to_index
  @collection['symbols_to_index']
end

#token_separatorsArray<String>

Additional characters to be treated as separators when indexing text

Returns:

  • (Array<String>)

    the token separators



102
103
104
# File 'lib/typesensual/collection.rb', line 102

def token_separators
  @collection['token_separators']
end

#typesense_collectionObject



206
207
208
# File 'lib/typesensual/collection.rb', line 206

def typesense_collection
  @typesense_collection ||= client.collections[name]
end

#versionString

The version of the collection, parsed from the Typesensual collection naming scheme.

Returns:

  • (String)

    the version of the collection

See Also:



125
126
127
# File 'lib/typesensual/collection.rb', line 125

def version
  parsed_name['version']
end