Skip to content

Commit e8f3dc8

Browse files
authored
Merge pull request #276 from meilisearch/fix-getting-started
Fix Getting Started in README
2 parents f4fa981 + 0f1167c commit e8f3dc8

File tree

1 file changed

+52
-47
lines changed

1 file changed

+52
-47
lines changed

README.md

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,31 @@ 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+
// Or if you are on a front-end environment:
54+
import MeiliSearch from 'meilisearch';
55+
56+
(async () => {
57+
const client = new MeiliSearch({
58+
host: 'http://127.0.0.1:7700',
59+
apiKey: 'masterKey'
60+
})
61+
62+
await client.createIndex({ uid: 'books' }) // only if your index does not exist
63+
const index = client.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+
let response = await index.addDocuments(documents)
75+
console.log(response) // => { "updateId": 0 }
76+
})()
7577
```
7678

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

8183
```javascript
8284
// MeiliSearch is typo-tolerant:
83-
await index.search('harry pottre')
85+
const search = await index.search('harry pottre')
86+
console.log(search)
8487
```
8588

8689
Output:
@@ -107,27 +110,29 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
107110

108111
Go checkout [examples](./examples)!
109112

113+
In this section, the examples contain the [`await` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await).
114+
110115
### Indexes
111116

112117
#### Create an index <!-- omit in toc -->
113118

114119
```javascript
115120
// Create an index
116-
meili.createIndex({ uid: 'books' }) // if your index does not exist
121+
await client.createIndex({ uid: 'books' })
117122
// Create an index and give the primary-key
118-
meili.createIndex({ uid: 'books', primaryKey: 'book_id' }) // if your index does not exist
123+
const index = await client.createIndex({ uid: 'books', primaryKey: 'book_id' })
119124
```
120125

121126
#### List all indexes <!-- omit in toc -->
122127

123128
```javascript
124-
await meili.listIndexes()
129+
const indexes = await client.listIndexes()
125130
```
126131

127132
#### Get an index object <!-- omit in toc -->
128133

129134
```javascript
130-
const index = await meili.getIndex('books')
135+
const index = await client.getIndex('books')
131136
```
132137

133138
### Documents
@@ -136,16 +141,16 @@ const index = await meili.getIndex('books')
136141

137142
```javascript
138143
// Get one document
139-
let myDocument = await index.getDocument(123)
144+
const document = await index.getDocument(123)
140145

141146
// Get documents by batch
142-
let myDocuments = await index.getDocuments({ offset: 4, limit: 20 })
147+
const documents = await index.getDocuments({ offset: 4, limit: 20 })
143148
```
144149

145150
#### Add documents <!-- omit in toc -->
146151

147152
```javascript
148-
index.addDocuments([{ book_id: 2, title: 'Madame Bovary' }])
153+
await index.addDocuments([{ book_id: 2, title: 'Madame Bovary' }])
149154
```
150155

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

163168
```javascript
164169
// Delete one document
165-
index.deleteDocument(2)
170+
await index.deleteDocument(2)
166171
// Delete several documents
167-
index.deleteDocuments([1, 42])
172+
await index.deleteDocuments([1, 42])
168173
// Delete all documents /!\
169-
index.deleteAllDocuments()
174+
await index.deleteAllDocuments()
170175
```
171176

172177
### Update status
@@ -184,7 +189,7 @@ await index.getAllUpdateStatus()
184189
#### Basic search <!-- omit in toc -->
185190

186191
```javascript
187-
await index.search('prince')
192+
const search = await index.search('prince')
188193
```
189194

190195
```json
@@ -292,21 +297,21 @@ This package works for MeiliSearch `v0.9.x`.
292297

293298
- Make a search request:
294299

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

297302
### Indexes
298303

299304
- List all indexes:
300305

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

303308
- Create new index:
304309

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

307312
- Get index object:
308313

309-
`meili.getIndex(uid: string)`
314+
`client.getIndex(uid: string)`
310315

311316
- Show Index information:
312317

@@ -388,38 +393,38 @@ Waiting on MeiliSearch v0.9.0
388393

389394
- Check if the server is healthy
390395

391-
`meili.isHealthy(): Promise<void>`
396+
`client.isHealthy(): Promise<void>`
392397

393398
- Set the server healthy
394399

395-
`meili.setHealthy(): Promise<void>`
400+
`client.setHealthy(): Promise<void>`
396401

397402
- Set the server unhealthy
398403

399-
`meili.setUnhealthy(): Promise<void>`
404+
`client.setUnhealthy(): Promise<void>`
400405

401406
- Change the server healthyness
402407

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

405410
### Stats
406411

407412
- Get database stats
408413

409-
`meili.databaseStats(): Promise<object>`
414+
`client.databaseStats(): Promise<object>`
410415

411416
### Version
412417

413418
- Get binary version
414419

415-
`meili.version(): Promise<object>`
420+
`client.version(): Promise<object>`
416421

417422
### System
418423

419424
- Get system information
420425

421-
`meili.systemInformation(): Promise<object>`
426+
`client.systemInformation(): Promise<object>`
422427

423428
- Get system information (pretty mode)
424429

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

0 commit comments

Comments
 (0)