Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Sort by date #23

Merged
merged 11 commits into from Jan 12, 2021
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -81,11 +81,11 @@ Don't forget to run `$ npm install` first, if you didn't do it in the previous s

This will do the following:

- Create an index called `artWorks` in your MeiliSearch instance.
- Create three indexes called `artWorks`, `artWorksAsc` and `artWorksDesc` in your MeiliSearch instance.

- Add all artworks documents to that index.
- Add all artworks documents to those indexes.

- Add custom settings for a more relevant search.
- Add custom settings to each one for a more relevant search.


### 5. Run the project
Expand All @@ -102,4 +102,4 @@ $ npm run serve

<hr>

**MeiliSearch** provides and maintains many **SDKs and Integration tools** like this one. We want to provide everyone with an **amazing search experience for any kind of project**. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
**MeiliSearch** provides and maintains many **SDKs and Integration tools** like the ones used in this project. We want to provide everyone with an **amazing search experience for any kind of project**. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
188 changes: 115 additions & 73 deletions setup/setup.js
Expand Up @@ -2,80 +2,69 @@ const MeiliSearch = require('meilisearch')
const dataset = require('./Artworks.json')
require('dotenv').config()

;(async () => {
// Create client
const client = new MeiliSearch({
host: process.env.VUE_APP_MEILISEARCH_HOST,
apiKey: process.env.VUE_APP_MEILISEARCH_API_KEY
})

// Create Index or get the existing one
const index = await client.getOrCreateIndex('artWorks', { primaryKey: 'ObjectID' })

// Check if index has is populated
const stats = await index.getStats()

if (stats.numberOfDocuments === dataset.length) {
console.log('Index "artWorks" is already populated')
return
}

console.log('Index "artWorks" created.')

// Add settings
const settings = {
distinctAttribute: null,
searchableAttributes: [
'Artist',
'Title',
'ArtistBio',
'Nationality',
'Gender',
'Date',
'Medium',
'Department',
'MultipleArtists',
'DateToSortBy'
],
displayedAttributes: [
'Title',
'Artist',
'ArtistBio',
'Nationality',
'Gender',
'Date',
'Medium',
'Dimensions',
'URL',
'Department',
'Classification',
'ThumbnailURL',
'MultipleArtists',
'DateToSortBy'
],
stopWords: ['a', 'an', 'the'],
synonyms: { },
attributesForFaceting: [
'Nationality', 'Gender', 'Classification'
]
}
await index.updateSettings(settings)
console.log('Settings added to "artWorks" index.')

// Process documents
const processedDataSet = dataProcessing(dataset)
const settings = {
distinctAttribute: null,
searchableAttributes: [
'Artist',
'Title',
'ArtistBio',
'Nationality',
'Gender',
'Date',
'Medium',
'Department',
'MultipleArtists',
'DateToSortBy'
],
displayedAttributes: [
'Title',
'Artist',
'ArtistBio',
'Nationality',
'Gender',
'Date',
'Medium',
'Dimensions',
'URL',
'Department',
'Classification',
'ThumbnailURL',
'MultipleArtists',
'DateToSortBy'
],
stopWords: ['a', 'an', 'the'],
synonyms: { },
attributesForFaceting: [
'Nationality', 'Gender', 'Classification'
]
}

// Add documents
const batchedDataSet = batch(processedDataSet, 10000)
console.log('Adding documents...')
for (let i = 0; i < batchedDataSet.length; i++) {
const { updateId } = await index.addDocuments(batchedDataSet[i])
await index.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
console.log('Documents added to "artWorks" index.')
})()
const rankingRulesAsc = [
'asc(DateToSortBy)',
'typo',
'words',
'proximity',
'attribute',
'wordsPosition',
'exactness'
]
const rankingRulesDesc = [
'desc(DateToSortBy)',
'typo',
'words',
'proximity',
'attribute',
'wordsPosition',
'exactness'
]
const defaultRankingRules = [
'typo',
'words',
'proximity',
'attribute',
'wordsPosition',
'exactness'
]

// Split dataset into batches
function batch (array, size) {
Expand Down Expand Up @@ -132,3 +121,56 @@ function dataProcessing (data) {
}
return processedDataArray
}

async function populateIndex ({ index, rules, name }, batchedDataSet) {
await index.updateSettings({ ...settings, rankingRules: rules })
console.log(`Settings added to ${name} index.`)
console.log(`Adding documents to ${name}...`)
for (const batch of batchedDataSet)
const { updateId } = await index.addDocuments(batch)
await index.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
}

async function indexIsPopulated (index, dataset) {
const indexStats = await index.getStats()
return indexStats.numberOfDocuments === dataset.length
}
;(async () => {
// Create client
const client = new MeiliSearch({
host: process.env.VUE_APP_MEILISEARCH_HOST,
apiKey: process.env.VUE_APP_MEILISEARCH_API_KEY
})

// Process documents
const processedDataSet = dataProcessing(dataset)

// Add documents batches array
const batchedDataSet = batch(processedDataSet, 10000)

// Get or create indexes

const artWorksIndex = await client.getOrCreateIndex('artWorks', { primaryKey: 'ObjectID' })
const artWorksAscIndex = await client.getOrCreateIndex('artWorksAsc', { primaryKey: 'ObjectID' })
const artWorksDescIndex = await client.getOrCreateIndex('artWorksDesc', { primaryKey: 'ObjectID' })

// Create Indexes array
const indexArray = [
{ name: 'artWorks', index: artWorksIndex, rules: defaultRankingRules },
{ name: 'artWorksAsc', index: artWorksAscIndex, rules: rankingRulesAsc },
{ name: 'artWorksDesc', index: artWorksDescIndex, rules: rankingRulesDesc }
]

for (const index of indexArray) {
const isPopulated = await indexIsPopulated(index.index, dataset)
if (isPopulated) {
console.log(`Index "${index.name}" already exists`)
} else {
await populateIndex(index, batchedDataSet)
console.log(`Documents added to "${index.name}"`)
}
}
})()
13 changes: 13 additions & 0 deletions src/App.vue
Expand Up @@ -17,6 +17,16 @@
<ais-search-box class="search-box" placeholder="Search here..." autofocus>
</ais-search-box>
<ais-stats/>
<ais-sort-by
:items="[
{ value: 'artWorks', label: 'Featured' },
{ value: 'artWorksAsc', label: 'Date asc.' },
{ value: 'artWorksDesc', label: 'Date desc.' },
]"
:class-names="{
'ais-SortBy': 'MyCustomSortBy'
}"
/>
<ais-current-refinements
:class-names="{
'ais-CurrentRefinements': 'MyCustomCurrentRefinements',
Expand Down Expand Up @@ -181,6 +191,9 @@ body {
.container {
padding: 1rem;
}
.MyCustomSortBy {
margin: 1rem;
}
.MyCustomCurrentRefinements {
margin: 1rem;
}
Expand Down