mongodb recommendation V1
This commit is contained in:
parent
72b75d938c
commit
b36df03f9a
@ -8,7 +8,10 @@ package com.camillepradel.movierecommender.controller;
|
||||
import com.camillepradel.movierecommender.model.Genre;
|
||||
import com.camillepradel.movierecommender.model.Movie;
|
||||
import com.camillepradel.movierecommender.model.Rating;
|
||||
import com.mongodb.AggregationOutput;
|
||||
import com.mongodb.BasicDBList;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.client.AggregateIterable;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
@ -16,6 +19,7 @@ import com.mongodb.client.MongoDatabase;
|
||||
import static com.mongodb.client.model.Filters.eq;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@ -157,8 +161,108 @@ public class MongoDBController implements DBControllerInterface {
|
||||
}
|
||||
|
||||
public List<Rating> ProcessRecommendationV1(Integer userId) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
MongoDatabase db = MongoDBConnector.getInstance().getConnexion();
|
||||
MongoCollection<Document> users = db.getCollection("users");
|
||||
|
||||
BasicDBObject project;
|
||||
BasicDBObject match;
|
||||
BasicDBObject sort;
|
||||
BasicDBObject limit;
|
||||
BasicDBObject unwind;
|
||||
BasicDBObject group;
|
||||
|
||||
project = new BasicDBObject("$project",
|
||||
new BasicDBObject("moviesid", "$movies.movieid")
|
||||
);
|
||||
|
||||
match = new BasicDBObject("$match",
|
||||
new BasicDBObject("_id", new BasicDBObject("$eq", userId))
|
||||
);
|
||||
|
||||
AggregateIterable<Document> output = users.aggregate(Arrays.asList(project, match));
|
||||
|
||||
final Document moviesid = output.first();
|
||||
|
||||
BasicDBList intersectionList = new BasicDBList();
|
||||
intersectionList.add("$movies.movieid" );
|
||||
intersectionList.add( moviesid.get("moviesid") );
|
||||
|
||||
project = new BasicDBObject("$project",
|
||||
new BasicDBObject("nbMoviesIntersect",
|
||||
new BasicDBObject("$size",
|
||||
new BasicDBObject("$setIntersection",
|
||||
intersectionList
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
match = new BasicDBObject("$match",
|
||||
new BasicDBObject("_id", new BasicDBObject("$ne", userId))
|
||||
);
|
||||
|
||||
sort = new BasicDBObject("$sort",
|
||||
new BasicDBObject("nbMoviesIntersect", -1)
|
||||
);
|
||||
|
||||
limit = new BasicDBObject("$limit", 5);
|
||||
|
||||
output = users.aggregate(Arrays.asList(project, match, sort, limit));
|
||||
|
||||
Document simUser = output.first();
|
||||
|
||||
unwind = new BasicDBObject("$unwind", "$movies");
|
||||
|
||||
match = new BasicDBObject("$match",
|
||||
new BasicDBObject()
|
||||
.append("_id", simUser.getInteger("_id"))
|
||||
.append("movies.movieid", new BasicDBObject("$nin", moviesid.get("moviesid")))
|
||||
);
|
||||
|
||||
group = new BasicDBObject("$group", new BasicDBObject()
|
||||
.append("movies", new BasicDBObject("$push", "$movies"))
|
||||
);
|
||||
|
||||
sort = new BasicDBObject("$sort", new BasicDBObject("movies.rating", -1));
|
||||
|
||||
output = users.aggregate(Arrays.asList(unwind, match, sort, group));
|
||||
|
||||
Document listMoviesDiff = output.first();
|
||||
|
||||
|
||||
ArrayList<Rating> listRating= new ArrayList<Rating>();
|
||||
MongoCollection<Document> movies = db.getCollection("movies");
|
||||
MongoCursor<Document> cursor;
|
||||
|
||||
ArrayList<Document> user_movies = (ArrayList) listMoviesDiff.get("movies");
|
||||
BasicDBObject inQuery = new BasicDBObject();
|
||||
HashMap<Integer,Integer> list = new HashMap<Integer,Integer>();
|
||||
for(Document movie : user_movies )
|
||||
list.put(movie.getInteger("movieid"), movie.getInteger("rating"));
|
||||
|
||||
inQuery.put("_id", new BasicDBObject("$in", list.keySet()));
|
||||
cursor = movies.find(inQuery).iterator();
|
||||
|
||||
HashMap<Integer,Rating> hashMoviesRating = new HashMap<Integer,Rating>();
|
||||
while (cursor.hasNext()) {
|
||||
Document movie = cursor.next();
|
||||
String[] genres;
|
||||
genres = movie.getString("genres").split("\\|");
|
||||
ArrayList<Genre> listGenres = new ArrayList<Genre>();
|
||||
for(String genre : genres){
|
||||
listGenres.add(new Genre(1,genre));
|
||||
}
|
||||
hashMoviesRating.put(movie.getInteger("_id"),
|
||||
new Rating(new Movie(movie.getInteger("_id"), movie.getString("title"), listGenres),
|
||||
userId, list.get(movie.getInteger("_id"))));
|
||||
|
||||
}
|
||||
|
||||
for(Document movie : user_movies )
|
||||
listRating.add(hashMoviesRating.get(movie.getInteger("movieid")));
|
||||
|
||||
return listRating;
|
||||
}
|
||||
|
||||
public List<Rating> ProcessRecommendationV2(Integer userId) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
|
Reference in New Issue
Block a user