From 224e25863c266b880540a78a98be720be0202169 Mon Sep 17 00:00:00 2001 From: Amelia Tee Qiao Ying Date: Mon, 29 Aug 2022 23:49:23 +0000 Subject: [PATCH 1/7] add filters --- _static/kendrasearchtools.js | 37 ++++++++++++++++++++++++++++-------- _templates/searchbox.html | 14 ++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 _templates/searchbox.html diff --git a/_static/kendrasearchtools.js b/_static/kendrasearchtools.js index f2d47ef889..6ab9b076a9 100644 --- a/_static/kendrasearchtools.js +++ b/_static/kendrasearchtools.js @@ -533,11 +533,20 @@ var KendraSearch = { _pulse_status : -1, init : function() { + var filters = {}; var params = $.getQueryParameters(); + console.log(params); if (params.q) { var query = params.q[0]; $('input[name="q"]')[0].value = query; - this.performSearch(query); + + Object.keys(params).forEach(function(key) { + if(key.startsWith("filter")){ + filters[key] = true; + $('input[name="' + key + '"]')[0].checked = true; + } + }); + this.performSearch(query, filters=filters); } }, @@ -577,16 +586,18 @@ var KendraSearch = { /** * execute search (requires search index to be loaded) */ - query : function(query, pageNumber, pageSize=10) { - var url = " https://9cs56celvj.execute-api.us-west-2.amazonaws.com/prod" + query : function(query, pageNumber, pageSize=10, filters={}) { + var url = "https://8e38o66cpd.execute-api.us-west-2.amazonaws.com/prod" $('#search-progress').empty(); query = KendraSearch.sanitize(query); + console.log(query, pageNumber, pageSize, filters, window.location.host); + fetch(url, { method: 'post', - body: JSON.stringify({ "queryText": query , "pageNumber": pageNumber, "pageSize": pageSize, "host": window.location.host}), + body: JSON.stringify({ "queryText": query , "pageNumber": pageNumber, "pageSize": pageSize, "filters": filters, "host": window.location.host}), }).then(response => response.json()) .then(function(data) { var docs = data["ResultItems"]; @@ -638,6 +649,7 @@ var KendraSearch = { }else{ KendraSearch.stopPulse(); KendraSearch.title.text(_('Search Results')); + var no_pages = Math.min(Math.floor(parseFloat(data["TotalNumberOfResults"])/parseFloat(pageSize))+1, 100.00/parseFloat(pageSize)); var maxPaginationButtons = Math.min(6, no_pages); var startPaginationButtons = Math.max(1, pageNumber-Math.ceil(maxPaginationButtons/2)); @@ -651,12 +663,11 @@ var KendraSearch = { } } KendraSearch.out.append(paginationItem); - $('.paginationnolink').each(function ( index, element ) { $(element).on('click', function() { KendraSearch.output.empty(); paginationItem.remove(); - KendraSearch.query(query, parseInt($(element).attr('id').split("-")[1])); + KendraSearch.query(query, parseInt($(element).attr('id').split("-")[1]), pageSize, filters); }); }); } @@ -670,20 +681,30 @@ var KendraSearch = { /** * perform a search for something (or wait until index is loaded) */ - performSearch : function(query) { + performSearch : function(query, filters) { // create the required interface elements this.out = $('#search-results'); this.title = $('

' + _('Searching...') + '

').appendTo(this.out); this.dots = $('').appendTo(this.title); this.status = $('

 

').appendTo(this.out); + // this.searchFilter = $('
').appendTo(this.out) this.output = $('