Sha256: 8d46f2168d98b96745eb0e20df958a347e76febe7af463137c98facbec875dc9

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

##
# These examples walk you through operations specifically related to aliases
# # This is a Typesense Premium feature (see: https://typesense.org/premium)
# Be sure to add `--license-key=<>` as a parameter, when starting a Typesense Premium server

require_relative './client_initialization'

# Create a collection
create_response = @typesense.collections.create(
  "name": 'books_january',
  "fields": [
    { "name": 'title', "type": 'string' },
    { "name": 'authors', "type": 'string[]' },
    { "name": 'authors_facet', "type": 'string[]', "facet": true },
    { "name": 'publication_year', "type": 'int32' },
    { "name": 'publication_year_facet', "type": 'string', "facet": true },
    { "name": 'ratings_count', "type": 'int32' },
    { "name": 'average_rating', "type": 'float' },
    { "name": 'image_url', "type": 'string' }
  ],
  "default_sorting_field": 'ratings_count'
)

ap create_response

# Create or update an existing alias
create_alias_response = @typesense.aliases.upsert('books',
                                                  "collection_name": 'books_january')
ap create_alias_response

# Add a book using the alias name `books`
hunger_games_book = {
  'id': '1', 'original_publication_year': 2008, 'authors': ['Suzanne Collins'], 'average_rating': 4.34,
  'publication_year': 2008, 'publication_year_facet': '2008', 'authors_facet': ['Suzanne Collins'],
  'title': 'The Hunger Games',
  'image_url': 'https://images.gr-assets.com/books/1447303603m/2767052.jpg',
  'ratings_count': 4_780_653
}

@typesense.collections['books'].documents.create(hunger_games_book)

# Search using the alias
ap @typesense.collections['books'].documents.search(
  'q': 'hunger',
  'query_by': 'title',
  'sort_by': 'ratings_count:desc'
)

# List all aliases
ap @typesense.aliases.retrieve

# Retrieve the configuration of a specific alias
ap @typesense.aliases['books'].retrieve

# Delete an alias
ap @typesense.aliases['books'].delete

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
typesense-0.9.0 examples/aliases.rb
typesense-0.8.0 examples/aliases.rb
typesense-0.8.0pre examples/aliases.rb
typesense-0.7.0 examples/aliases.rb
typesense-0.7.0.pre1 examples/aliases.rb
typesense-0.7.0.pre0 examples/aliases.rb
typesense-0.6.0 examples/aliases.rb
typesense-0.5.5 examples/aliases.rb
typesense-0.5.4 examples/aliases.rb
typesense-0.5.3 examples/aliases.rb
typesense-0.5.2 examples/aliases.rb
typesense-0.5.1 examples/aliases.rb
typesense-0.5.0 examples/aliases.rb
typesense-0.4.0 examples/aliases.rb