Created
June 13, 2023 20:18
-
-
Save gustavopch/8053ca773f08dbde8b818d9c18a8d0c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Client, SearchClient } from 'typesense' | |
const TYPESENSE_HOST = '' | |
const TYPESENSE_PORT = 0 | |
const TYPESENSE_ADMIN_KEY = '' | |
const TYPESENSE_SEARCH_KEY = '' | |
const client = new Client({ | |
nodes: [ | |
{ | |
host: TYPESENSE_HOST, | |
port: TYPESENSE_PORT, | |
protocol: 'https', | |
}, | |
], | |
apiKey: TYPESENSE_ADMIN_KEY, | |
}) | |
// Create preset | |
await client.presets().upsert('test', { | |
value: { per_page: 1 }, | |
}) | |
// Create collection | |
await client.collections().create({ | |
name: 'test', | |
fields: [ | |
{ | |
name: 'title', | |
type: 'string', | |
}, | |
], | |
}) | |
// Index some docs | |
await client.collections('test').documents().create({ | |
title: 'Test 1', | |
}) | |
await client.collections('test').documents().create({ | |
title: 'Test 2', | |
}) | |
await client.collections('test').documents().create({ | |
title: 'Test 3', | |
}) | |
// Create scoped key with the preset embedded | |
const searchKey = client.keys().generateScopedSearchKey(TYPESENSE_SEARCH_KEY, { | |
preset: 'test', | |
}) | |
// Create search client using that scoped key | |
const searchClient = new SearchClient({ | |
nodes: [ | |
{ | |
host: TYPESENSE_HOST, | |
port: TYPESENSE_PORT, | |
protocol: 'https', | |
}, | |
], | |
apiKey: searchKey, | |
}) | |
// Search | |
const result = await searchClient.collections('test').documents().search( | |
{ | |
q: '*', | |
}, | |
{}, | |
) | |
console.log(result) | |
// Expected to have 1 hit because `per_page: 1` is set in the scoped key. | |
// | |
// { | |
// facet_counts: [], | |
// found: 3, | |
// hits: [ | |
// { document: [Object], highlight: {}, highlights: [] }, | |
// { document: [Object], highlight: {}, highlights: [] }, | |
// { document: [Object], highlight: {}, highlights: [] } | |
// ], | |
// out_of: 3, | |
// page: 1, | |
// request_params: { collection_name: 'test', per_page: 10, q: '*' }, | |
// search_cutoff: false, | |
// search_time_ms: 0 | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment