Skip to content

Commit 49352ba

Browse files
committed
Fix Getting Started in README
1 parent f4fa981 commit 49352ba

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

README.md

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,29 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
4949
Here is a quickstart for a search request
5050

5151
```js
52-
const MeiliSearch = require('meilisearch') // import MeiliSearch from 'meilisearch'
53-
54-
// Credentials of your MeiliSearch Instance
55-
const config = {
56-
host: 'http://127.0.0.1:7700',
57-
apiKey: 'masterKey',
58-
}
59-
60-
const meili = new MeiliSearch(config)
61-
62-
await meili.createIndex({ uid: 'books' }) // if your index does not exist
63-
const index = await meili.getIndex('books');
64-
65-
const documents = [
66-
{ book_id: 123, title: 'Pride and Prejudice' },
67-
{ book_id: 456, title: 'Le Petit Prince' },
68-
{ book_id: 1, title: 'Alice In Wonderland' },
69-
{ book_id: 1344, title: 'The Hobbit' },
70-
{ book_id: 4, title: 'Harry Potter and the Half-Blood Prince' },
71-
{ book_id: 42, title: "The Hitchhiker's Guide to the Galaxy" },
72-
]
73-
74-
await index.addDocuments(documents) // { "updateId": 0 }
52+
const MeiliSearch = require('meilisearch');
53+
54+
(async () => {
55+
const client = new MeiliSearch({
56+
host: 'http://127.0.0.1:7700',
57+
apiKey: 'masterKey'
58+
})
59+
60+
await client.createIndex({ uid: 'books' }) // only if your index does not exist
61+
const index = client.getIndex('books')
62+
63+
const documents = [
64+
{ book_id: 123, title: 'Pride and Prejudice' },
65+
{ book_id: 456, title: 'Le Petit Prince' },
66+
{ book_id: 1, title: 'Alice In Wonderland' },
67+
{ book_id: 1344, title: 'The Hobbit' },
68+
{ book_id: 4, title: 'Harry Potter and the Half-Blood Prince' },
69+
{ book_id: 42, title: "The Hitchhiker's Guide to the Galaxy" },
70+
]
71+
72+
let response = await index.addDocuments(documents)
73+
console.log(response) // => { "updateId": 0 }
74+
})()
7575
```
7676

