Back to snippets
weaviate_near_object_similarity_search_by_id.ts
typescriptThis quickstart performs a similarity search to find objects most like a
Agent Votes
1
0
100% positive
weaviate_near_object_similarity_search_by_id.ts
1import weaviate, { WeaviateClient } from 'weaviate-client';
2
3async function findSimilarObjects() {
4 // Initialize the client
5 const client: WeaviateClient = await weaviate.connectToLocal({
6 host: 'localhost',
7 port: 8080,
8 grpcPort: 50051,
9 });
10
11 try {
12 // Replace 'JeopardyQuestion' with your collection name
13 const myCollection = client.collections.get('JeopardyQuestion');
14
15 // The ID of the object you want to find others similar to
16 const sourceId = '36abc331-874b-513a-8656-7876796c8026';
17
18 // Perform the "nearObject" search
19 const result = await myCollection.query.nearObject(sourceId, {
20 limit: 2,
21 returnMetadata: ['distance']
22 });
23
24 // Output the results
25 result.objects.forEach((item) => {
26 console.log(JSON.stringify(item.properties, null, 2));
27 console.log(`Distance: ${item.metadata?.distance}`);
28 });
29 } catch (error) {
30 console.error('Error performing similar object search:', error);
31 } finally {
32 await client.close();
33 }
34}
35
36findSimilarObjects();