Integrating Search
Sorting
By default, search results are ranked by relevance as determined by the pipeline. Sorting allows you to override the default ranking logic and order results based on a field and it's corresponding value in either ascending or descending order.
Pre-requisites and limitations:
- The field must be an Integer, Float, Timestamp, or a String.
- There must be at least one record in the collection that has that field and its corresponding value.
- The field must not be a 'List'.
Instructions
Use the "sort" parameter in your query and specify the field you want to sort by. For example, the following query will sort results based on modified_time
in ascending order:
{
"q": "blog",
"filter": "_id != ''",
"fields": "title,description,url",
"sort": "modified_time"
}
To sort in descending order, simply add a (-) before the field name "sort": "-modified_time"
.
Adding sorting to the default search interface
If you have set up your website search using the generated code from Sajari's default search interface, then follow these instructions to add a sort selector to your interface:
- Replace the results page search box div element (
<div id="results-search-box"></div>
) in the generated code with the following code:
<div>
<div id="results-search-box"></div>
<select id="pipeline-select">
<option value='{"sort":""}'>Relevance</option>
<option value='{"sort":"-modified_time"}'>Date</option>
</select>
</div>
- Add this script to the bottom of the
<body>
section:
<script>
var select = document.getElementById('pipeline-select');
select.onchange = function (e) {
var value = e.target.value;
searchInterface('pub', 'pipeline.values-set', JSON.parse(value));
searchInterface('pub', 'pipeline.search-send');
};
</script>
If you are using React SDK, refer to the React SDK documentation.
Note: The sort is a strict logic and the results returned by the index will be sorted according to the value of the field used for sort. You can choose to use values of a field as a factor in the ranking logic rather than a strict sort.