7777
With the `updateId`, you can check the status (`processed` or `failed`) of your documents addition thanks to this [method](#update-status).
@@ -80,7 +80,8 @@ With the `updateId`, you can check the status (`processed` or `failed`) of your
8080

8181
```javascript
8282
// MeiliSearch is typo-tolerant:
83-
await index.search('harry pottre')
83+
const search = await index.search('harry pottre')
84+
console.log(search)
8485
```
8586

8687
Output:
@@ -107,27 +108,30 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
107108

108109
Go checkout [examples](./examples)!
109110

111+
In this section, the examples contain the [`await` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await). This operator is used to wait for a `Promise`.<br>
112+
Despite it is not detailled in the examples above, it can only be used inside an `async` function.
113+
110114
### Indexes
111115

112116
#### Create an index <!-- omit in toc -->
113117

114118
```javascript
115119
// Create an index
116-
meili.createIndex({ uid: 'books' }) // if your index does not exist
120+
const index = await client.createIndex({ uid: 'books' })
117121
// Create an index and give the primary-key
118-
meili.createIndex({ uid: 'books', primaryKey: 'book_id' }) // if your index does not exist
122+
const index = await client.createIndex({ uid: 'books', primaryKey: 'book_id' })
119123
```
120124

121125
#### List all indexes <!-- omit in toc -->
122126

123127
```javascript
124-
await meili.listIndexes()
128+
const indexes = await client.listIndexes()
125129
```
126130

127131
#### Get an index object <!-- omit in toc -->
128132

129133
```javascript
130-
const index = await meili.getIndex('books')
134+
const index = await client.getIndex('books')
131135
```
132136

133137
### Documents
@@ -136,16 +140,16 @@ const index = await meili.getIndex('books')
136140

137141
```javascript
138142
// Get one document
139-
let myDocument = await index.getDocument(123)
143+
const document = await index.getDocument(123)
140144

141145
// Get documents by batch
142-
let myDocuments = await index.getDocuments({ offset: 4, limit: 20 })
146+
const documents = await index.getDocuments({ offset: 4, limit: 20 })
143147
```
144148

145149
#### Add documents <!-- omit in toc -->
146150

147151
```javascript
148-
index.addDocuments([{ book_id: 2, title: 'Madame Bovary' }])
152+
await index.addDocuments([{ book_id: 2, title: 'Madame Bovary' }])
149153
```
150154

151155
Response:
@@ -162,11 +166,11 @@ With this `updateId` you can track your [operation update](#update-status).
162166

163167
```javascript
164168
// Delete one document
165-
index.deleteDocument(2)
169+
await index.deleteDocument(2)
166170
// Delete several documents
167-
index.deleteDocuments([1, 42])
171+
await index.deleteDocuments([1, 42])
168172
// Delete all documents /!\
169-
index.deleteAllDocuments()
173+
await index.deleteAllDocuments()
170174
```
171175

172176
### Update status
@@ -184,7 +188,7 @@ await index.getAllUpdateStatus()
184188
#### Basic search <!-- omit in toc -->
185189

186190
```javascript
187-
await index.search('prince')
191+
const search = await index.search('prince')
188192
```
189193

190194
```json
@@ -292,21 +296,21 @@ This package works for MeiliSearch `v0.9.x`.
292296

293297
- Make a search request:
294298

295-
`meili.getIndex('xxx').search(query: string, options?: Types.SearchParams): Promise<Types.SearchResponse>`
299+
`client.getIndex('xxx').search(query: string, options?: Types.SearchParams): Promise<Types.SearchResponse>`
296300

297301
### Indexes
298302

299303
- List all indexes:
300304

301-
`meili.listIndexes(): Promise<object[]>`
305+
`client.listIndexes(): Promise<object[]>`
302306

303307
- Create new index:
304308

305-
`meili.createIndex(data: Types.CreateIndexRequest): Promise<Types.CreateIndexResponse>`
309+
`client.createIndex(data: Types.CreateIndexRequest): Promise<Types.CreateIndexResponse>`
306310

307311
- Get index object:
308312

309-
`meili.getIndex(uid: string)`
313+
`client.getIndex(uid: string)`
310314

311315
- Show Index information:
312316

@@ -388,38 +392,38 @@ Waiting on MeiliSearch v0.9.0
388392

389393
- Check if the server is healthy
390394

391-
`meili.isHealthy(): Promise<void>`
395+
`client.isHealthy(): Promise<void>`
392396

393397
- Set the server healthy
394398

395-
`meili.setHealthy(): Promise<void>`
399+
`client.setHealthy(): Promise<void>`
396400

397401
- Set the server unhealthy
398402

399-
`meili.setUnhealthy(): Promise<void>`
403+
`client.setUnhealthy(): Promise<void>`
400404

401405
- Change the server healthyness
402406

403-
`meili.changeHealthTo(health: boolean): Promise<void>`
407+
`client.changeHealthTo(health: boolean): Promise<void>`
404408

405409
### Stats
406410

407411
- Get database stats
408412

409-
`meili.databaseStats(): Promise<object>`
413+
`client.databaseStats(): Promise<object>`
410414

411415
### Version
412416

413417
- Get binary version
414418

415-
`meili.version(): Promise<object>`
419+
`client.version(): Promise<object>`
416420

417421
### System
418422

419423
- Get system information
420424

421-
`meili.systemInformation(): Promise<object>`
425+
`client.systemInformation(): Promise<object>`
422426

423427
- Get system information (pretty mode)
424428

425-
`meili.systemInformationPretty(): Promise<object>`
429+
`client.systemInformationPretty(): Promise<object>`

0 commit comments

Comments
 (0)