Skip to content

Nicolas TERRIEN et Philippe RATEAU #11

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 4 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

Expand All @@ -26,6 +28,10 @@ public static RestHighLevelClient createClient() {

public List<String> getActorsSuggests(String searchQuery) throws IOException {
// TODO
// Ne marche pas
SearchRequest searchRequest = null;
RequestOptions options = null;
this.client.search(searchRequest, options);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public Neo4JRepository() {
public List<Map<String, GraphItem>> getConnectionsToKevinBacon(String actorName) {
Session session = driver.session();

// TODO
// la requête cypher :
String request = "MATCH (KevinB:Actor {name: \"Bacon, Kevin (I)\"} ), (a:Actor {name: " + actorName + "}), p = shortestPath((KevinB)-[:PLAYED_IN*]-(Al)) RETURN p";
// TODO : reste à lancer la requete depuis java et de formater le résultat...

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public RedisRepository() {

public List<String> getLastTenSearches() {
// TODO
// J'imagine qu'il faut trouver le bon truc genre oracleOfBacon:lastsearches dans le get
this.jedis.get("");
return null;
}
}
104 changes: 67 additions & 37 deletions script/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,86 @@ const csv = require("csv-parser");
const fs = require("fs");
const { Client } = require("@elastic/elasticsearch");

// /!\ il faut mettre le dossier imbd-data dans le dossier racine du projet

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

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

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
})
// 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)

// TODO créer l'index (et plus pour la ré-exécution ?)
// Drop index if exists
await client.indices.delete({
index: INDEX_NAME,
ignore_unavailable: true
});

// Création de l'indice
client.indices.create({ index: INDEX_NAME }, (err, resp) => {
if (err) console.trace(err.message);
});

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", name => {
///console.log("name:", name)
actors.push(name['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)
body = createBulkInsertQueries(actors, 10000)
// client.bulk(createBulkInsertQuery(actors), (err, resp) => {
// if (err) console.trace(err.message);
// else console.log(`Inserted ${resp.body.items.length} actors`);
// client.close();
// });
recBulk(client, body)
});
}

function recBulk(client, bulks) {
console.log("remaining bulks " + bulks.length);
if (bulks.length <= 0) {
return Promise.resolve();
}

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

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

function createBulkInsertQueries(names, length) {
// TODO
body = createBulkInsertQuery(names).body
console.log(body[0])
size = Math.floor(body.length / length)
bodies = []
for (i = 0; i < length; i++) {
bodies.push({ body: body.slice(i * size, (i + 1) * size) })
}
bodies.push({ body: body.slice((length + 1) * size, body.length - 1) })
return bodies
}

// Fonction utilitaire permettant de formatter les données pour l'insertion "bulk" dans elastic
function createBulkInsertQuery(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()));

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

return { body };
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()));

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

return { body };
}

insert().catch(console.error);
insert().catch(console.error);