Navigation Menu

Skip to content

Commit

Permalink
Implement pager
Browse files Browse the repository at this point in the history
  • Loading branch information
darashi committed Aug 21, 2012
1 parent 0af35f4 commit f851613
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
54 changes: 43 additions & 11 deletions lib/server.js
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions views/pager.jade
@@ -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;
3 changes: 3 additions & 0 deletions views/search.jade
Expand Up @@ -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
Expand All @@ -30,6 +32,7 @@ block content
span &raquo;

!{record.data.desc}
include pager

.span3
if pathFacets.length > 0
Expand Down

0 comments on commit f851613

Please sign in to comment.