Likes and Favorites

Valid from Datafari X.X

Problematic :

Users have the possibility to store pointers to documents shown in results list.

In the following document, “favorites” corresponds to the functionality where a connected user can save a pointer to a document he found in the results list.

Technically wise, we use Apache Cassandra for saving all the favorites.

Using Cassandra, we cannot use standard relational db schemas, like the following:

Modeling with Nosql in mind:

For our nosql modeling, we took our inspiration from this article http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/ aggregation-with-user-preference-data.jpeg .

Applied to our problematic, we get the following :

That means we have dedicated to favorites (which is distinct from the collection dedicated to user authentication and authorization).

The request :

The aim is to get for the connected user the list of favorite documents out of the ones which are currently displayed. After the standard search query, we send only one http request which contains only the username . In return, we get all the documents in the index that are favorites.

On the browser side, we go through this documents list and pick the ones that match the currently displayed documents to identify the favorites.

The Implementation :

There are two parts: Frontend and backend. For the frontend, we added a widget that builds the inteface and makes all the Ajax Requests. For the backend, we created two classes: Like and Favorite. We created the corresponding servlets that use these two classes and send back the result.

  1. Frontend :

    The widget is called LikesAndFavoritesWidget. This widget inherits from ResultWidget and is only included in SearchView.jsp if the functionality of likes was activated by the admin.

    This inheritance is important because we need to execute first the code of the ResultWidget and then the LikesAndFavoriteWidget.

    Once the buildWidget is executed, we execute the beforeRequest which will add the fl nbLikes:field(nbLikes) in the Solr query so we can retrieve as part of the results the number of likes per document.

    Then the afterRequest will fire. The afterRequest retrieves the documents liked and saved by the user from the server (ajax request) and stores it in window.globalVariableLikes and window.globalVariableFavorites. Then we parse the results retrieved from Solr and put the correct values in html by checking if the doc belong in the window.globalVariableLikes and window.globalVariableFavorites.

    Next, we put a listener to the “Like” button and a listener for saving a favorite. Depending on which button is clicked, we send a request to add/delete the favorite or the like from the server and from the lists saved in the global variable window of javascript.

Illustration 1: State Diagram of AfterRequest of LikesAndFavoritesWidget

  1. Backend :

We use two major classes which are Like And Favorite. They provide the method that will get, delete and add a Favorite or a Like by querying the Cassandra database. Whenever a change is needed to likes and favorites, we use directly these classes.

Like.class and Favorite.class belong to the package com.francelabs.datafari.user which also contains UserConstants.class where we put all the constant Strings used in this package.

We also use the following servlets:

  • AddFavorite: add a document to favorites,

  • DeleteFavorite: delete a document from favorites,

  • GetLikesAndFavorites: gets all the likes and the favorites of a user,,, .

AddLike, AddFavorite, DeleteFavorite are very similar. Therefore we will detail only one of them, the others can be derived from it. This is the sequence diagram involving the AddLike servlet:

Illustration 2: Sequence Diagram of AddLike for a normal Behaviour

Here is the sequence diagram that involves the LikesAndFavoritesWidget to retrieve the likes and favorites of a user :

Illustration 3: Sequence Diagram of getting the likes and favorites from server


Valid from Datafari 2.0 up to X.X

Problematic :

Starting with Datafari 2.0, users have the possibility to store pointers to documents shown in results list.

In the following document, “likes” corresponds to the functionality where a connected user can like or unlike a document, and “favorites” corresponds to the functionality where a connected user can save a pointer to a document he found in the results list.

Technically wise, we use Apache Cassandra for saving all the likes and favorites and Solr for saving the counters of likes of each document.

Using Cassandra, we cannot use standard relational db schemas, like the following:

Modeling with Nosql in mind:

For our nosql modeling, we took our inspiration from this article http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/ aggregation-with-user-preference-data.jpeg .

Applied to our problematic, we get the following :

That means we have dedicated to likes and favorites (which is distinct from the collection dedicated to user authentication and authorization).

The request :

The aim is to get for the connected user the list of documents he liked out of the ones which are currently displayed. After the standard search query, we send only one http request which contains only the username . In return, we get all the documents in the index that are liked, as well as the list of favorites.

On the browser side, we go through this documents list and pick the ones that match the currently displayed documents to identify the favorites and the liked.

In order to display the total number of likes per documents, we use Solr and its External File Fields mechanism ( https://cwiki.apache.org/confluence/display/solr/Working+with+External+Files+and+Processes).

Every time a connected user like or unlikes a document, we increment or decrement the counter of the document in the file located in SOLR_HOME/FileShare/data/externalk_nbLikes.

The Implementation :

There are two parts: Frontend and backend. For the frontend, we added a widget that builds the inteface and makes all the Ajax Requests. For the backend, we created two classes: Like and Favorite. We created the corresponding servlets that use these two classes and send back the result.

  1. Frontend :

    The widget is called LikesAndFavoritesWidget. This widget inherits from ResultWidget and is only included in SearchView.jsp if the functionality of likes was activated by the admin.

    This inheritance is important because we need to execute first the code of the ResultWidget and then the LikesAndFavoriteWidget.

    Once the buildWidget is executed, we execute the beforeRequest which will add the fl nbLikes:field(nbLikes) in the Solr query so we can retrieve as part of the results the number of likes per document.

    Then the afterRequest will fire. The afterRequest retrieves the documents liked and saved by the user from the server (ajax request) and stores it in window.globalVariableLikes and window.globalVariableFavorites. Then we parse the results retrieved from Solr and put the correct values in html by checking if the doc belong in the window.globalVariableLikes and window.globalVariableFavorites.

    Next, we put a listener to the “Like” button and a listener for saving a favorite. Depending on which button is clicked, we send a request to add/delete the favorite or the like from the server and from the lists saved in the global variable window of javascript.

Illustration 1: State Diagram of AfterRequest of LikesAndFavoritesWidget

     2. Backend :

We use two major classes which are Like And Favorite. They provide the method that will get, delete and add a Favorite or a Like by querying the Cassandra database. Whenever a change is needed to likes and favorites, we use directly these classes.

Like.class and Favorite.class belong to the package com.francelabs.datafari.user which also contains UserConstants.class where we put all the constant Strings used in this package.

We also use the following servlets:

  • AddFavorite: add a document to favorites,

  • AddLikes: add document to likes,

  • DeleteFavorite: delete a document from favorites,

  • Unlike: delete a document from the likes,

  • GetLikesAndFavorites: gets all the likes and the favorites of a user,,, .

AddLike, AddFavorite, DeleteFavorite and Unlike are very similar. Therefore we will detail only one of them, the others can be derived from it. This is the sequence diagram involving the AddLike servlet:

Illustration 2: Sequence Diagram of AddLike for a normal Behaviour

Here is the sequence diagram that involves the LikesAndFavoritesWidget to retrieve the likes and favorites of a user :

Illustration 3: Sequence Diagram of getting the likes and favorites from server