In this blog post, we will learn how to add filters to the API responsible for fetching the list of skills i.e. the endpoint – https://api.susi.ai/cms/getSkillList.json. The purpose of adding filters is to return a list of skills based on some parameters associated with the skill, that would be required to allow the user to get the desired response that s/he may be using to display it on the UI.
Overview of the API
- API to fetch the list of skills –
- URL – https://api.susi.ai/cms/getSkillList.json
- It takes 5 optional parameters –
- model – It is the name of the model that user is requesting
- group – It is the name of the group that user is requesting
- language – It is the name of the language that user is requesting
- skill – It is the name of the skill that user is requesting
- applyFilter – It has true/false values, depending whether filtering is required
- If the request URL contains the parameter applyFilter as true, in that case the other 2 compulsory parameters are –
- filter_name – ascending/descending, depending upon the type of sorting the user wants
- filter_type – lexicographical, rating, etc based on what basis the filtering is going to happen
So, we will now look into adding a new filter_type to the API.
Detailed explanation of the implementation
- We can add filters based on the key values of the Metadata object of individual skills. The Metadata object for each skill is similar to the following object –
- We will now add provision for URL parameter, filter_type=feedback in the API, which will filter the results based on the feedback_count key, which tells the number of feedback/comments a skill has received.
- In the serviceImpl method of the ListSkillService class, we can see a code snippet that handles the filtering part, It checks the filter_type parameter received in the URL on if-else block. The code snippet looks like this –
- Similarly, we will need to add an else if condition with feedback_type=feedback and write the code block inside it. Here is the code for it, which is explained in detail.
Working of the above code snippet
- The first if condition checks for the filter_type the user has requested and enters the condition if it is equal to feedback
- The next if-else handles the case of ascending and descending and sorts the list of skills accordingly.
- The variable jsonValues passed in the Collections.sort function contains a List of JSONObject. Here, each object stands for the metadata object for a skill.
- Since, the sorting is not a simple linear sort, the sort function is overloaded with a comparator function that specifies the key, based on which the sorting would take place.
- The feedback_count value is stored in valA and valB for the two variables that is considered at an instance while sorting. For any other key based filtering, we need to replace the feedback_count with the desired key name.
- The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero.
- The value returned to the sort function determines the order of the sorted array.
- For, ascending and descending, the parameters of the compare function is swapped, so that we can achieve the exact opposite results from one another.
This was the implementation for the filtering of Skill List based on a key value present in the Skill Metadata object. I hope, you found the blog helpful in making the understanding of the implementation better.
- API implementation of ListSkillService.java : https://github.com/fossasia/susi_server/blob/development/src/ai/susi/server/api/cms/ListSkillService.java
- Article on Sort overloading in JAVA : http://www.codejava.net/java-core/the-java-language/what-is-overloading-in-java-and-examples