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
Merged

Sort by date #23

merged 11 commits into from Jan 12, 2021

Conversation

CaroFG
Copy link
Contributor

@CaroFG CaroFG commented Dec 23, 2020

No description provided.

@CaroFG CaroFG linked an issue Dec 23, 2020 that may be closed by this pull request
@fharper fharper removed their request for review December 28, 2020 16:33
@fharper
Copy link
Contributor

fharper commented Dec 28, 2020

I cannot review this since I have issue with my MeiliSearch installation right now, so maybe @curquiza can check it?

@curquiza
Copy link
Member

Hello @fharper and @CaroFG!

@fharper you can use docker to start MeiliSearch, do you still have issues on your computer when using docker?

$ docker run -it --rm  -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --no-analytics=true

@CaroFG can you fix the git conflict before we do any review? 🙂

I'll try to review it asap then, but not sure I'll have the time to do it before the end of this week, sorry 😢

@CaroFG
Copy link
Contributor Author

CaroFG commented Dec 30, 2020

Conflicts solved!

@fharper
Copy link
Contributor

fharper commented Dec 30, 2020

@fharper you can use docker to start MeiliSearch, do you still have issues on your computer when using docker?

No I can't, it's not stable yet on M1. Actually, Docker releases a tech preview version, I'll see if it's working. and I'll let you know if I can.

I'll try to review it asap then, but not sure I'll have the time to do it before the end of this week, sorry 😢

Don't apologize, it's nice enough of you to help while I can't test things myself yet. We appreciate a lot 💙

@fharper
Copy link
Contributor

fharper commented Dec 30, 2020

@curquiza I'm now able to run Meili on my machine using a compiled version, so unless you have time tomorrow to test this PR since I'm not there, I'll do it next year.

@fharper fharper self-requested a review December 30, 2020 20:51
@fharper fharper added the enhancement New feature or request label Dec 30, 2020
@fharper fharper added this to Backlog in Developer Relations via automation Dec 30, 2020
@curquiza
Copy link
Member

"next year" 🤣 Like it!
I will not have the time to do it this year either.

Copy link
Member

@curquiza curquiza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my requests for changes are not always related to this PR specifically

  1. I just realized, when you arrive at step 3 of the README (Donwload the dataset) you have not asked the user to do an npm install before, so you get an error: Error: Cannot find module 'axios'.
    FYI: npm install is present in step 4 but not before.

  2. The footer talks about "SDKs and Integration tools" -> is this repo an integration tool? I would say no, but maybe I'm wrong. If it's not, maybe adapt the footer for the dev rel team would be a good idea (or remove it).

  3. In the README at step 4, you write "Create an index called artWorks in your MeiliSearch instance." -> this is not true anymore if I'm not wrong 🙂


I tested the demo and it works! Well done!
My request for changes in the code will not impact the visual of your demo 🙂

setup/setup.js Outdated
Comment on lines 92 to 93
await client.createIndex('artWorksAsc', { primaryKey: 'ObjectID' })
const indexAsc = client.getIndex('artWorksAsc')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await client.createIndex('artWorksAsc', { primaryKey: 'ObjectID' })
const indexAsc = client.getIndex('artWorksAsc')
const indexAsc = await client.createIndex('artWorksAsc', { primaryKey: 'ObjectID' })

createIndex already returns an Index instance 🙂

Copy link
Contributor Author

@CaroFG CaroFG Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerning the README, number 1 had already been corrected in another PR, 2. I reformulated the footer sentence. Let me know what you think, please. 3. Updated it. Thank you for your feedback!!!!

setup/setup.js Outdated
Comment on lines 108 to 109
await client.createIndex('artWorksDesc', { primaryKey: 'ObjectID' })
const indexDesc = client.getIndex('artWorksDesc')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await client.createIndex('artWorksDesc', { primaryKey: 'ObjectID' })
const indexDesc = client.getIndex('artWorksDesc')
const indexDesc = await client.createIndex('artWorksDesc', { primaryKey: 'ObjectID' })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!! Corrected :)

