Skip to content

Agustin ZORZANO et Helder BETIOL #16

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 @@ -67,11 +67,12 @@ public String getConnectionsToKevinBacon(String actorName) {

@Get("suggest?q=:searchQuery")
public List<String> getActorSuggestion(String searchQuery) throws IOException {
return Arrays.asList("Niro, Chel",
"Senanayake, Niro",
"Niro, Juan Carlos",
"de la Rua, Niro",
"Niro, Simão");
// return Arrays.asList("Niro, Chel",
// "Senanayake, Niro",
// "Niro, Juan Carlos",
// "de la Rua, Niro",
// "Niro, Simão");
return elasticSearchRepository.getActorsSuggests(searchQuery);
}

@Get("last-searches")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package com.serli.oracle.of.bacon.repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;

public class ElasticSearchRepository {
public static final String INDEX = "actors";

private final RestHighLevelClient client;

Expand All @@ -26,6 +42,46 @@ public static RestHighLevelClient createClient() {

public List<String> getActorsSuggests(String searchQuery) throws IOException {
// TODO
return null;
/*
GET actors/_search
{
"suggest": {
"mySuggest": {
"text": "niro",
"completion": {
"field": "suggest",
"skip_duplicates": true
}
}
}
}
*/

final List<String> suggestions = new ArrayList<String>();

SearchRequest searchRequest = new SearchRequest(ElasticSearchRepository.INDEX);
SuggestBuilder suggestBuilder = new SuggestBuilder();
SearchSourceBuilder searchBuilder = new SearchSourceBuilder();


SuggestionBuilder suggestionBuilder = SuggestBuilders.completionSuggestion("suggest")
.size(5).prefix(searchQuery).skipDuplicates(true);
suggestBuilder.addSuggestion("suggestion", suggestionBuilder);
searchBuilder.suggest(suggestBuilder);

searchRequest.source(searchBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("suggestion");
List<CompletionSuggestion.Entry> entries = completionSuggestion.getEntries();

entries.forEach(entry -> {
entry.getOptions().forEach(option -> {
suggestions.add(option.getHit().getSourceAsMap().get("name").toString());
});
});

return suggestions;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ public List<Map<String, GraphItem>> getConnectionsToKevinBacon(String actorName)
Session session = driver.session();

// TODO
// Result result = session.writeTransaction( new TransactionWork<String>()
// {
// @Override
// public String execute( Transaction tx )
// {
// Result result = tx.run( "MATCH p= " +
// "(bacon:Person {name:\"Bacon, Kevin (I)\"})-[*]-(actor:Person {name:\""+
// actorName+"\"}) " + "RETURN p");
// return result;
// }
// } );
// System.out.println( result );
/* Le plus court chemin :
MATCH p=shortestPath(
(bacon:Person {name:"Kevin Bacon"})-[*]-(actor:Person {name:"Al Pacino"})
)
RETURN p
*/
return null;
}

Expand Down
47 changes: 39 additions & 8 deletions script/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@ const fs = require("fs");
const { Client } = require("@elastic/elasticsearch");

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

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

await client.indices.create({
index: INDEX_NAME,
body : {
"mappings": {
"properties": {
"name": { "type": "text" },
"suggest": { "type": "completion" },
}
}
}
});

let actors = [];
let first = true;
fs.createReadStream("./imdb-data/actors.csv")
fs.createReadStream("/home/agustin/Desktop/IMT/big_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 ({ name }) => { // on a change la premiere ligne du csv : name:ID -> name
// ajouter les acteurs dans la liste
actors.push(name);
})
// 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)
.on("end", async () => {
// insérer dans elastic
recBulk(client, createBulkInsertQueries(actors, 100000)).then(res => {
console.log(res);
});
});
}

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

function createBulkInsertQueries(names, length) {
// TODO
let i;
let slices = []
let bulks = []
for(i = 0; i <= names.length / length; i++) {
slices.push(names.slice(i, i + length));
}
slices.forEach(slice => {
bulks.push(createBulkInsertQuery(slice))
});
return bulks;
}

// 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({ index: { _index: "actors", _type: "_doc" } });
acc.push({ name, suggest });
return acc;
}, []);
Expand Down