### Using Environment Variables
Source: https://typesense.org/docs/29.0/api/server-configuration.md
Example of starting the Typesense server using environment variables for configuration.
```bash
TYPESENSE_DATA_DIR=/var/lib/typesense TYPESENSE_API_KEY=AS3das2awQ2 ./typesense-server
```
--------------------------------
### Using a Configuration File (Command Line)
Source: https://typesense.org/docs/29.0/api/server-configuration.md
Example of starting the Typesense server using a configuration file specified via a command-line argument.
```bash
./typesense-server --config=/etc/typesense/typesense-server.ini
```
--------------------------------
### Go Client Installation and Usage
Source: https://typesense.org/docs/29.0/api/api-clients.md
Command to get the Typesense Go client library and the necessary import statements for Go code.
```go
// $ go get github.com/typesense/typesense-go/v3/typesense
import (
"github.com/typesense/typesense-go/v3/typesense"
"github.com/typesense/typesense-go/v3/typesense/api"
"github.com/typesense/typesense-go/v3/typesense/api/pointer"
)
```
--------------------------------
### Go Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a schema with an auto-embedding field using Go.
```go
schema := &api.CollectionSchema{
Name: "products",
Fields: []api.Field{
{
Name: "product_name",
Type: "string",
},
{
Name: "categories",
Type: "string[]",
},
{
Name: "embedding",
Type: "float[]",
Embed: &struct {
From []string "json:\"from\""
ModelConfig struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
RefreshToken *string "json:\"refresh_token,omitempty\""
Url *string "json:\"url,omitempty\""
} "json:\"model_config\""
}{
From: []string{"product_name", "categories"},
ModelConfig: struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
RefreshToken *string "json:\"refresh_token,omitempty\""
Url *string "json:\"url,omitempty\""
}{
ModelName: "ts/e5-small",
},
},
}
}
}
```
--------------------------------
### Configuration File Example (INI)
Source: https://typesense.org/docs/29.0/api/server-configuration.md
Example of the Typesense server configuration file in INI format.
```ini
; /etc/typesense/typesense-server.ini
[server]
api-key = Rhsdhas2asasdasj2
data-dir = /var/lib/typesense
log-dir = /var/log/typesense
api-port = 9090
```
--------------------------------
### Getting all stopwords sets
Source: https://typesense.org/docs/29.0/api/stopwords.md
Example of fetching all available stopword sets.
```shell
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/stopwords"
```
--------------------------------
### Multi-search for Query Suggestions (Go)
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of performing a multi-search to get both query suggestions and actual search results side-by-side.
```go
searchRequests := api.MultiSearchSearchesParameter{
Searches: []api.MultiSearchCollectionParameters{
{
Collection: pointer.Any("product_queries"),
Q: pointer.Any("shoe"),
QueryBy: pointer.Any("q"),
},
{
Collection: pointer.Any("products"),
Q: pointer.Any("shoe"),
QueryBy: pointer.Any("product_name"),
},
},
}
client.MultiSearch.Perform(context.Background(), &api.MultiSearchParams{}, searchRequests)
```
--------------------------------
### PHP Client Installation
Source: https://typesense.org/docs/29.0/api/api-clients.md
Command to install the Typesense PHP client library using Composer.
```shell
composer require php-http/curl-client typesense/typesense-php
```
--------------------------------
### Ruby Client Installation
Source: https://typesense.org/docs/29.0/api/api-clients.md
Command to install the Typesense Ruby client library using gem.
```shell
gem install typesense
```
--------------------------------
### Go Example
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of creating a 'no_hits_queries' analytics rule using the Go client.
```go
ruleName := "product_no_hits"
ruleConfiguration := &api.AnalyticsRuleUpsertSchema{
Type: api.AnalyticsRuleUpsertSchemaTypeNohitsQueries,
Params: api.AnalyticsRuleParameters{
Source: api.AnalyticsRuleParametersSource{
Collections: []string{"products"},
},
Destination: api.AnalyticsRuleParametersDestination{
Collection: "no_hits_queries",
},
Limit: pointer.Int(1000),
},
}
client.Analytics().Rules().Upsert(context.Background(), ruleName, ruleConfiguration)
```
--------------------------------
### Python Client Installation
Source: https://typesense.org/docs/29.0/api/api-clients.md
Command to install the Typesense Python client library using pip.
```shell
pip install typesense
```
--------------------------------
### Go Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a collection with an embedding field using an OpenAI-compatible API in Go.
```go
schema := &api.CollectionSchema{
Name: "products",
Fields: []api.Field{
{
Name: "product_name",
Type: "string",
},
{
Name: "embedding",
Type: "float[]",
Embed: &struct {
From []string "json:\"from\""
ModelConfig struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
RefreshToken *string "json:\"refresh_token,omitempty\""
Url *string "json:\"url,omitempty\""
} "json:\"model_config\""
}{
From: []string{"product_name"},
ModelConfig: struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
```
--------------------------------
### JavaScript Client Installation
Source: https://typesense.org/docs/29.0/api/api-clients.md
Instructions for installing the Typesense JavaScript client library for use in a browser environment.
```javascript
// npm install typesense @babel/runtime
// Browser
```
--------------------------------
### Java Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a schema with an auto-embedding field using Java.
```java
CollectionSchema collectionSchema = new CollectionSchema();
ArrayList embedFrom = new ArrayList<>();
embedFrom.add("product_name");
embedFrom.add("categories");
collectionschema.name("products")
.addFieldsItem(new Field().name("product_name").type(FieldTypes.STRING))
.addFieldsItem(new Field().name("categories").type(FieldTypes.STRING_ARRAY))
.addFieldsItem(new Field().name("embedding").type(FieldTypes.FLOAT_ARRAY).embed(
new FieldEmbed().from(embedFrom).modelConfig(new FieldEmbedModelConfig().modelName("ts/e5-small"))
));
CollectionResponse collectionResponse = client.collections().create(collectionSchema);
```
--------------------------------
### Python Example
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of creating a 'no_hits_queries' analytics rule using the Python client.
```python
let ruleName = 'product_no_hits'
let ruleConfiguration = {
"type": "nohits_queries",
"params": {
"source": {
"collections": ["products"]
},
"destination": {
"collection": "no_hits_queries"
},
"limit": 1000
}
}
client.analytics.rules.upsert(ruleName, ruleConfiguration)
```
--------------------------------
### Python Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a schema with an auto-embedding field using Python.
```python
schema = {
"name": "products",
"fields": [
{
"name" : "product_name",
"type" : "string"
},
{
"name" : "categories",
"type" : "string[]"
},
{
"name" : "embedding",
"type" : "float[]",
"embed": {
"from": [
"product_name",
"categories"
],
"model_config": {
"model_name": "ts/e5-small"
}
}
}
]
}
client.collections.create(schema)
```
--------------------------------
### Multi-search for Query Suggestions (Python)
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of performing a multi-search to get both query suggestions and actual search results side-by-side.
```python
let searchRequests = {
'searches': [
{
'collection': 'product_queries',
'q': 'shoe',
'query_by': 'q'
},
{
'collection': 'products',
'q': 'shoe',
'query_by': 'product_name'
}
]
}
client.multi_search.perform(searchRequests, {})
```
--------------------------------
### Multi-search for Query Suggestions (Ruby)
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of performing a multi-search to get both query suggestions and actual search results side-by-side.
```rb
search_requests = {
'searches' => [
{
'collection' => 'product_queries',
'q' => 'shoe',
'query_by' => 'q'
},
{
'collection' => 'products',
'q' => 'shoe',
'query_by' => 'q'
}
]
}
client.multi_search.perform(search_requests, {})
```
--------------------------------
### Ruby Example
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of creating a 'no_hits_queries' analytics rule using the Ruby client.
```ruby
rule_name = 'product_no_hits'
rule_configuration = {
'type' => 'nohits_queries',
'params' => {
'source' => {
'collections' => ['products']
},
'destination' => {
'collection' => 'no_hits_queries'
},
'limit' => 1000
}
}
typesense.analytics.rules.upsert(rule_name, rule_configuration)
```
--------------------------------
### Go Schema Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a Typesense collection schema with embedding configuration including indexing and query prefixes in Go.
```go
schema := &api.CollectionSchema{
Name: "products",
Fields: []api.Field{
{Name: "product_name", Type: "string"},
{Name: "embedding", Type: "float[]",
Embed: &struct {
From []string "json:\"from\""
ModelConfig struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
RefreshToken *string "json:\"refresh_token,omitempty\""
Url *string "json:\"url,omitempty\""
} "json:\"model_config\""
}{
From: []string{"product_name"},
ModelConfig: struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
RefreshToken *string "json:\"refresh_token,omitempty\""
Url *string "json:\"url,omitempty\""
}{
ModelName: "e5-base",
```
--------------------------------
### Ruby Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a schema with an auto-embedding field using Ruby.
```ruby
schema = {
"name" => "products",
"fields" => [
{
"name" => "product_name",
"type" => "string"
},
{
"name" => "categories",
"type" => "string[]"
},
{
"name" => "embedding",
"type" => "float[]",
"embed" => {
"from" => [
"product_name",
"categories"
],
"model_config" => {
"model_name" => "ts/e5-small"
}
}
}
]
}
client.collections.create(schema)
```
--------------------------------
### Ruby Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of setting up a multi-search request with a vector query in Ruby. It shows the structure for specifying the collection, query, vector field, and the `k` parameter for controlling the number of results. The `exclude_fields` parameter is also demonstrated.
```ruby
search_requests = {
'searches': [
{
'collection' => 'docs',
'q' => '*',
'vector_query' => 'embedding:([0.96826, 0.94, 0.39557, 0.306488], k:100)',
'exclude_fields' => 'embedding'
}
]
}
```
--------------------------------
### Vector Query Parameters Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
An example of how to use various vector search parameters within the `vector_query` parameter.
```json
{
"vector_query": "vector_field_name:([], k: 100, alpha: 0.8, distance_threshold:0.30)"
}
```
--------------------------------
### Java Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a collection with an embedding field using an OpenAI-compatible API in Java.
```java
CollectionSchema collectionSchema = new CollectionSchema();
ArrayList embedFrom = new ArrayList<>();
embedFrom.add("product_name");
embedFrom.add("categories");
collectionschema.name("products")
.addFieldsItem(new Field().name("product_name").type(FieldTypes.STRING))
.addFieldsItem(new Field().name("categories").type(FieldTypes.STRING_ARRAY))
.addFieldsItem(new Field().name("embedding").type(FieldTypes.FLOAT_ARRAY).embed(
new FieldEmbed().from(embedFrom).modelConfig(new FieldEmbedModelConfig().modelName("openai/text-embedding-ada-002").apiKey("your_api_key_as_required_by_the_custom_provider").url("https://your-custom-openai-compatible-api.domain.com")
));
CollectionResponse collectionResponse = client.collections().create(collectionSchema);
```
--------------------------------
### PHP Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a schema with an auto-embedding field using PHP.
```php
$schema = [
"name" => "products",
"fields" => [
[
"name" => "product_name",
"type" => "string"
],
[
"name" => "categories",
"type" => "string[]"
],
[
"name" => "embedding",
"type" => "float[]",
"embed" => [
"from" => [
"product_name",
"categories"
],
"model_config" => [
"model_name" => "ts/e5-small"
]
]
]
]
];
$client->collections->create($schema);
```
--------------------------------
### JavaScript Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a multi-search with a vector query in JavaScript. It demonstrates how to specify the collection, query, vector field, and control the number of results with `k`. It also shows how to exclude the vector field from the response to save bandwidth.
```javascript
let searchRequests = {
'searches': [
{
'collection': 'docs',
'q': '*',
'vector_query' : 'embedding:([0.96826, 0.94, 0.39557, 0.306488], k:100)',
'exclude_fields': 'embedding'
}
]
}
let commonSearchParams = {}
client.multiSearch.perform(searchRequests, commonSearchParams)
```
--------------------------------
### Ruby Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a collection with an embedding field using an OpenAI-compatible API in Ruby.
```ruby
schema = {
"name" => "products",
"fields" => [
{
"name" => "product_name",
"type" => "string"
},
{
"name" => "embedding",
"type" => "float[]",
"embed" => {
"from" => [
"product_name"
],
"model_config" => {
"model_name" => "openai/text-embedding-ada-002",
"api_key" => "your_api_key_as_required_by_the_custom_provider",
"url" => "https://your-custom-openai-compatible-api.domain.com"
}
}
}
]
}
client.collections.create(schema)
```
--------------------------------
### cURL Example
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of creating a 'no_hits_queries' analytics rule using cURL.
```bash
curl -k "http://localhost:8108/analytics/rules" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
-d '{ "name": "product_no_hits", "type": "nohits_queries", "params": { "source": { "collections": ["products"] }, "destination": { "collection": "no_hits_queries" }, "limit": 1000 } }'
```
--------------------------------
### Python Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a collection with an embedding field using an OpenAI-compatible API in Python.
```python
schema = {
"name": "products",
"fields": [
{
"name" : "product_name",
"type" : "string"
},
{
"name" : "embedding",
"type" : "float[]",
"embed": {
"from": [
"product_name"
],
"model_config": {
"model_name": "openai/text-embedding-ada-002",
"api_key": "your_api_key_as_required_by_the_custom_provider",
"url": "https://your-custom-openai-compatible-api.domain.com"
}
}
}
]
}
client.collections.create(schema)
```
--------------------------------
### Go Semantic Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a semantic search using the Typesense Go client.
```go
searchParameters := &api.SearchCollectionParams{
Q: pointer.Any("chair"),
QueryBy: pointer.Any("embedding"),
}
client.Collection("products").Documents().Search(context.Background(), searchParameters)
```
--------------------------------
### PHP Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a collection with an embedding field using an OpenAI-compatible API in PHP.
```php
$schema = [
"name" => "products",
"fields" => [
[
"name" => "product_name",
"type" => "string"
],
[
"name" => "embedding",
"type" => "float[]",
"embed" => [
"from" => [
"product_name"
],
"model_config" => [
"model_name" => "openai/text-embedding-ada-002",
"api_key" => "your_api_key_as_required_by_the_custom_provider",
"url" => "https://your-custom-openai-compatible-api.domain.com"
]
]
]
]
];
$client->collections->create($schema);
```
--------------------------------
### Python Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a multi-search with a vector query in Python. This snippet illustrates how to define the search parameters, including the collection, query, vector field, and the `k` parameter. It also demonstrates excluding the vector field from the results.
```python
search_requests = {
'searches': [
{
'collection': 'docs',
'q' : '*',
'vector_query': 'embedding:([0.96826, 0.94, 0.39557, 0.306488], k:100)',
'exclude_fields': 'embedding'
}
]
}
common_search_params = {}
client.multi_search.perform(search_requests, common_search_params)
```
--------------------------------
### Go
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a multi-search with a vector query in Go.
```go
searchRequests := api.MultiSearchSearchesParameter{
Searches: []api.MultiSearchCollectionParameters{
{
Collection: pointer.Any("docs"),
Q: pointer.Any("*"),
VectorQuery: pointer.Any("embedding:([0.96826, 0.94, 0.39557, 0.306488], k:100)"), // <=== Be sure to replace `embedding` with the name of the field that stores your embeddings.
ExcludeFields: pointer.Any("embedding"), // <=== Don't return the raw floating point numbers in the vector field in the search API response, to save on network bandwidth.
},
},
}
commonSearchParams := &api.MultiSearchParams{}
client.MultiSearch.Perform(context.Background(), commonSearchParams, searchRequests)
```
--------------------------------
### JavaScript Example
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of creating a 'no_hits_queries' analytics rule using the JavaScript client.
```javascript
let ruleName = 'product_no_hits'
let ruleConfiguration = {
"type": "nohits_queries",
"params": {
"source": {
"collections": ["products"]
},
"destination": {
"collection": "no_hits_queries"
},
"limit": 1000
}
}
client.analytics.rules().upsert(ruleName, ruleConfiguration)
```
--------------------------------
### Python Schema Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a Typesense collection schema with embedding configuration including indexing and query prefixes in Python.
```python
schema = {
"name": "products",
"fields": [
{
"name" : "product_name",
"type" : "string"
},
{
"name" : "embedding",
"type" : "float[]",
"embed": {
"from": ["product_name"],
"model_config": {
"model_name": "e5-base",
"indexing_prefix": "passage:",
"query_prefix": "query:"
}
}
}
]
}
client.collections.create(schema)
```
--------------------------------
### Ruby Schema Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a Typesense collection schema with embedding configuration including indexing and query prefixes in Ruby.
```ruby
schema = {
"name" => "products",
"fields" => [
{
"name" => "product_name",
"type" => "string"
},
{
"name" => "embedding",
"type" => "float[]",
"embed" => [
"from" => ["product_name"],
"model_config" => [
"model_name" => "e5-base",
"indexing_prefix" => "passage:",
"query_prefix" => "query:"
]
}
]
}
client.collections.create(schema)
```
--------------------------------
### Java Schema Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a Typesense collection schema with embedding configuration including indexing and query prefixes in Java.
```java
CollectionSchema collectionSchema = new CollectionSchema();
ArrayList embedFrom = new ArrayList<>();
embedFrom.add("product_name");
embedFrom.add("categories");
collectionschema.name("products")
.addFieldsItem(new Field().name("product_name").type(FieldTypes.STRING))
.addFieldsItem(new Field().name("categories").type(FieldTypes.STRING_ARRAY))
.addFieldsItem(new Field().name("embedding").type(FieldTypes.FLOAT_ARRAY).embed(
new FieldEmbed().from(embedFrom).modelConfig(new FieldEmbedModelConfig().modelName("e5-base").indexingPrefix("passage:").queryPrefix("query:")
));
CollectionResponse collectionResponse = client.collections().create(collectionSchema);
```
--------------------------------
### PHP Schema Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a Typesense collection schema with embedding configuration including indexing and query prefixes in PHP.
```php
$schema = [
"name" => "products",
"fields" => [
[
"name" => "product_name",
"type" => "string"
],
[
"name" => "embedding",
"type" => "float[]",
"embed" => [
"from" => ["product_name"],
"model_config" => [
"model_name" => "e5-base",
"indexing_prefix" => "passage:",
"query_prefix" => "query:"
]
]
]
]
];
$client->collections->create($schema);
```
--------------------------------
### PHP Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a multi-search with a vector query in PHP. It shows how to structure the search request, including the collection, query, vector field, and the `k` parameter for result count. It also includes excluding the vector field from the response.
```php
$searchRequests = [
'searches' => [
[
'collection' => 'docs',
'q' => '*',
'vector_query' => 'embedding:([0.96826, 0.94, 0.39557, 0.306488], k:100)',
'exclude_fields' => 'embedding'
]
]
];
$commonSearchParams = [];
$client->multiSearch->perform($searchRequests, $commonSearchParams);
```
--------------------------------
### cURL Command
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example cURL command to create a collection.
```bash
curl 'http://localhost:8108/collections' \
-X POST \
```
--------------------------------
### Sample Response for Scoped Search Key
Source: https://typesense.org/docs/29.0/api/api-keys.md
Example of a generated scoped API key.
```text
OW9DYWZGS1Q1RGdSbmo0S1QrOWxhbk9PL2kxbTU1eXA3bCthdmE5eXJKRT1STjIzeyJmaWx0ZXJfYnkiOiJjb21wYW55X2lkOjEyNCIsImV4cGlyZXNfYXQiOjE5MDYwNTQxMDZ9
```
--------------------------------
### Hybrid Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
This example demonstrates how to perform a hybrid search by querying both a regular field ('product_name') and an embedding field ('embedding'). Typesense combines keyword and semantic search results using Rank Fusion.
```javascript
let search_parameters = {
'q' : 'chair',
'query_by' : 'product_name,embedding',
'sort_by' : '_text_match:desc',
}
client.collections('products').documents().search(search_parameters)
```
```php
$search_parameters = [
'q' => 'chair',
'query_by' => 'product_name,embedding',
'sort_by' => '_text_match:desc',
];
$client->collections['products']->documents->search($search_parameters);
```
```python
search_parameters = {
'q' : 'chair',
'query_by' : 'product_name,embedding',
'sort_by' : '_text_match:desc',
}
client.collections['products'].documents.search(search_parameters)
```
```ruby
search_parameters = {
'q' => 'chair',
'query_by' => 'product_name,embedding',
'sort_by' => '_text_match:desc',
}
client.collections['products'].documents.search(search_parameters)
```
```java
SearchParameters searchParameters = new SearchParameters();
searchParameters.q("chair")
.queryBy("product_name,embedding,")
.sortBy("_text_match:desc");
SearchResult searchResult = client.collections("products").documents().search(searchParameters);
```
```go
searchParameters := &api.SearchCollectionParams{
Q: pointer.String("chair"),
QueryBy: pointer.String("product_name,embedding"),
SortBy: pointer.String("_text_match:desc"),
}
client.Collection("products").Documents().Search(context.Background(), searchParameters)
```
--------------------------------
### Index a document with a vector
Source: https://typesense.org/docs/29.0/api/vector-search.md
This example demonstrates how to index a document that includes a vector embedding.
```js
let document = {
'title': 'Louvre Museuem',
'points': 1,
'embedding': [0.04, 0.234, 0.113, 0.001]
}
client.collections('docs').documents().create(document)
```
```php
$document = [
'title' => 'Louvre Museuem',
'points' => 1,
'embedding' => array(0.04, 0.234, 0.113, 0.001)
];
$client->collections['docs']->documents->create($document);
```
```python
document = {
'title': 'Louvre Museuem',
'points': 1,
'embedding': [0.04, 0.234, 0.113, 0.001]
}
client.collections['docs'].documents.create(document)
```
```ruby
document = {
'title' => 'Louvre Museuem',
'points' => 1,
'embedding' => [0.04, 0.234, 0.113, 0.001]
}
client.collections['docs'].documents.create(document)
```
--------------------------------
### Get a specific stopwords set
Source: https://typesense.org/docs/29.0/api/stopwords.md
Example of retrieving stopwords associated with a specific stopword set named 'countries'.
```shell
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/stopwords/countries"
```
--------------------------------
### Multi-search for Query Suggestions (cURL)
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of performing a multi-search to get both query suggestions and actual search results side-by-side.
```bash
curl "http://localhost:8108/multi_search" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
-d '{ \
"searches": [ \
{ \
"collection": "product_queries", \
"q": "shoe", \
"query_by": "q" \
}, \
{ \
"collection": "products", \
"q": "shoe", \
"query_by": "q" \
} \
] \
}'
```
--------------------------------
### Java Client Setup
Source: https://typesense.org/docs/29.0/api/api-clients.md
Instructions for setting up the Typesense Java client library by downloading the JAR file and importing necessary classes.
```java
// Download the JAR file from the releases section in the typesense-java repository.
// (https://github.com/typesense/typesense-java/releases)
// And the import it them to your project
import org.typesense.api.*;
import org.typesense.models.*;
import org.typesense.resources.*;
```
--------------------------------
### Query for Similar Documents
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of how to find documents similar to a specific document by its ID.
```shell
curl 'http://localhost:8108/multi_search' \
-X POST \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
-d '{
"searches": [
{
"collection": "docs",
"q": "*",
"vector_query": "embedding:([], id: foobar)"
}
]
}'
# Be sure to replace `embedding` with the name of the field that stores your embeddings.
```
--------------------------------
### List all aliases
Source: https://typesense.org/docs/29.0/api/collection-alias.html
Example of listing all collection aliases using Go.
```go
client.Aliases().Retrieve(context.Background())
```
--------------------------------
### Multi-search for Query Suggestions (JavaScript)
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of performing a multi-search to get both query suggestions and actual search results side-by-side.
```js
let searchRequests = {
'searches': [
{
'collection': 'product_queries',
'q': 'shoe',
'query_by': 'q'
},
{
'collection': 'products',
'q': 'shoe',
'query_by': 'product_name'
}
]
}
client.multiSearch.perform(searchRequests, {})
```
--------------------------------
### Go Import Parameters
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of setting remote embedding batch size during document import in Go.
```go
importParameters := &api.ImportDocumentsParams{
RemoteEmbeddingBatchSize: pointer.Int(200),
}
client.Collection("products").Documents().Import(context.Background(), documents, importParameters)
```
--------------------------------
### List all aliases
Source: https://typesense.org/docs/29.0/api/collection-alias.html
Example of listing all collection aliases using Shell.
```shell
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/aliases"
```
--------------------------------
### List all Keys
Source: https://typesense.org/docs/29.0/api/api-keys.md
Examples of how to list all API keys using different programming languages and cURL.
```js
client.keys().retrieve()
```
```php
$client->keys->retrieve();
```
```python
client.keys.retrieve()
```
```ruby
client.keys.retrieve
```
```dart
await client.keys.retrieve();
```
```java
ApiKeysResponse apiKeysResponse = client.keys().retrieve();
```
```go
client.Keys().Retrieve(context.Background())
```
```swift
let (apiKeys, response) = try await client.keys().retrieve()
```
```bash
curl 'http://localhost:8108/keys' \
-X GET \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
```
--------------------------------
### Command Line Arguments
Source: https://typesense.org/docs/29.0/api/server-configuration.md
Command line arguments can be passed to the server as --parameter=value.
```bash
--parameter=value
```
--------------------------------
### JavaScript Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a schema with an auto-embedding field using JavaScript.
```javascript
let schema = {
"name": "products",
"fields": [
{
"name": "product_name",
"type": "string"
},
{
"name": "categories",
"type": "string[]"
},
{
"name": "embedding",
"type": "float[]",
"embed": {
"from": [
"product_name",
"categories"
],
"model_config": {
"model_name": "ts/e5-small"
}
}
}
]
};
client.collections('products').create(schema);
```
--------------------------------
### Go Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Schema definition and collection creation using a built-in embedding model in Go.
```go
schema := &api.CollectionSchema{
Name: "products",
Fields: []api.Field{
{
Name: "brand",
Type: "string",
},
{
Name: "categories",
Type: "string[]",
},
{
Name: "embedding",
Type: "float[]",
Embed: &struct {
From []string "json:\"from\""
ModelConfig struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
RefreshToken *string "json:\"refresh_token,omitempty\""
Url *string "json:\"url,omitempty\""
} "json:\"model_config\""
}{
From: []string{"brand", "categories"},
ModelConfig: struct {
AccessToken *string "json:\"access_token,omitempty\""
ApiKey *string "json:\"api_key,omitempty\""
ClientId *string "json:\"client_id,omitempty\""
ClientSecret *string "json:\"client_secret,omitempty\""
DocumentTask *string "json:\"document_task,omitempty\""
IndexingPrefix *string "json:\"indexing_prefix,omitempty\""
ModelName string "json:\"model_name\""
ProjectId *string "json:\"project_id,omitempty\""
QueryPrefix *string "json:\"query_prefix,omitempty\""
QueryTask *string "json:\"query_task,omitempty\""
```
--------------------------------
### Dart Client Installation and Usage
Source: https://typesense.org/docs/29.0/api/api-clients.md
Instructions for adding the Typesense Dart client library to a Dart or Flutter project and how to import it.
```dart
// With Dart:
// $ dart pub add typesense
// With Flutter:
// $ flutter pub add typesense
// This will add a line like this to your package's pubspec.yaml:
// dependencies:
// typesense: ^0.3.0
// Now in your Dart code, you can use:
import 'package:typesense/typesense.js';
```
--------------------------------
### Cloning a collection schema
Source: https://typesense.org/docs/29.0/api/collections.html
This example shows how to clone an existing collection's schema, including overrides and synonyms, but not the documents themselves.
```bash
curl -k "http://localhost:8108/collections?src_name=existing_coll" -X POST -H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"name": "new_coll"
}'
```
--------------------------------
### Bash Example for Sending Click Events
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Sends a click event to Typesense to be aggregated by a counter rule.
```bash
curl "http://localhost:8108/analytics/events" -X POST \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
-d '{
"type": "click",
"name": "products_click_event",
"data": {
"doc_id": "1024",
"user_id": "111112"
}
}'
```
--------------------------------
### Create a stemming dictionary
Source: https://typesense.org/docs/29.0/api/stemming.md
First, create a JSONL file with your word mappings.
```json
{"word": "people", "root": "person"}
{"word": "children", "root": "child"}
{"word": "geese", "root": "goose"}
```
--------------------------------
### Sample Response for API Key Creation
Source: https://typesense.org/docs/29.0/api/api-keys.md
This is a sample JSON response when an API key is created.
```json
{
"actions": [
"*"
],
"collections": [
"*"
],
"description": "Admin key.",
"expires_at": 64723363199,
"id": 1,
"value": "k8pX5hD0793d8YQC5aD1aEPd7VleSuGP"
}
```
--------------------------------
### Java Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Schema definition and collection creation using a built-in embedding model in Java.
```java
CollectionSchema collectionSchema = new CollectionSchema();
ArrayList embedFrom = new ArrayList<>();
embedFrom.add("product_name");
embedFrom.add("categories");
collectionschema.name("products")
.addFieldsItem(new Field().name("product_name").type(FieldTypes.STRING))
.addFieldsItem(new Field().name("categories").type(FieldTypes.STRING_ARRAY))
.addFieldsItem(new Field().name("embedding").type(FieldTypes.FLOAT_ARRAY).embed(
new FieldEmbed().from(embedFrom).modelConfig(new FieldEmbedModelConfig().modelName("ts/all-MiniLM-L12-v2"))
));
CollectionResponse collectionResponse = client.collections().create(collectionSchema);
```
--------------------------------
### Create Collection for No Hits Queries (Go)
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of creating a collection to log no-hits search queries using Go.
```go
schema := &api.CollectionSchema{
Name: "no_hits_queries",
Fields: []api.Field{
{Name: "q", Type: "string"},
{Name: "count", Type: "int32"},
}}
client.Collections().Create(context.Background(), schema)
```
--------------------------------
### Go Example for Creating a Counter Rule
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Defines a 'counter' analytics rule to increment a field when a click event occurs.
```go
ruleName := "product_clicks"
ruleConfiguration := &api.AnalyticsRuleUpsertSchema{
Type: "counter",
Params: api.AnalyticsRuleParameters{
Source: api.AnalyticsRuleParametersSource{
Collections: []string{"products"},
Events: &[]struct {
Name string "json:\"name\""
Type string "json:\"type\""
Weight float32 "json:\"weight\""
}{
{Type: "click", Weight: 1, Name: "products_click_event"},
},
},
Destination: api.AnalyticsRuleParametersDestination{
Collection: "products",
CounterField: pointer.Any("popularity"),
},
},
}
client.Analytics().Rules().Upsert(context.Background(), ruleName, ruleConfiguration)
```
--------------------------------
### Java Semantic Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a semantic search using the Typesense Java client.
```java
SearchParameters searchParameters = new SearchParameters();
searchParameters.q("chair")
.queryBy("embedding");
SearchResult searchResult = client.collections("products").documents().search(searchParameters);
```
--------------------------------
### Ruby Semantic Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a semantic search using the Typesense Ruby client.
```ruby
search_parameters = {
'q' => 'chair',
'query_by' => 'embedding',
}
client.collections['products'].documents.search(search_parameters)
```
--------------------------------
### Python Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Schema definition and collection creation using a built-in embedding model in Python.
```python
schema = {
"name": "products",
"fields": [
{
"name" : "brand",
"type" : "string"
},
{
"name" : "categories",
"type" : "string[]"
},
{
"name" : "embedding",
"type" : "float[]",
"embed": {
"from": [
"brand",
"categories"
],
"model_config": {
"model_name": "ts/all-MiniLM-L12-v2"
}
}
}
]
}
client.collections.create(schema)
```
--------------------------------
### List all synonyms
Source: https://typesense.org/docs/29.0/api/synonyms.md
Examples of how to list all synonyms associated with a collection using various programming languages.
```javascript
client.collections('products').synonyms().retrieve()
```
```php
$client->collections['products']->synonyms->retrieve();
```
```python
client.collections['products'].synonyms.retrieve()
```
```ruby
client.collections['products'].synonyms.retrieve
```
```dart
await client.collection('products').synonyms.retrieve();
```
```java
SearchSynonymsResponse searchSynonymsResponse = client.collections("products").synonyms().retrieve();
```
```go
client.Collection("products").Synonyms().Retrieve(context.Background())
```
```swift
let (searchSynonyms, response) = try await client.collection(name: "products").synonyms().retrieve()
```
```bash
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/collections/products/synonyms"
```
--------------------------------
### Python Semantic Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a semantic search using the Typesense Python client.
```python
search_parameters = {
'q' : 'chair',
'query_by' : 'embedding',
}
client.collections['products'].documents.search(search_parameters)
```
--------------------------------
### Sample Response for Stemming Dictionary Import
Source: https://typesense.org/docs/29.0/api/stemming.md
This is a sample response after importing a stemming dictionary.
```json
{
"id": "irregular-plurals",
"words": [
{"root": "person", "word": "people"},
{"root": "child", "word": "children"},
{"root": "goose", "word": "geese"}
]
}
```
--------------------------------
### PHP Semantic Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a semantic search using the Typesense PHP client.
```php
$search_parameters = [
'q' => 'chair',
'query_by' => 'embedding',
];
$client->collections['products']->documents->search($search_parameters);
```
--------------------------------
### JavaScript Semantic Search Example
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of performing a semantic search using the Typesense JavaScript client.
```javascript
let search_parameters = {
'q' : 'chair',
'query_by' : 'embedding',
}
client.collections('products').documents().search(search_parameters)
```
--------------------------------
### Aggregated Data Example
Source: https://typesense.org/docs/29.0/api/analytics-query-suggestions.md
Example of the aggregated data structure when using meta fields in search.
```json
{
"facet_counts": [],
"found": 1,
"hits": [
{
"document": {
"count": 1,
"analytics_tag": "oxford",
"filter_by": "points:>100",
"id": "9677940243608161791",
"q": "iron"
},
"highlight": {},
"highlights": []
}
],
"out_of": 1,
"page": 1,
"request_params": {
"collection_name": "top_queries",
"first_q": "*",
"per_page": 10,
"q": "*"
},
"search_cutoff": false,
"search_time_ms": 0
}
```
--------------------------------
### Create Collection with GCP Vertex AI Embedding (Go)
Source: https://typesense.org/docs/29.0/api/vector-search.md
Example of creating a collection with an embedding field configured to use GCP Vertex AI in Go.
```go
schema := &api.CollectionSchema{
Name: "products",
Fields: []api.Field{
{Name: "product_name", Type: "string"},
{Name: "embedding", Type: "float[]",
Embed: &struct {
From []string "json:\"from\""
ModelConfig struct {
```
--------------------------------
### Upload a stemming dictionary
Source: https://typesense.org/docs/29.0/api/stemming.md
Then upload it using the stemming dictionary API.
```bash
curl "http://localhost:8108/stemming/dictionaries/import?id=irregular-plurals" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
--data-binary @dictionary.jsonl
```
--------------------------------
### Field Schema Example
Source: https://typesense.org/docs/29.0/api/collections.html
Example of how to define a field with name, type, facet, and index properties.
```json
{
"name": "title",
"type": "string",
"facet": false,
"index": true
}
```