Hi Team, I have a question, how to use alias. ```{...
# community-help
b
Hi Team, I have a question, how to use alias.
Copy code
{
  "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
j
Do you already have a collection named
items_1
?
b
Yes
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
client.collections(collectionName).documents().search(searchParameters) if I pass
test
as collectionName it doesn't work
j
Could you clone this code-snippet and reproduce the issue you’re seeing: https://gist.github.com/jasonbosco/7c3432713216c378472f13e72246f46b
(You’ll find curl commands for aliases in the docs)
And then share that snippet with me so I can try it out locally?
b
Give me some time
I will post here
👍 1
Do you want be to run these curls in my collection ?
I don't have any local instance
j
Yeah, feel free to use your Typesense Cloud cluster hostname
(and leave out the API key when you share the code snippet here)
b
Copy code
{
  "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": []
}
Copy code
{
  "success": true
}
Copy code
{"success":true}{"results":[{"code":404,"error":"Could not find a field named `name` in the schema."}]}
j
That snippet was an example btw, could you edit it as needed to create the aliases and share the edited snippet with me?
🆗 1
b
Copy code
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"
            }
          ]
        }'
Collection 1: companies with documents count 2
Copy code
{
  "aliases": [
    {
      "collection_name": "items_1",
      "name": "test"
    },
    {
      "collection_name": "companies",
      "name": "companies_alias"
    }
  ]
}
j
Great, could you also post the output of running that script?
b
Copy code
{"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."}]}
j
Could you change the last curl command to:
Copy code
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",
            }
          ]
        }'
Changed
query_by=name
to
query_by=company_name
(the gist I shared with you had a typo in it)
b
Copy code
{
  "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": 578730123365187700,
          "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
    }
  ]
}
I got result
j
Yeah, so the alias feature is working as expected…
Could you try seeing how this example is different from what you have?
b
How to call it from JavaSCript
Let me search from
test
alias
j
You’ll find JS snippets next to the curl commands
Wherever the docs say “collection name”, you can use an alias instead
b
One sec
In previous curl I searched from collection itself
j
Copy code
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?
b
I meant in my host
let me curl in my collection first, then Js code
It worked
🙌 1
It was a wrong configuration
👍 1
Thank you so much for you help @Jason Bosco
👍 1
j
Happy to!