Skip to content
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

last commit #6

Merged
merged 1 commit into from
Mar 24, 2021
Merged
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 @@ -51,28 +51,34 @@ public static void main(String[] args) throws IOException, InterruptedException
// Ajout de elasticsearch
if(count.get() == 0) {
count.getAndIncrement();
return;
}
else {
String[] input = line.split(",");
String suggestionArray = "[";
suggestionArray += "\"" + line.replace("\"", "") + "\"";
String[] input = line.split(",");
StringBuilder suggestionArray = new StringBuilder("[");
suggestionArray.append("\"")
.append(line.replace("\"", ""))
.append("\"");

if (input.length == 2) {
suggestionArray += ", \"" + input[1].replace("\"", "") + ", " + input[0].replace("\"", "") + "\"";
}
if (input.length == 2) {
suggestionArray.append(", \"")
.append(input[1].replace("\"", ""))
.append(", ")
.append(input[0].replace("\"", ""))
.append("\"");
}

suggestionArray += "]";
suggestionArray.append("]");

String jsonString = "{ \"name_suggest\": {\"input\": " + suggestionArray + " }, \"name\": \""+ line.replace("\"", "") + "\"}";
request.add(
new IndexRequest("actors")
.id(Integer.toString(count.getAndIncrement()))
.type("_doc")
.source(jsonString, XContentType.JSON)
);
if(count.get() % BULK_SIZE == 0) {
doRequest();
}

String jsonString = "{ \"name_suggest\": {\"input\": " + suggestionArray.toString() + " }, \"name\": \""+ line.replace("\"", "") + "\"}";
request.add(
new IndexRequest("actors")
.id(Integer.toString(count.getAndIncrement()))
.type("_doc")
.source(jsonString, XContentType.JSON)
);
if(count.get() % BULK_SIZE == 0) {
doRequest();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import org.elasticsearch.search.suggest.term.TermSuggestion;

public class ElasticSearchRepository {

private final RestHighLevelClient client;
private static final String FIELD_NAME = "name";
private static final String ACTORS_COMPLETION = "actors_completion";
private static final String SUGGESTION_FIELD = "name_suggest";
private static final String INDEX = "actors";
private final RestHighLevelClient client;

public ElasticSearchRepository() {
client = createClient();
Expand All @@ -33,27 +36,28 @@ public static RestHighLevelClient createClient() {
}

public List<String> getActorsSuggests(String searchQuery) throws IOException {
SearchRequest searchRequest = new SearchRequest("actors");

SearchRequest searchRequest = new SearchRequest(INDEX);

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SuggestionBuilder completionSuggestionBuilder =
SuggestBuilders.completionSuggestion("name_suggest").text(searchQuery).size(10);
SuggestBuilders.completionSuggestion(SUGGESTION_FIELD).text(searchQuery).size(10);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("actors_completion", completionSuggestionBuilder);
suggestBuilder.addSuggestion(ACTORS_COMPLETION, completionSuggestionBuilder);
searchSourceBuilder.suggest(suggestBuilder);

searchRequest.source(searchSourceBuilder);

SearchResponse searchResponse = client.search(searchRequest);

// retrieving suggestion
// Récupération des suggestions
Suggest suggest = searchResponse.getSuggest();
CompletionSuggestion completionSuggestion = suggest.getSuggestion("actors_completion");
CompletionSuggestion completionSuggestion = suggest.getSuggestion(ACTORS_COMPLETION);

List<String> suggestions = new LinkedList<>();
for (CompletionSuggestion.Entry entry : completionSuggestion.getEntries()) {
for (CompletionSuggestion.Entry.Option option : entry) {
suggestions.add((String) option.getHit().getSourceAsMap().get("name"));
suggestions.add((String) option.getHit().getSourceAsMap().get(FIELD_NAME));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ public Neo4JRepository() {
this.driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "motdepasse"));
}

public List<GraphItem> getConnectionsToKevinBacon(String actorName) {
private String getQuery() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("MATCH p = shortestPath((Kevin:Actor {name: \"Bacon, Kevin (I)\"})-[:PLAYED_IN*]-(Other:Actor {name: {nomAutreActeur}})) ")
.append("RETURN p")
;
return strBuilder.toString();
}

public List<GraphItem> getConnectionsToKevinBacon(String actorName) {
Session session = driver.session();
// Implémentation de l'oracle de Bacon
List<GraphItem> graphe = new LinkedList<>();
Expand All @@ -41,15 +49,13 @@ public List<GraphItem> getConnectionsToKevinBacon(String actorName) {

while (resultats.hasNext()) {
Record entree = resultats.next();
System.out.println(entree);
List<Value> valeurs = entree.values();

// normalement il n'y a qu'un path par Record

for (Value valeur : valeurs) {
Path chemin = valeur.asPath();

chemin.nodes().forEach(node -> {
// |)4r|< |\/|4g1(
String type = node.labels().iterator().next();

String clePropriete = "Actor".equals(type) ? "name" : "title";
Expand All @@ -65,14 +71,6 @@ public List<GraphItem> getConnectionsToKevinBacon(String actorName) {
return graphe;
}

private String getQuery() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("MATCH p = shortestPath((Kevin:Actor {name: \"Bacon, Kevin (I)\"})-[:PLAYED_IN*]-(Other:Actor {name: {nomAutreActeur}})) ")
.append("RETURN p")
;
return strBuilder.toString();

}

private GraphEdge mapRelationShipToNodeEdge(Relationship relationship) {
return new GraphEdge(relationship.id(), relationship.startNodeId(), relationship.endNodeId(), relationship.type());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
import java.util.Arrays;

public class RedisRepository {
private final Jedis jedis;
private static final int HISTORY_MAX_LENGTH = 9;

private final Jedis jedis;
private final static String KEY = "SearchHistory";

public RedisRepository() {
this.jedis = new Jedis("localhost");
}
public void saveSearch(String query) {
jedis.lpush(KEY, query);
jedis.ltrim(KEY, 0, 9);
jedis.ltrim(KEY, 0, HISTORY_MAX_LENGTH);
}
public List<String> getLastTenSearches() {
String[] last10 = jedis.lrange(KEY, 0, -1).toArray(new String[0]);
Expand Down