#community-help

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.

Powered by Struct AI

3

1

1

Mar 13, 2023 (9 months ago)
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
07:53 PM
Hi Team, I have a question, how to use alias.
{
    "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 help
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:54 PM
Do you already have a collection named items_1?
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
08:03 PM
Yes
08:03
Babin
08:03 PM
Collection 1: items_1 with documents count 499
Collection 2: items with documents count 8862
Collection 3: item-categories with documents count 660
Collection 4: brands with documents count 3908
08:03
Babin
08:03 PM
client.collections(collectionName).documents().search(searchParameters) if I pass test as collectionName it doesn't work
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:04 PM
Could you clone this code-snippet and reproduce the issue you’re seeing: https://gist.github.com/jasonbosco/7c3432713216c378472f13e72246f46b
08:04
Jason
08:04 PM
(You’ll find curl commands for aliases in the docs)
08:05
Jason
08:05 PM
And then share that snippet with me so I can try it out locally?
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
08:05 PM
Give me some time
08:05
Babin
08:05 PM
I will post here

1

08:06
Babin
08:06 PM
Do you want be to run these curls in my collection ?
08:06
Babin
08:06 PM
I don't have any local instance
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:09 PM
Yeah, feel free to use your Typesense Cloud cluster hostname
08:09
Jason
08:09 PM
(and leave out the API key when you share the code snippet here)
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
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": []
}
08:24
Babin
08:24 PM
{"success":true}
08:24
Babin
08:24 PM
{"success":true}{"results":[{"code":404,"error":"Could not find a field named `name` in the schema."}]}
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:25 PM
That snippet was an example btw, could you edit it as needed to create the aliases and share the edited snippet with me?

1

Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
08:44 PM
source .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"
            }
          ]
        }'
08:44
Babin
08:44 PM
Collection 1: companies with documents count 2
08:45
Babin
08:45 PM
{"aliases":[{"collection_name":"items_1","name":"test"},{"collection_name":"companies","name":"companies_alias"}]}
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:46 PM
Great, could you also post the output of running that script?
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
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
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:48 PM
Could you change the last curl command to:

curl "${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",
            }
          ]
        }'
08:49
Jason
08:49 PM
Changed query_by=name to query_by=company_name (the gist I shared with you had a typo in it)
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
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
        }
    ]
}
08:51
Babin
08:51 PM
I got result
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:52 PM
Yeah, so the alias feature is working as expected…
08:52
Jason
08:52 PM
Could you try seeing how this example is different from what you have?
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
08:52 PM
How to call it from JavaSCript
08:52
Babin
08:52 PM
Let me search from test alias
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:52 PM
You’ll find JS snippets next to the curl commands
08:53
Jason
08:53 PM
Wherever the docs say “collection name”, you can use an alias instead
Babin
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
08:54 PM
One sec
08:54
Babin
08:54 PM
In previous curl I searched from collection itself
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:55 PM
curl "${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
Photo of md5-96b600ac63a91c0a63ecfe2fa00e8233
Babin
08:55 PM
I meant in my host
08:55
Babin
08:55 PM
let me curl in my collection first, then Js code
09:02
Babin
09:02 PM
It worked

1

09:02
Babin
09:02 PM
It was a wrong configuration

1

09:13
Babin
09:13 PM
Thank you so much for you help Jason

1

Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:13 PM
Happy to!

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community

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.

1

30
4mo

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.

2

46
30mo

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.

2

21
2d

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.

34
12mo

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.

3

35
3mo