i am trying like this..dont have example in typesc...
# community-help
g
i am trying like this..dont have example in typescript too..pls help on this
j
Here’s a typescript example: https://github.com/typesense/typesense-js/blob/master/doc/examples/server/documents.ts In your code snippet, that last two lines should be:
Copy code
const results = await searchClient.collections('companies').documents().search(searchParameters);

return data;
g
thanks for ur quick reply
Copy code
{
  "error": "TypeError",
  "message": "searchClient.collections is not a function"
}
i am getting above error..
// const results = await searchClient.collections('companies').documents().search(searchParameters); const results = await searchClient.collections('companies').documents('USA').retrieve(); return results;
j
Could you share how you’ve instantiated
searchClient
?
g
const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({ server: { apiKey: '2kxogtNROPlHjEBSy8pbd9f7Zxw3HcAQ', // Be sure to use an API key that only allows search operations nodes: [ { host: '1bf8hqds9032wpntp-1.a1.typesense.net', path: '', // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense' port: 443, protocol: 'https', }, ], cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching. }, // The following parameters are directly passed to Typesense's search API endpoint. // So you can pass any parameters supported by the search endpoint below. // query_by is required. additionalSearchParameters: { query_by: 'country', }, });
j
Looks like you’re using the
TypesenseInstantSearchAdapter
which you’d only use if you’re using Instantsearch.js which is a UI library. In your case, in your code snippet you’re using typesense-js directly. So you want to instantiate the client like this: https://typesense.org/docs/0.23.1/api/authentication.html
g
yes i am trying above typescript one
node_modules/typesense/src/Typesense/ApiCall.ts607 - error TS2322: Type 'null' is not assignable to type 'ResponseType'. 60 responseType = null ~~~~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts957 - error TS2322: Type 'null' is not assignable to type 'ResponseType'. 95 responseType = null ~~~~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts20786 - error TS2571: Object is of type 'unknown'. 207 `Request #${requestNumber}: Request to Node ${node.index} failed due to "${error.code} ${error.message}${ ~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts207100 - error TS2571: Object is of type 'unknown'. 207 `Request #${requestNumber}: Request to Node ${node.index} failed due to "${error.code} ${error.message}${ ~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts20813 - error TS2571: Object is of type 'unknown'. 208 error.response == null ? '' : ' - ' + JSON.stringify(error.response?.data) ~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts20866 - error TS2571: Object is of type 'unknown'. 208 error.response == null ? '' : ' - ' + JSON.stringify(error.response?.data) ~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts26396 - error TS2454: Variable 'candidateNode' is used before being assigned. 263
Request #${requestNumber}: No healthy nodes were found. Returning the next node, Node ${candidateNode.index}
~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts26512 - error TS2454: Variable 'candidateNode' is used before being assigned. 265 return candidateNode ~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts26825 - error TS7006: Parameter 'node' implicitly has an 'any' type. 268 nodeDueForHealthcheck(node, requestNumber: number = 0): boolean { ~~ node_modules/typesense/src/Typesense/ApiCall.ts29022 - error TS7006: Parameter 'node' implicitly has an 'any' type. 290 setNodeHealthcheck(node, isHealthy): void { ~~ node_modules/typesense/src/Typesense/ApiCall.ts29028 - error TS7006: Parameter 'isHealthy' implicitly has an 'any' type. 290 setNodeHealthcheck(node, isHealthy): void { ~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts29528 - error TS7006: Parameter 'node' implicitly has an 'any' type. 295 uriFor(endpoint: string, node): string { ~~ node_modules/typesense/src/Typesense/ApiCall.ts3057 - error TS7053: Element implicitly has an 'any' type because expression of type '"X-TYPESENSE-API-KEY"' can't be used to index type '{}'. Property 'X-TYPESENSE-API-KEY' does not exist on type '{}'. 305 defaultHeaders[APIKEYHEADERNAME] = this.apiKey ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts3075 - error TS7053: Element implicitly has an 'any' type because expression of type '"Content-Type"' can't be used to index type '{}'. Property 'Content-Type' does not exist on type '{}'. 307 defaultHeaders['Content-Type'] = 'application/json' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/ApiCall.ts31115 - error TS7006: Parameter 'seconds' implicitly has an 'any' type. 311 async timer(seconds): Promise<void> { ~~~~~~~ node_modules/typesense/src/Typesense/Configuration.ts725 - error TS2322: Type 'NodeConfiguration | null' is not assignable to type 'NodeConfiguration'. Type 'null' is not assignable to type 'NodeConfiguration'. 72 this.nearestNode = options.nearestNode || null ~~~~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/Configuration.ts825 - error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'. 82 this.sendApiKeyAsQueryParam = options.sendApiKeyAsQueryParam // We will set a default for this in Client and SearchClient ~~~~~~~~~~~~~~~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/Configuration.ts915 - error TS2322: Type 'Record<string, string> | undefined' is not assignable to type 'Record<string, string>'. Type 'undefined' is not assignable to type 'Record<string, string>'. 91 this.additionalHeaders = options.additionalHeaders ~~~~~~~~~~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/Configuration.ts16624 - error TS7006: Parameter 'array' implicitly has an 'any' type. 166 private shuffleArray(array) { ~~~~~ node_modules/typesense/src/Typesense/Documents.ts20135 - error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type. 201 if (RangeError instanceof error && error?.includes('Too many properties to enumerate')) { ~~~~~ node_modules/typesense/src/Typesense/Documents.ts20144 - error TS2571: Object is of type 'unknown'. 201 if (RangeError instanceof error && error?.includes('Too many properties to enumerate')) { ~~~~~ node_modules/typesense/src/Typesense/Documents.ts21025 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'string | undefined'. 210 throw new Error(error) ~~~~~ node_modules/typesense/src/Typesense/Errors/ImportError.ts615 - error TS7006: Parameter 'message' implicitly has an 'any' type. 6 constructor(message, importResults) { ~~~~~~~ node_modules/typesense/src/Typesense/Errors/ImportError.ts624 - error TS7006: Parameter 'importResults' implicitly has an 'any' type. 6 constructor(message, importResults) { ~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/RequestWithCache.ts227 - error TS2322: Type 'unknown' is not assignable to type 'T'. 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'. 22 return requestFunction.call(requestContext, ...requestFunctionArguments) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node_modules/typesense/src/Typesense/SearchOnlyDocuments.ts227 - error TS2322: Type 'null' is not assignable to type 'AbortSignal'. 22 abortSignal = null ~~~~~~~~~~~ node_modules/typesense/src/Typesense/SearchOnlyDocuments.ts277 - error TS7053: Element implicitly has an 'any' type because expression of type '"use_cache"' can't be used to index type '{}'. Property 'use_cache' does not exist on type '{}'. 27 additionalQueryParams['use_cache'] = true ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Found 27 errors in 6 files. Errors Files 15 node_modules/typesense/src/Typesense/ApiCall.ts:60 4 node_modules/typesense/src/Typesense/Configuration.ts:72 3 node_modules/typesense/src/Typesense/Documents.ts:201 2 node_modules/typesense/src/Typesense/Errors/ImportError.ts:6 1 node_modules/typesense/src/Typesense/RequestWithCache.ts:22 2 node_modules/typesense/src/Typesense/SearchOnlyDocuments.ts:22
i am getting this error
my code snippet like this..
import { env } from '../environment/environment'; import { ExceptionsHandler } from '../exceptionHandler/exceptionHandler'; import { DataWrapper, ErrorAzureResponse } from '../response/response'; import { Client } from 'typesense'; import Configuration, { NodeConfiguration, ConfigurationOptions } from 'typesense/src/Typesense/Configuration'; import { CollectionCreateSchema } from 'typesense/src/Typesense/Collections'; const typesense = new Client({ nodes: [ { host: '1bf8hqds9032wpntp-1.a1.typesense.net', port: 443, protocol: 'http', } as NodeConfiguration, ], apiKey: '2kxogtNROPlHjEBSy8pbd9f7Zxw3HcAQ', numRetries: 3, // A total of 4 tries (1 original try + 3 retries) connectionTimeoutSeconds: 120, // Set a longer timeout for large imports logLevel: 'debug', } as ConfigurationOptions); const SearchExceptionHandler = (e: any) => { throw new ErrorAzureResponse({ error: 'INTERNAL_SERVER_ERROR', message: e }); }; export class Search { async getTypesearch(latitude: number, longitude: number) { const searchParameters = { q: 'USA', query_by: 'country', group_by: 'country', group_limit: '1', }; const results = await typesense.collections('companies').documents('USA').retrieve(); return results; } }
i created account in typssensecloud
ur help is much appriciated..
j
Could you try updating these import two lines to lib instead of src:
Copy code
import Configuration, { NodeConfiguration, ConfigurationOptions } from 'typesense/lib/Typesense/Configuration';
import { CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';
It’s essentially trying to recompile the Typesense TS source code, instead of using the precompiled one in lib, and throwing those errors
g
okay thanks..let me check
yes ..great...its working..
❤️ 1
🙌 1
how to pass parameters instead of id..
j
Didn’t get you… Could you rephrase that with an example?
g
message has been deleted
this is my json..
i want to search by country..
this is test JSON i created..for POC purpose..
const searchParameters = { q: 'USA', query_by: 'country', group_by: 'country', group_limit: '1', }; const results = await typesense.collections('companies').documents().search(searchParameters); return results;
j
You want something like this:
Copy code
export class Search {
  async getTypesearch(latitude: number, longitude: number, country: string) {
    const searchParameters = {
      q: country, // <== change this to a parameter
      query_by: 'country',
      group_by: 'country',
      group_limit: '1',
    };



    const results = await typesense.collections('companies').documents().search(searchParameters); // <== change this to search instead of retrieve
    return results;
  }
}
g
great...i got the error if i put group_limit
with out that its working.
async getTypesearch(latitude: number, longitude: number, country: string) { const searchParameters = { q: country, query_by: 'country', group_by: 'country', }; const results = await typesense.collections('companies').documents().search(searchParameters); return results; }
j
group_limit should be a number instead of a string
🙌 1
g
how to add multiple condition?
country ,langtitude,longtiude also in search parameters
j
You want to use
filter_by
search parameter for this. It’s documented under this table: https://typesense.org/docs/0.23.1/api/search.html#query-parameters
🙌 1
g
Okay ..thanks..let me try..
thank you very much..