From e9e1adf089e03b37440c5ce52193fca34326793f Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Mon, 15 Feb 2016 18:24:27 +0100 Subject: [PATCH] Implements a search --- webapp/css/satbd.sateliite.bar.css | 12 +++++++ webapp/html/index.html | 1 + webapp/js/components/album/album.html | 3 +- webapp/js/services.js | 37 ++++++++++++++++++--- webapp/js/views/search/search.html | 25 ++++++++++++-- webapp/js/views/search/search.js | 48 +++++++++++++++++++++++++-- 6 files changed, 117 insertions(+), 9 deletions(-) diff --git a/webapp/css/satbd.sateliite.bar.css b/webapp/css/satbd.sateliite.bar.css index 8a47d6a..cb0cd4a 100644 --- a/webapp/css/satbd.sateliite.bar.css +++ b/webapp/css/satbd.sateliite.bar.css @@ -108,3 +108,15 @@ body > .container-fluid { .album .album-ellipsis p:last-child{ margin-bottom: 15px; } +.search-result { + position: relative; +} +.search-score { + position:absolute; + right:15px; + top:0; +} + +.search-result > h4 { + padding-top: 15px; +} \ No newline at end of file diff --git a/webapp/html/index.html b/webapp/html/index.html index 6f9a598..416d7e2 100644 --- a/webapp/html/index.html +++ b/webapp/html/index.html @@ -49,6 +49,7 @@ + diff --git a/webapp/js/components/album/album.html b/webapp/js/components/album/album.html index c673bcf..71a51ca 100644 --- a/webapp/js/components/album/album.html +++ b/webapp/js/components/album/album.html @@ -2,7 +2,8 @@
{{$ctrl.album.titleDisplay}}
- + +

{{$ctrl.album.Note}}/5.0

{{$ctrl.album.ref}}
diff --git a/webapp/js/services.js b/webapp/js/services.js index 97bc92c..ef52dc4 100644 --- a/webapp/js/services.js +++ b/webapp/js/services.js @@ -35,12 +35,41 @@ services.factory('albumService',['$http','$log','$q', function($http,$log,$q) { }); } - function search(terms) { - var defer = $q.defer(); - defer.reject('Search is not implemented'); - return defer.promise; + var keywordRegexp = /(\s+)|$/g; + + function fuzzifyTerms(terms) { + return terms.replace(keywordRegexp,"~2 "); } + function processResult(res, timeOffset) { + res.took += timeOffset; + return res; + } + + function search(terms,from,fuzzify, previousTime) { + if (typeof fuzzify == 'undefined' ) { + fuzzify = true; + } + if (typeof previousTime == 'undefined' ) { + previousTime = 0; + } + + return $http.post('/api/search', { + "size" : 10, + "from": from || 0, + "explain": false, + "highlight":{}, + "fields": ["ID","série","collection","titre","éditeur","ref","description","Note"], + "query": { + "query": terms + } + }).then(function(response) { + if ( fuzzify == true && response.data.total_hits == 0) { + return search(fuzzifyTerms(terms),from,false,response.data.took); + } + return processResult(response.data,previousTime); + }); + } return { get: get, search: search diff --git a/webapp/js/views/search/search.html b/webapp/js/views/search/search.html index 6ed0fc3..94b6b5e 100644 --- a/webapp/js/views/search/search.html +++ b/webapp/js/views/search/search.html @@ -1,2 +1,23 @@ -

Votre recherche de '{{ terms }}' n'a pas abouti

-

C'est toujours pas implémenté, couillon

+

Votre recherche '{{terms}}' à donné {{response.total_hits}} résultats en {{inSecond(response.took)}} s

+ +
+
+ +
+
+
{{h.fields['ref']}} Score: {{round4Dec(h.score)}}
+

{{h.fields['série']}} - {{h.fields['titre']}}

+
+ +
+
+ +
+ Chargement ... +
diff --git a/webapp/js/views/search/search.js b/webapp/js/views/search/search.js index 85da9fc..de1cac0 100644 --- a/webapp/js/views/search/search.js +++ b/webapp/js/views/search/search.js @@ -1,12 +1,56 @@ 'use strict'; angular.module('satbd.satellite.bar.views.search',[ - 'ngRoute' + 'ngRoute', + 'ngSanitize', + 'satbd.satellite.bar.services', + 'satbd.satellite.bar.components.album', + 'angular-inview' ]).config(function($routeProvider) { $routeProvider.when('/search', { templateUrl: 'js/views/search/search.html', controller: 'SearchCtrl' }); -}).controller('SearchCtrl', function($scope,$routeParams) { +}).controller('SearchCtrl', function($scope,$routeParams,$log,$sce,albumService) { $scope.terms=$routeParams.q; + $scope.scrollMore = false; + $scope.allHits = []; + + $scope.pushHits = function (response) { + for ( var i = 0; i < response.hits.length; i++ ) { + $scope.allHits.push(response.hits[i]); + } + // stop scrolling if we have all hits + $scope.scrollMore = $scope.allHits.length < response.total_hits; + }; + + albumService.search($scope.terms).then( function(data) { + $scope.response = data; + $scope.pushHits(data); + }); + + $scope.loadMore = function () { + if ( !$scope.scrollMore || $scope.allHits.length >= $scope.response.total_hits) { + $scope.scrollMore = false; + $log.info('Done scrolling results'); + return; + } + $log.info('Scrolling more results'); + + albumService.search($scope.terms,$scope.allHits.length).then( function(data) { + $scope.pushHits(data); + }); + }; + + $scope.inSecond= function(bleveValue) { + return bleveValue / 1e6; + } + + $scope.trustedFragment = function(f) { + return $sce.trustAsHtml(f); + } + + $scope.round4Dec = function(value) { + return Math.round(value * 10000) / 10000; + } });