Advanced Filtering
Recall the previous topic on filtering content via metadata, but consider the case
where two
different users accessing a portal may need to see different content. This is possible
with
advanced filtering features. To use advanced filtering, an administrator must write
JavaScript
code that uses properties on the user
and portal
objects to
generate a search query which will be used when filtering content. The advanced filtering
interface is located on the "Search Facets" tab of the portal admin UI.
Filter Queries
Titania Delivery will use a text string to filter content via the Solr search engine. The format of these queries is the same as the Standard Query Parser. Before writing a filter function, it is important to familiarize oneself with the format of these queries, as a query will be the return value of the function described below.
Writing the Filter Function
The filter function should be viewed as the body of a JavaScript function. Any function
declarations, parameters, or ending curly braces are not required or recommended for
the main
filter function. You may however, use functions within your filter function. When
writing the
filter function, the goal is to return a string that will be used directly as a Solr
search
query that filters content. Keep in mind that in order to achieve this goal, the author
has
access to the user
and portal
objects. The final line of the
function should be a return statement, and the type should be a string. An example
filter
function is given
below.
if (user) { return null; } return "portalVisible_md: true"
The effect of the above function is that the portal will display all content when
a user is
logged in, yet filter content based on the value of the portalVisible
metadata
attribute for anonymous users.
"_md"
to represent that it is a metadata attribute.
Failure to append this suffix may cause the function to failTesting the Filter Function
Before saving your filter function, it is strongly recommended to test your code using
the built in testing functionality. If the test passes, it will ensure your code will
properly
compile, and also that the queries it returns are valid for both logged-in and anonymous
users.
Saving code that returns an invalid query to the search engine may cause problems
within the
portal. To test, simply write your code and click the "Test Script" button. If a results
box
appears with the text No Problems Detected.
, it is safe to save your filter
function.
If the test fails, a helpful message will be displayed with the errors producted.
Other Information
After testing your script, the save button above the code editing area will save your new filters to the portal. Please allow several seconds for changes in the script to be reflected by the portal. The query used to filter content will be evaluated every time a user logs in and out of the portal, meaning various users and anonymous users can see different content. This allows for a potentially very different portal experience based on information about the user.