setup/setup.js Outdated
Comment on lines 99 to 104
for (let i = 0; i < batchedDataSet.length; i++) {
const { updateId } = await indexAsc.addDocuments(batchedDataSet[i])
await indexAsc.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe could you use the forEach method to loop on your array instead of a for loop?
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/forEach

Your solution is not wrong but not the most idiomatic way to do it in JS, I think this is a good opportunity for you to practice the "functional" way of JavaScript by using the built-in methods the languages provides 🙂

More about looping on an array: https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript
A article Avoid Writing Another For-Loop in JavaScript : https://medium.com/better-programming/never-write-another-for-loop-in-javascript-9db11afa6445

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I tried, and the forEach function happens to have different behavior than the 'for loop' when used with async functions. The forEach method doesn't wait for the callback function to be done before calling the next one, which caused, in my case, MeiliSearch communication errors. So I'll stick to the 'for loop' this time.
But thank you so much for your feedback. I really appreciate the time you take to help me progress and teach me the best practices. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My pleasure! Oh wait... 🤣

setup/setup.js Outdated
Comment on lines 114 to 102
for (let i = 0; i < batchedDataSet.length; i++) {
const { updateId } = await indexDesc.addDocuments(batchedDataSet[i])
await indexDesc.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here about the for loop.

@fharper
Copy link
Contributor

fharper commented Jan 5, 2021

@CaroFG I will let you fix the points @curquiza made before reviewing it.

@CaroFG CaroFG requested a review from curquiza January 6, 2021 19:23
Copy link
Member

@curquiza curquiza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

There is still the npm install issue in the README, but maybe do you plan to fix it in another PR @CaroFG?

@CaroFG
Copy link
Contributor Author

CaroFG commented Jan 7, 2021

There is still the npm install issue in the README, but maybe do you plan to fix it in another PR @CaroFG?

Are you sure about that? Cause it was already solved in main and I rebased...

@curquiza
Copy link
Member

curquiza commented Jan 7, 2021

Are you sure about that? Cause it was already solved in main and I rebased...

Indeed! My bad Caro, I did not notice you rebased! Sorry! That's perfect 😊

Developer Relations automation moved this from Backlog to In Progress Jan 7, 2021
@bidoubiwa
Copy link
Contributor

bidoubiwa commented Jan 7, 2021

In the README at step 4, you write "Create an index called artWorks in your MeiliSearch instance." -> this is not true anymore if I'm not wrong 🙂

I think those steps are not what the user is supposed to do but what the script npm run prep-meili will do. Even with the lazy index, the script will create that index (unless I understood the readme badly in which case sorry sorry)

@CaroFG CaroFG requested review from bidoubiwa and removed request for tpayet January 7, 2021 09:34
Copy link
Contributor

@bidoubiwa bidoubiwa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest replacing all for loops with maps. But this can be done in another PR as it is may be not the main purpose of this PR.

Good job on this PR !!

setup/setup.js Outdated
Comment on lines 161 to 173
async function populateIndex (array, batchedDataSet) {
settings.rankingRules = array.rules
const index = array.index
await index.updateSettings(settings)
console.log(`Settings added to ${array.name} index.`)
console.log(`Adding documents to ${array.name}...`)
for (let i = 0; i < batchedDataSet.length; i++) {
const { updateId } = await index.addDocuments(batchedDataSet[i])
await index.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more modern approach to these function parameters would be to use destructuring in function arguments and spread operators to construct your objects.

destructuring

If you don't know destructuring basically it looks like this:

const obj = { name: "Caro", age: "way older than charlotte" }
const { name, age } = obj;
console.log(name) // Caro
console.log(age) // way older than charlotte

This can also be used in functions the following way:

const user = {
  id: 42,
  displayName: 'jdoe',
  fullName: {
    firstName: 'John',
    lastName: 'Doe'
  }
};

function whois({displayName, fullName: {firstName: name}}) {
  return `${displayName} is ${name}`;
}

console.log(whois(user));  // "jdoe is John"

Spread operators

Spread operators can be used for a variety of things, in our case, we will use it to ease the merge objects togethers.

A quick example with arrays first where instead of using the concat function of javascript, we use the spread operator to merge both arrays.

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];

arr1 = [...arr1, ...arr2];
//  arr1 is now [0, 1, 2, 3, 4, 5]
let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };

let clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }

let mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }

As you can observe the order of the objects is really important, as in this situation let mergedObj = { ...obj1, ...obj2 };, all attributes found both in obj1 and obj2 will be overwritten by => obj2 foo becomes baz in mergedObj.

To adapt that to your code it would look like this:

