[Groonga-commit] nroonga/norema [master] Implement pager

Back to archive index

Yoji SHIDARA null+****@clear*****
Tue Aug 21 15:30:08 JST 2012


Yoji SHIDARA	2012-08-21 15:30:08 +0900 (Tue, 21 Aug 2012)

  New Revision: f851613693e7a0299736b84415bd602101d128a4
  https://github.com/nroonga/norema/commit/f851613693e7a0299736b84415bd602101d128a4

  Log:
    Implement pager

  Added files:
    views/pager.jade
  Modified files:
    lib/server.js
    views/search.jade

  Modified: lib/server.js (+43 -11)
===================================================================
--- lib/server.js    2012-08-21 14:57:29 +0900 (1e55ee4)
+++ lib/server.js    2012-08-21 15:30:08 +0900 (fa7f9e4)
@@ -29,13 +29,20 @@ app.use(function(error, request, response, next){
 });
 
 // helpers
-function search(query, callback) {
-  var url = 'http://' + searchNode + '/2011-02-01/search?size=100&facet=path&q=' + encodeURIComponent(query);
+function search(options, callback) {
+  var searchOptions = {
+    size: options.size,
+    facet: 'path',
+    q: options.query,
+    start: options.start
+  }
+
+  var url = 'http://' + searchNode + '/2011-02-01/search?' + querystring.stringify(searchOptions);
   var buffer = '';
   var request = http.get(url, function(response) {
     if (response.statusCode !== 200) {
       var error = new Error('Search server returned ' + response.statusCode);
-      error.query = query;
+      error.query = options.query;
       return callback(error);
     }
 
@@ -74,24 +81,49 @@ app.get('/', function(request, response){
 });
 
 app.get('/search', function(request, response, next) {
-  var query = request.query.query;
+  var options = {
+    query: request.query.query,
+    start: parseInt((request.query.start || '0'), 10),
+    size: 100
+  };
 
-  console.log('QUERY <' + query + '>');
-  search(query, function(error, results) {
+  console.log('SEARCH', options);
+  search(options, function(error, results) {
     if (error) {
       return next(error);
     }
+    var numFound = results.hits.found;
+    var numReturned = results.hits.hit.length;
+    var start = results.hits.start;
+    var nextStart = start + options.size;
+    var previousStart = start - options.size;
+    var nextLink = previousLink = null;
+
+    if (nextStart < numFound) {
+      nextLink = urlForSearch({
+        query: options.query,
+        start: nextStart
+      });
+    }
+    if (previousStart >= 0) {
+      previousLink = urlForSearch({
+        query: options.query,
+        start: previousStart
+      });
+    }
 
     var locals = {
       urlForSearch: urlForSearch,
       titleToId: titleToId,
-      query: query,
+      query: options.query,
       records: results.hits.hit,
-      numFound: results.hits.found,
+      numFound: numFound,
       pathFacets: results.facets.path.constraints,
-      from: results.hits.start + 1,
-      to: results.hits.start + results.hits.hit.length,
-      numShowing: results.hits.hit.length
+      from: start + 1,
+      to: start + numReturned,
+      numShowing: numReturned,
+      nextLink: nextLink,
+      previousLink: previousLink
     };
 
     return response.render('search.jade', locals);

  Added: views/pager.jade (+14 -0) 100644
===================================================================
--- /dev/null
+++ views/pager.jade    2012-08-21 15:30:08 +0900 (11c7c11)
@@ -0,0 +1,14 @@
+ul.pager
+  if previousLink
+    li.previous
+      a(href=previousLink, rel='prev') &laquo; Prev
+  else
+    li.previous.disabled
+      a &laquo; Prev
+
+  if nextLink
+    li.next
+      a(href=nextLink, rel='next') Next &raquo;
+  else
+    li.next.disabled
+      a Next &raquo;

  Modified: views/search.jade (+3 -0)
===================================================================
--- views/search.jade    2012-08-21 14:57:29 +0900 (752865b)
+++ views/search.jade    2012-08-21 15:30:08 +0900 (5ecf65d)
@@ -14,8 +14,10 @@ block content
       else
         .alert.alert-info
           | No entry found.
+
   .row
     .span9
+      include pager
       if records.length > 0
         each record, index in records
           .record
@@ -30,6 +32,7 @@ block content
                   span  &raquo; 
 
             !{record.data.desc}
+      include pager
 
     .span3
       if pathFacets.length > 0
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index