Skip to content

version of Maxime Laug and Charlie Pauvré #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions oracle-of-bacon-backend/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>oracle-of-bacon-backend</name>
<comment>Project oracle-of-bacon-backend created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1616599270626</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
13 changes: 13 additions & 0 deletions oracle-of-bacon-backend/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=C\:/Program Files/Java/jdk-11.0.10
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
49 changes: 35 additions & 14 deletions script/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@ const csv = require("csv-parser");
const fs = require("fs");
const { Client } = require("@elastic/elasticsearch");

const client = new Client({ node: "http://localhost:9200" });

async function insert() {
// TODO créer l'index (et plus pour la ré-exécution ?)


const client = new Client({ node: "http://localhost:9200" });

await client.indices.delete({
index: 'bacon',
ignore_unavailable: true
});

await client.indices.create({ index: 'bacon' });

let actors = [];
let first = true;
fs.createReadStream("./imdb-data/actors.csv")
.pipe(csv())
// Pour chaque ligne on créé un document JSON pour l'acteur correspondant
.on("data", async ({ name }) => {
// TODO ajouter les acteurs au tableau
.on("data", async (data) => {
//console.log(data['name:ID'])
actors.push({
name: data['name:ID']
});
})
// A la fin on créé l'ensemble des acteurs dans ElasticSearch
.on("end", () => {
// TODO insérer dans elastic (les fonctions ci-dessous peuvent vous aider)
client.bulk(createBulkInsertQueries(actors.map(actor => actor.name), 10000), (err, resp) => {
if (err) console.trace(err.message);
else console.log(`Inserted ${resp.body.items.length} actors`);
client.close();
});
});
}

Expand All @@ -28,29 +43,35 @@ function recBulk(client, bulks) {
}

const first = bulks.pop();
return client.bulk(first).then((r) => {
return client.bulk(first).then(() => {
console.log("Done inserting " + first.body.length / 2);
return recBulk(client, bulks);
});
}

function createBulkInsertQueries(names, length) {
// TODO
const arrays = [];
while (names.length > 0) {
arrays.push(names.splice(0, length));
}

return arrays.map((arr) => createBulkInsertQuery(arr));
}

// Fonction utilitaire permettant de formatter les données pour l'insertion "bulk" dans elastic
function createBulkInsertQuery(names) {
//console.log(names);
const body = names.reduce((acc, name) => {
const suggest = [name];
const parts = name.split(",");
parts.forEach((part) => suggest.push(part.trim()));
parts.reverse().forEach((part) => suggest.push(part.trim()));
// const suggest = [name];
// const parts = name.split(",");
// parts.forEach((part) => suggest.push(part.trim()));
// parts.reverse().forEach((part) => suggest.push(part.trim()));

acc.push({ index: { _index: "actor", _type: "_doc" } });
acc.push({ name, suggest });
acc.push({ index: { _index: "bacon" } });
acc.push({ name });
return acc;
}, []);

//console.log(body);
return { body };
}

Expand Down