Suggested change
async function populateIndex (array, batchedDataSet) {
settings.rankingRules = array.rules
const index = array.index
await index.updateSettings(settings)
console.log(`Settings added to ${array.name} index.`)
console.log(`Adding documents to ${array.name}...`)
for (let i = 0; i < batchedDataSet.length; i++) {
const { updateId } = await index.addDocuments(batchedDataSet[i])
await index.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
}
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 (let i = 0; i < batchedDataSet.length; i++) {
const { updateId } = await index.addDocuments(batchedDataSet[i])
await index.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you soooo much for all the explanations, I truly appreciate the time you've taken to review my code and help me progress. I'll do a PR for the "for loops"

setup/setup.js Outdated
Comment on lines 175 to 182
async function indexIsPopulated (index, dataset) {
const indexStats = await index.getStats()
if (indexStats.numberOfDocuments === dataset.length) {
return true
} else {
return false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async function indexIsPopulated (index, dataset) {
const indexStats = await index.getStats()
if (indexStats.numberOfDocuments === dataset.length) {
return true
} else {
return false
}
}
async function indexIsPopulated (index, dataset) {
const indexStats = await index.getStats()
return indexStats.numberOfDocuments === dataset.length
}

setup/setup.js Outdated
Comment on lines 94 to 101
for (let i = 0; i < indexArray.length; i++) {
const isPopulated = await indexIsPopulated(indexArray[i].index, dataset)
if (isPopulated) {
console.log(`Index "${indexArray[i].name}" already exists`)
} else {
await populateIndex(indexArray[i], batchedDataSet)
console.log(`Documents added to "${indexArray[i].name}"`)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned, changing all for into map could be in another PR as it should recieve a particular attention.
Now, we can still talk about the choice of for loop!

Do you know about for..of and for..in

for of

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"

for in

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"

You can update your code accordingly with something like this:

Suggested change
for (let i = 0; i < indexArray.length; i++) {
const isPopulated = await indexIsPopulated(indexArray[i].index, dataset)
if (isPopulated) {
console.log(`Index "${indexArray[i].name}" already exists`)
} else {
await populateIndex(indexArray[i], batchedDataSet)
console.log(`Documents added to "${indexArray[i].name}"`)
}
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}"`)
}

setup/setup.js Outdated
await index.waitForPendingUpdate(updateId, {
timeOutMs: 100000
})
const indexArray = [artWorks, artWorksAsc, artWorksDesc]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

setup/setup.js Outdated
Comment on lines 88 to 90
const artWorks = { name: 'artWorks', index: artWorksIndex, rules: defaultRankingRules }
const artWorksAsc = { name: 'artWorksAsc', index: artWorksAscIndex, rules: rankingRulesAsc }
const artWorksDesc = { name: 'artWorksDesc', index: artWorksDescIndex, rules: rankingRulesDesc }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const artWorks = { name: 'artWorks', index: artWorksIndex, rules: defaultRankingRules }
const artWorksAsc = { name: 'artWorksAsc', index: artWorksAscIndex, rules: rankingRulesAsc }
const artWorksDesc = { name: 'artWorksDesc', index: artWorksDescIndex, rules: rankingRulesDesc }

see next suggestion.

setup/setup.js Outdated

// Process documents
const processedDataSet = dataProcessing(dataset)
// Check if index are populated and populate them is needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not accurate comment

Suggested change
// Check if index are populated and populate them is needed

Copy link
Contributor

@bidoubiwa bidoubiwa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, I approve this PR :)

setup/setup.js Outdated Show resolved Hide resolved
Co-authored-by: cvermand <33010418+bidoubiwa@users.noreply.github.com>
@CaroFG CaroFG requested a review from bidoubiwa January 11, 2021 17:11
Copy link
Contributor

@bidoubiwa bidoubiwa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :) Since there are no tests, could you check if everything still works correctly?

@CaroFG
Copy link
Contributor Author

CaroFG commented Jan 12, 2021

LGTM :) Since there are no tests, could you check if everything still works correctly?

Yes, it does

@CaroFG CaroFG merged commit 523563f into main Jan 12, 2021
Developer Relations automation moved this from In Progress to Completed Jan 12, 2021
@CaroFG CaroFG mentioned this pull request Jan 12, 2021
@CaroFG CaroFG deleted the sort-by-date branch February 10, 2021 08:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Create ascending and descending index to sort by date
4 participants