|
| 1 | +import { MeiliSearch } from '../../../src/lib/meilisearch' |
| 2 | + |
| 3 | +const config = { |
| 4 | + host: 'http://127.0.0.1:7700', |
| 5 | + apiKey: 'masterKey', |
| 6 | +} |
| 7 | + |
| 8 | +const client = new MeiliSearch(config) |
| 9 | +const uid = 'movies' |
| 10 | + |
| 11 | +const addDataset = async () => { |
| 12 | + const index = await client.getOrCreateIndex(uid) |
| 13 | + const documents = await index.getDocuments() |
| 14 | + |
| 15 | + const dataset = [ |
| 16 | + { id: 1, title: 'Carol', genres: ['Romance', 'Drama'] }, |
| 17 | + { id: 2, title: 'Wonder Woman', genres: ['Action', 'Adventure'] }, |
| 18 | + { id: 3, title: 'Life of Pi', genres: ['Adventure', 'Drama'] }, |
| 19 | + { |
| 20 | + id: 4, |
| 21 | + title: 'Mad Max: Fury Road', |
| 22 | + genres: ['Adventure', 'Science Fiction'], |
| 23 | + }, |
| 24 | + { id: 5, title: 'Moana', genres: ['Fantasy', 'Action'] }, |
| 25 | + { id: 6, title: 'Philadelphia', genres: ['Drama'] }, |
| 26 | + ] |
| 27 | + if (documents.length === 0) { |
| 28 | + const { updateId } = await index.addDocuments(dataset) |
| 29 | + await index.waitForPendingUpdate(updateId) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +;(async () => { |
| 34 | + await addDataset() |
| 35 | + try { |
| 36 | + const index = await client.getOrCreateIndex('movies') |
| 37 | + |
| 38 | + const resp = await index.search( |
| 39 | + 'Moana', |
| 40 | + { |
| 41 | + limit: 1, |
| 42 | + attributesToHighlight: ['title'], |
| 43 | + }, |
| 44 | + 'POST' |
| 45 | + ) |
| 46 | + console.log({ resp }) |
| 47 | + console.log({ hit: resp.hits[0] }) |
| 48 | + } catch (e) { |
| 49 | + console.error(e) |
| 50 | + } |
| 51 | +})() |
0 commit comments