src/jquery.azuresearch.js
//Execute the AJAX call to Azure Search
Execute the AJAX call to Azure Search
Execute the AJAX call to Azure Search
function search() {
        local.isGeoSearch = false;
        if (local.waitingLatLong)
            return;
        if (ls.geoSearch.lat && ls.geoSearch.lng) {
            debug('Geo searching...');
            debug(ls.geoSearch.lat);
            debug(ls.geoSearch.lng);
            local.isGeoSearch = true;
            if (!ls.searchParms.orderby || ls.searchParms.orderby.indexOf(ls.geoSearch.fieldName) == 0) {
                var orderby = "geo.distance(" + ls.geoSearch.azureFieldName;
                orderby += ", geography'POINT(" + ls.geoSearch.lng + " " + ls.geoSearch.lat + ")')";
                if (ls.searchParms.orderby && ls.searchParms.orderby.indexOf(' desc') != -1) orderby += ' desc';
                ls.searchParms.orderby = orderby;
            }
        }
        var f = null;
        var previousFilter = ls.searchParms.filter;
        if (ls.facetsSelected.length > 0) {
            var facetFilter = [];
            ls.facetsSelected.forEach(function (item, index) {
                var p = item.split('|');
                facetFilter.push(p[0] + '/any(m: m eq \'' + p[1].replace(/[']/gi,'\'\'') + '\')');
            });
            f = facetFilter.join(' ' + ls.facets.searchMode + ' ');
            if (previousFilter)
                f = ls.searchParms.filter + ' ' + ls.facets.searchMode + ' ' + f;
        }
        if (local.isGeoSearch && ls.geoSearch.maxDistance) {
            debug('Filter Geo searching by distance : ' + ls.geoSearch.maxDistance);
            var geoFilter = "geo.distance(" + ls.geoSearch.azureFieldName + ", geography'POINT(" + ls.geoSearch.lng + " " + ls.geoSearch.lat + ")') le " + ls.geoSearch.maxDistance;
            if(f) {
                f += ' ' + ls.facets.searchMode + ' ' + geoFilter
            } else {
                f = geoFilter;
                if (previousFilter)
                    f = ls.searchParms.filter + ' ' + ls.facets.searchMode + ' ' + f;
            }
        }
        if (f)
            ls.searchParms.filter = f;
        var settings = {
            "crossDomain": true,
            "url": ls.azureSearch.url,
            "method": "POST",
            "headers": {
                "Content-Type": "application/json",
                "Accept": "application/json",
                "api-key": ls.azureSearch.key,
                "Cache-Control": "no-cache",
            },
            "data": JSON.stringify(ls.searchParms)
        }
        $.ajax(settings).done(function (response) {
            local.totalResults = ls.searchParms.count && response['@odata.count']
                ? response['@odata.count'] : -1;
            ls.onResults.call(response, local);
        });
        ls.searchParms.filter = previousFilter;
    }
function search ( ) { local . isGeoSearch = false ; if ( local . waitingLatLong ) return ; if ( ls . geoSearch . lat && ls . geoSearch . lng ) { debug ( 'Geo searching...' ) ; debug ( ls . geoSearch . lat ) ; debug ( ls . geoSearch . lng ) ; local . isGeoSearch = true ; if ( ! ls . searchParms . orderby || ls . searchParms . orderby . indexOf ( ls . geoSearch . fieldName ) == 0 ) { var orderby = "geo.distance(" + ls . geoSearch . azureFieldName ; orderby += ", geography'POINT(" + ls . geoSearch . lng + " " + ls . geoSearch . lat + ")')" ; if ( ls . searchParms . orderby && ls . searchParms . orderby . indexOf ( ' desc' ) != - 1 ) orderby += ' desc' ; ls . searchParms . orderby = orderby ; } } var f = null ; var previousFilter = ls . searchParms . filter ; if ( ls . facetsSelected . length > 0 ) { var facetFilter = [ ] ; ls . facetsSelected . forEach ( function ( item , index ) { var p = item . split ( '|' ) ; facetFilter . push ( p [ 0 ] + '/any(m: m eq \'' + p [ 1 ] . replace ( / ['] / gi , '\'\'' ) + '\')' ) ; } ) ; f = facetFilter . join ( ' ' + ls . facets . searchMode + ' ' ) ; if ( previousFilter ) f = ls . searchParms . filter + ' ' + ls . facets . searchMode + ' ' + f ; } if ( local . isGeoSearch && ls . geoSearch . maxDistance ) { debug ( 'Filter Geo searching by distance : ' + ls . geoSearch . maxDistance ) ; var geoFilter = "geo.distance(" + ls . geoSearch . azureFieldName + ", geography'POINT(" + ls . geoSearch . lng + " " + ls . geoSearch . lat + ")') le " + ls . geoSearch . maxDistance ; if ( f ) { f += ' ' + ls . facets . searchMode + ' ' + geoFilter } else { f = geoFilter ; if ( previousFilter ) f = ls . searchParms . filter + ' ' + ls . facets . searchMode + ' ' + f ; } } if ( f ) ls . searchParms . filter = f ; var settings = { "crossDomain" : true , "url" : ls . azureSearch . url , "method" : "POST" , "headers" : { "Content-Type" : "application/json" , "Accept" : "application/json" , "api-key" : ls . azureSearch . key , "Cache-Control" : "no-cache" , } , "data" : JSON . stringify ( ls . searchParms ) } $ . ajax ( settings ) . done ( function ( response ) { local . totalResults = ls . searchParms . count && response [ '@odata.count' ] ? response [ '@odata.count' ] : - 1 ; ls . onResults . call ( response , local ) ; } ) ; ls . searchParms . filter = previousFilter ; }
Execute the AJAX call to Azure Search
Execute the AJAX call to Azure Search
//Check if it's geo search //Save the current filter //Apply Facet Filters // apply filter and escape single quotes in value (') //Apply geo distance filter if configured //Return the filter to the original state