Skip to content

Commit

Permalink
Update and fixes for 1.1.2 (#6)
Browse files Browse the repository at this point in the history
- Move examples into another folder and expanded documentation.
- Fixes issue on Webpack and potentially other bundles to target ESM files when bundling.
- Fixes default parameter host=localhost resolving to ipv6 ::1 since Node 17 by using 127.0.0.1 instead.
  • Loading branch information
Rendez authored Apr 25, 2023
1 parent 01f343d commit 4420c5f
Show file tree
Hide file tree
Showing 21 changed files with 480 additions and 77 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-dodos-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@qdrant/js-client-rest': patch
---

Move examples into another folder and expanded documentation
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json",
"changelog": "@changesets/cli/changelog",
"changelog": ["@changesets/changelog-github", {"repo": "qdrant/qdrant-js"}],
"commit": false,
"fixed": [],
"linked": [],
Expand Down
6 changes: 6 additions & 0 deletions .changeset/slimy-cycles-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@qdrant/js-client-rest': patch
'@qdrant/qdrant-js': patch
---

Fixes issue on Webpack and potentially other bundles to target ESM files when bundling.
5 changes: 5 additions & 0 deletions .changeset/thirty-olives-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@qdrant/js-client-rest': patch
---

Fixes default parameter host=localhost resolving to ipv6 ::1 since Node 17 by using 127.0.0.1 instead.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
packages/**/package-lock.json
**/yarn.lock
**/package-lock.json
**/node_modules/
**/.pnpm-store/
**/*.env
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@
This repository contains packages of the JS SDK for the [Qdrant](https://github.com/qdrant/qdrant) vector search engine.

The SDK package can be found under [packages/qdrant-js](./packages/qdrant-js).

## Releases

Major and minor versions align with Qdrant's engine releases, whilst patch are reserved for fixes regarding the current minor release. Check out [RELEASE.md](./RELEASE.md) for more info on release guidelines.

For release automation we use [`changesets`](https://github.com/changesets/changesets) both for pull requests and pushes to the master branch, and their [Github Action](https://github.com/changesets/action) to automate changeset steps.

## Contributions

In order to contribute there are a couple of things you may need to do.

We make use of [`pnpm`](https://pnpm.io/) instead of `npm` or `yarn` to manage and install packages in this monorepo, make sure it's installed on your local environment.

After checking out the repository and desired branch, run `pnpm install` to install all package's dependencies and run the compilation steps. This will work for the monorepo.

> For anything outside the monorepo, e.g.: [`examples/node-js-basic`](./examples/node-js-basic) feel free to use `npm` for installing packages and running scripts.
40 changes: 40 additions & 0 deletions examples/node-js-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Node.js Example

Access Qdrant API from Node.js

## How to

Install dependencies

```bash
npm install --save @qdrant/js-client-rest
```

Import client

```js
import {QdrantClient} from '@qdrant/js-client-rest';
```

Initialize client

```js
const client = new QdrantClient({url: 'http://127.0.0.1:6333'});
```

## Run example

```bash
node ./index.js
```

## Connect to Qdrant Cloud

```js
const client = new QdrantClient({
url: 'https://xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.us-east-0-1.aws.cloud.qdrant.io',
apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
});
```

Obtain API key from [Qdrant Cloud](https://cloud.qdrant.io/)
240 changes: 240 additions & 0 deletions examples/node-js-basic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import {QdrantClient} from '@qdrant/js-client-rest';

async function main() {
const collectionName = 'test_collection';

const client = new QdrantClient({url: 'http://127.0.0.1:6333'});

const response = await client.getCollections();

const collectionNames = response.collections.map((collection) => collection.name);

if (collectionNames.includes(collectionName)) {
await client.deleteCollection(collectionName);
}

await client.createCollection(collectionName, {
vectors: {
size: 4,
distance: 'Cosine',
},
optimizers_config: {
default_segment_number: 2,
},
replication_factor: 2,
});

// -------- Create payload indexes -------------

await client.createPayloadIndex(collectionName, {
field_name: 'city',
field_schema: 'keyword',
wait: true,
});

await client.createPayloadIndex(collectionName, {
field_name: 'count',
field_schema: 'integer',
wait: true,
});

await client.createPayloadIndex(collectionName, {
field_name: 'coords',
field_schema: 'geo',
wait: true,
});

// -------- Add points -------------

await client.upsert(collectionName, {
wait: true,
points: [
{
id: 1,
vector: [0.05, 0.61, 0.76, 0.74],
payload: {
city: 'Berlin',
country: 'Germany',
count: 1000000,
square: 12.5,
coords: {lat: 1.0, lon: 2.0},
},
},
{id: 2, vector: [0.19, 0.81, 0.75, 0.11], payload: {city: ['Berlin', 'London']}},
{id: 3, vector: [0.36, 0.55, 0.47, 0.94], payload: {city: ['Berlin', 'Moscow']}},
{id: 4, vector: [0.18, 0.01, 0.85, 0.8], payload: {city: ['London', 'Moscow']}},
{id: '98a9a4b1-4ef2-46fb-8315-a97d874fe1d7', vector: [0.24, 0.18, 0.22, 0.44], payload: {count: [0]}},
{id: 'f0e09527-b096-42a8-94e9-ea94d342b925', vector: [0.35, 0.08, 0.11, 0.44]},
],
});

const collectionInfo = await client.getCollection(collectionName);
console.log(collectionInfo.points_count);
// prints: 6

const points = await client.retrieve(collectionName, {
ids: [1, 2],
});

console.log('points: ', points);
// prints:
// points: [
// {
// id: 1,
// payload: {
// city: 'Berlin',
// coords: [Object],
// count: 1000000,
// country: 'Germany',
// square: 12.5
// },
// vector: null
// },
// { id: 2, payload: { city: [Array] }, vector: null }
// ]

// -------- Search ----------------
const queryVector = [0.2, 0.1, 0.9, 0.7];

const res1 = await client.search(collectionName, {
vector: queryVector,
limit: 3,
});

console.log('search result: ', res1);
// prints:
// search result: [
// {
// id: 4,
// version: 3,
// score: 0.99248314,
// payload: { city: [Array] },
// vector: null
// },
// {
// id: 1,
// version: 3,
// score: 0.89463294,
// payload: {
// city: 'Berlin',
// coords: [Object],
// count: 1000000,
// country: 'Germany',
// square: 12.5
// },
// vector: null
// },
// {
// id: '98a9a4b1-4ef2-46fb-8315-a97d874fe1d7',
// version: 3,
// score: 0.8543979,
// payload: { count: [Array] },
// vector: null
// }
// ]

const resBatch = await client.searchBatch(collectionName, {
searches: [
{
vector: queryVector,
limit: 1,
},
{
vector: queryVector,
limit: 2,
},
],
});

console.log('search batch result: ', resBatch);
// prints:
// search batch result: [
// [
// {
// id: 4,
// version: 3,
// score: 0.99248314,
// payload: null,
// vector: null
// }
// ],
// [
// {
// id: 4,
// version: 3,
// score: 0.99248314,
// payload: null,
// vector: null
// },
// {
// id: 1,
// version: 3,
// score: 0.89463294,
// payload: null,
// vector: null
// }
// ]
// ]

// -------- Search filters ----------------

const res2 = await client.search(collectionName, {
vector: queryVector,
limit: 3,
filter: {
must: [
{
key: 'city',
match: {
value: 'Berlin',
},
},
],
},
});

console.log('search result with filter: ', res2);
// prints:
// search result with filter: [
// {
// id: 1,
// version: 3,
// score: 0.89463294,
// payload: {
// city: 'Berlin',
// coords: [Object],
// count: 1000000,
// country: 'Germany',
// square: 12.5
// },
// vector: null
// },
// {
// id: 3,
// version: 3,
// score: 0.83872515,
// payload: { city: [Array] },
// vector: null
// },
// {
// id: 2,
// version: 3,
// score: 0.66603535,
// payload: { city: [Array] },
// vector: null
// }
// ]

return 0;
}

main()
.then((code) => {
if (code !== 0) {
process.exit(code);
}
})
.catch((err) => {
console.error(err);
process.exit(1);
});
60 changes: 60 additions & 0 deletions examples/node-js-basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4420c5f

Please sign in to comment.