Using Aliases in Typesense Search Queries
TLDR Babin had a question about using aliases in Typesense search queries, specifically when passing collection names. Jason helped identify a misconfiguration and provided guidance, resolving the issue.
3
1
1
Mar 13, 2023 (9 months ago)
Babin
07:53 PM{
"aliases": [
{
"collection_name": "items_1_031323",
"name": "items_1_alias"
},
{
"collection_name": "items_1",
"name": "test"
}
]
}
I have created 2nd alias on collection Items_1. Now in search query, when I pass collection name as test, it doesn't work. How do I use
test
alias name in search, so that I don't need to change the front end configuration. Appreciate any helpJason
07:54 PMitems_1
?Babin
08:03 PMBabin
08:03 PMCollection 2: items with documents count 8862
Collection 3: item-categories with documents count 660
Collection 4: brands with documents count 3908
Babin
08:03 PMtest
as collectionName it doesn't workJason
08:04 PMJason
08:04 PMJason
08:05 PMBabin
08:05 PMBabin
08:05 PM1
Babin
08:06 PMBabin
08:06 PMJason
08:09 PMJason
08:09 PMBabin
08:24 PM{
"created_at": 1678739026,
"default_sorting_field": "num_employees",
"enable_nested_fields": false,
"fields": [
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "company_name",
"optional": false,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "num_employees",
"optional": false,
"sort": true,
"type": "int32"
},
{
"facet": true,
"index": true,
"infix": false,
"locale": "",
"name": "country",
"optional": false,
"sort": false,
"type": "string"
}
],
"name": "companies",
"num_documents": 0,
"symbols_to_index": [],
"token_separators": []
}
Babin
08:24 PM{"success":true}
Babin
08:24 PM{"success":true}{"results":[{"code":404,"error":"Could not find a field named `name` in the schema."}]}
Jason
08:25 PM1
Babin
08:44 PMsource .env
typeSenseApiKey=${TYPE_SENSE_API_MASTER_KEY}
typeSenseHost=${TYPE_SENSE_HOSTURL}
curl "${typeSenseHost}/collections" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${typeSenseApiKey}" \
-d '{
"name": "companies",
"fields": [
{"name": "company_name", "type": "string" },
{"name": "num_employees", "type": "int32" },
{"name": "country", "type": "string", "facet": true }
],
"default_sorting_field": "num_employees"
}'
curl "${typeSenseHost}/collections/companies/documents/import?action=create" \
-H "X-TYPESENSE-API-KEY: ${typeSenseApiKey}" \
-H "Content-Type: text/plain" \
-X POST \
-d '{"id": "124","company_name": "Stark Industries","num_employees": 5215,"country": "USA"}
{"id": "125","company_name": "Acme Corp","num_employees": 2133,"country": "CA"}'
curl "${TYPE_SENSE_HOSTURL}/aliases/companies_alias" -X PUT \
-H "X-TYPESENSE-API-KEY: ${TYPE_SENSE_API_MASTER_KEY}" \
-H "Content-Type: application/json" \
-d "{\"collection_name\":\"companies\"}"
curl "${typeSenseHost}/multi_search?query_by=name" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${typeSenseApiKey}" \
-d '{
"searches": [
{
"collection": "companies_alias",
"q": "stark"
}
]
}'
Babin
08:44 PMBabin
08:45 PMJason
08:46 PMBabin
08:48 PM{"created_at":1678740196,"default_sorting_field":"num_employees","enable_nested_fields":false,"fields":[{"facet":false,"index":true,"infix":false,"locale":"","name":"company_name","optional":false,"sort":false,"type":"string"},{"facet":false,"index":true,"infix":false,"locale":"","name":"num_employees","optional":false,"sort":true,"type":"int32"},{"facet":true,"index":true,"infix":false,"locale":"","name":"country","optional":false,"sort":false,"type":"string"}],"name":"companies","num_documents":0,"symbols_to_index":[],"token_separators":[]}{"success":true}
{"success":true}{"collection_name":"companies","name":"companies_alias"}{"results":[{"code":404,"error":"Could not find a field named `name` in the schema."}]}
Jason
08:48 PMcurl "${typeSenseHost}/multi_search?query_by=company_name" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${typeSenseApiKey}" \
-d '{
"searches": [
{
"collection": "companies_alias",
"q": "stark",
}
]
}'
Jason
08:49 PMquery_by=name
to query_by=company_name
(the gist I shared with you had a typo in it)Babin
08:51 PM{
"results": [
{
"facet_counts": [],
"found": 1,
"hits": [
{
"document": {
"company_name": "Stark Industries",
"country": "USA",
"id": "124",
"num_employees": 5215
},
"highlight": {
"company_name": {
"matched_tokens": [
"Stark"
],
"snippet": "<mark>Stark</mark> Industries"
}
},
"highlights": [
{
"field": "company_name",
"matched_tokens": [
"Stark"
],
"snippet": "<mark>Stark</mark> Industries"
}
],
"text_match": 578730123365187705,
"text_match_info": {
"best_field_score": "1108091338752",
"best_field_weight": 15,
"fields_matched": 1,
"score": "578730123365187705",
"tokens_matched": 1
}
}
],
"out_of": 2,
"page": 1,
"request_params": {
"collection_name": "companies",
"per_page": 10,
"q": "stark"
},
"search_cutoff": false,
"search_time_ms": 0
}
]
}
Babin
08:51 PMJason
08:52 PMJason
08:52 PMBabin
08:52 PMBabin
08:52 PMtest
aliasJason
08:52 PMJason
08:53 PMBabin
08:54 PMBabin
08:54 PMJason
08:55 PMcurl "${typeSenseHost}/multi_search?query_by=company_name" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${typeSenseApiKey}" \
-d '{
"searches": [
{
"collection": "companies_alias",
"q": "stark",
}
]
}'
companies_alias
is the alias name right?Babin
08:55 PMBabin
08:55 PMBabin
09:02 PM1
Babin
09:02 PM1
Babin
09:13 PM1
Jason
09:13 PMTypesense
Indexed 3015 threads (79% resolved)
Similar Threads
Resolving Typesense Server Connection with Correct API Keys and Paths
Rubai had trouble creating a working API key for Typesense and understanding number_hits versus num_documents. Jason provided clarification and correct code for generating a search-only API Key and using correct server configurations.
Sorting Results in Typesense and Handling Errors
Ramees asks about sorting results by distance in Typesense, entering a list of strings as a field, and resolving an error with `fields` format. Kishore Nallan assists with these issues and advises on storing timestamps and proper authentication.
Trouble in Implementing Deeply Nested Search
Anirudh is struggling to implement a two-level nested search. Jason asked for some specific examples to study the issue. Anirudh provided some material, realizing that adding top fields helped but might over-index. Jason then suggested reporting this issue on GitHub.
Querying and Indexing Multiple Elements Issues
Krish queried fields with multiple elements, which Kishore Nallan suggested checking `drop_tokens_threshold`. Krish wished to force OR mode for token, but Kishore Nallan admitted the feature was missing. Krish was able to resolve the issue with url encoding.
Issues with Importing Typesense Collection to Different Server
Kevin had problems migrating a Typesense collection between Docusaurus sites on different machines. Jason advised them on JSONL format, handling server hosting, and creating a collection schema before importing documents, leading to successful import.