Asponte Labs > Blog Detail

Web Content Viewer- Asponte Custom Skin

Actions

Unraveling the mysteries of IBM Portal Search REST API - Part 3 - Constraints and Sorting

  • Created By: Maria Rauba
  • Updated: December 12, 2016
  • Tags: search, portal, rest, facets, constraints, api, sort, sortkey, fieldtypes, ibm, sortorder, wcm

In Part 1 of this blog, I introduced basic information about the Search REST API and how to use the Constraint API to setup a basic search.

In Part 2 of this blog, I talked about how to use the Facets REST API to categorize your content.  In this final blog post on the Search REST API topic, I will show how to use the Constraints REST API to filter on those facets to get better search results.  I will also explain some tricks for sorting.

 

The documentation for 8.5 can be found here:  https://www.ibm.com/support/knowledgecenter/SSYJ99_8.5.0/search-rest-api/constraints-spec.html

however as I described in Part 1 of this blog, the documentation in 8.0 is a bit clearer in the json syntax to use.

Typically user interfaces for a search will display the facets with the counts as checkboxes. When you check the checkbox for a facet, it kicks off a search REST API call with constraints.  A sample where a user has clicked on the 'News' authoring template facet would be as follows (line breaks added for clarity):

https://myserver.com/wps/contenthandler/searchfeed/search?queryLang=en&locale=en&resultLang=en

&query=maria&scope=1470768476221&start=0

&sortKey=relevance&sortOrder=desc&results=10&querySuggestionEnabled=true

&facet={"id": "authoringtemplate", "count": "ALL", "sortOrder": "DESC"}

&facet={"id": "tb_applicationtype", "count": "ALL", "sortOrder": "DESC"}

&facet={"id": "year", "count": "ALL", "sortOrder": "DESC"}

&facet={"id": "source", "count": "ALL", "sortOrder": "DESC"}

&constraint={"type":"category","id":"facets","values":["authoringtemplate/AT - News"]}

If the user then additionally clicked on a Year filter for '2016', the request would look like:

 

https://myserver.com/wps/contenthandler/searchfeed/search?queryLang=en&locale=en&resultLang=en

&query=maria&scope=1470768476221&start=0

&sortKey=relevance&sortOrder=desc&results=10&querySuggestionEnabled=true

&facet={"id": "authoringtemplate", "count": "ALL", "sortOrder": "DESC"}

&facet={"id": "tb_applicationtype", "count": "ALL", "sortOrder": "DESC"}

&facet={"id": "year", "count": "ALL", "sortOrder": "DESC"}

&facet={"id": "source", "count": "ALL", "sortOrder": "DESC"}

&constraint={"type":"category","id":"facets","values":["authoringtemplate/AT - News"]}

&constraint={"type":"category","id":"facets","values":["year/2016"]}

You can keep adding more and more constraints.  You can only add constraints for things that have been added as a facet.

WCM categories can also be used as constraints.  Since category names can be duplicated in other parts of the taxonomy, it is best to use the UUID for filtering by category.  In order to do that, a custom seedlist will be needed to add a custom field with the category UUID.  

Sorting

The examples above use 'Relevance' for the sort order, but many times you may need to also provide a sort by date capability to sort by Publish Date (effectivedate).  If you simply change the sortKey to 'effectivedate' it seems to almost work, but you'll notice the order isn't 100% accurate.  To make the sort by date work, you need to tell your Search service to treat the effectivedate field as a date type.  To do this set the 'fieldTypes' search parameter for your search service as follows:

fieldTypes:  {"effectivedate":"date"}

This is not documented anywhere except the sample Faceted search article mentioned in Part 1 which does a sort by the integer Popularity and it mentions setting fieldTypes for non-String fields.  A restart of your search server and your portal server is needed to make this take effect.

 

Summary

Using the Search REST API is complex, but hopefully the samples and explanations in this 3-part blog have unravelled many of the API's mysteries.

 

Complementary Content
${loading}