Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP

  • Loading branch information...
commit 97ce15eed8ccbb0c3eb067926d7e359104dc8a8f 1 parent 887e2b3
@benjreinhart benjreinhart authored
View
8 components/search/collection.js
@@ -1,16 +1,18 @@
module.exports = Collection;
-function Collection(query) {
+function Collection(query, options) {
+ options = options || {};
if (!(this instanceof Collection))
- return new Collection(query);
+ return new Collection(query, options);
this.hits = [];
this.total = null;
this.query = query;
+ this.page = options.page;
this.fetch = this.fetch.bind(this);
}
Collection.prototype.toJSON = function() {
- return {hits: this.hits, total: this.total}
+ return {hits: this.hits, page: this.page, total: this.total}
}
Collection.prototype.fetch = function(cb) {
View
7 components/search/controller.js
@@ -7,9 +7,10 @@ exports.post = post;
*/
function *post() {
- var query = this.request.body.query
- , reverse = this.request.body.reverse
- , collection = Collection(query);
+ var body = this.request.body
+ , query = body.query
+ , reverse = body.reverse
+ , collection = Collection(query, {page: body.page});
try {
yield collection.fetch;
View
7 components/search/middleware/build_query.js
@@ -11,14 +11,15 @@ var toQuery = requireRoot('components/search/query');
*/
exports.buildQuery = function *(next) {
- var query = this.request.body.query
+ var body = this.request.body
+ , query = body.query
, sort = query.sort
, qualifier = query.qualifier.qualifier;
- this.request.body.reverse = 'last' === qualifier
+ body.reverse = 'last' === qualifier
? 'asc' === sort
: 'desc' === sort;
- this.request.body.query = toQuery(query);
+ body.query = toQuery(query, body.page);
yield next;
}
View
1  components/search/middleware/index.js
@@ -3,5 +3,6 @@ var compose = require('koa-compose');
module.exports = compose([
requireRoot('components/search/middleware/enforce_query').enforceQuery,
requireRoot('components/search/middleware/parse_query').parseQuery,
+ requireRoot('components/search/middleware/parse_page').parsePage,
requireRoot('components/search/middleware/build_query').buildQuery
]);
View
15 components/search/middleware/parse_page.js
@@ -0,0 +1,15 @@
+var number = /^\d+$/;
+
+/**
+ * Validates the request `page` param, defaults
+ * to 0 in the event that the param is anything but
+ * a valid number. Also parses the number from a
+ * string to a number.
+ */
+
+exports.parsePage = function *(next) {
+ var page = 0, reqPage = this.request.body.page;
+ if (number.test(reqPage)) page = +reqPage;
+ this.request.body.page = page;
+ yield next;
+}
View
11 components/search/query.js
@@ -1,10 +1,13 @@
module.exports = toQuery;
+const DEFAULT_PAGE_SIZE = requireRoot('core/config').search.DEFAULT_SIZE;
+
var extend = require('underscore').extend
, QueryBuilder = requireRoot('core/lib/elasticsearch/query_builders');
-function toQuery(search) {
+function toQuery(search, page) {
var at = search.at
+ , from = getFrom(page)
, range = search.dateRange
, qualifier = search.qualifier
, sort = 'last' === qualifier.qualifier ? 'desc' : 'asc'
@@ -12,7 +15,7 @@ function toQuery(search) {
query = extend(
QueryBuilder.buildSort('date', sort),
- QueryBuilder.buildSize(qualifier.arguments[0]),
+ QueryBuilder.buildSize({size: qualifier.arguments[0], from: from}),
QueryBuilder.buildFilter(
QueryBuilder.buildDateRange(range.from, range.to)
)
@@ -27,3 +30,7 @@ function toQuery(search) {
function buildVenueQuery(at) {
return QueryBuilder.buildFuzzyLikeThis(["venue.name"], at);
}
+
+function getFrom(page) {
+ return page * DEFAULT_PAGE_SIZE;
+}
View
16 components/search/results.client.js
@@ -7,20 +7,30 @@ module.exports = Collection.extend({
initialize: function() {
var self = this;
self.on('reset', function(){
- self.trigger('success', {total: self.total, hits: self.models});
+ self.trigger('success', self.toJSON());
});
},
parse: function(response) {
+ this.page = response.page;
this.total = response.total;
return response.hits;
},
- fetch: function(search) {
+ fetch: function(search, page) {
+ page = page || 0;
return fetch.call(this, {
- data: {query: search},
+ data: {query: search, page: page},
reset: true,
type: 'POST'
});
+ },
+
+ toJSON: function() {
+ return {
+ hits: this.models,
+ page: this.page,
+ total: this.total
+ }
}
});
View
9 components/search/router.client.js
@@ -13,16 +13,17 @@ module.exports = Backbone.Router.extend({
form.render();
form.on('submit', this.onSearchSubmit, this);
+ this.results = new Results({model: Result});
+ this.results.on('error', this.onSearchError, this);
+ this.results.on('success', this.onSearchSuccess, this);
+
this.resultsView = new ResultsView({
el: $('#js-content')
});
},
onSearchSubmit: function(search) {
- var results = new Results({model: Result});
- results.on('error', this.onSearchError, this);
- results.on('success', this.onSearchSuccess, this);
- results.fetch(search);
+ this.results.fetch(search);
},
onSearchError: function(results, xhr) {
View
6 components/search/views/form.js
@@ -11,11 +11,11 @@ module.exports = Backbone.View.extend({
React.renderComponent(Form({onSubmit: this.onSubmit.bind(this)}), this.$el[0]);
},
- onSubmit: function(value) {
- if (isEmpty(value))
+ onSubmit: function(search) {
+ if (isEmpty(search))
console.log('empty');
else
- this.trigger('submit', value);
+ this.trigger('submit', search);
},
onError: function() {
View
7 components/search/views/results.js
@@ -7,7 +7,12 @@ module.exports = Backbone.View.extend({
},
render: function(results){
- React.renderComponent(Results({total: results.total, results: results.hits}), this.$el[0]);
+ var results = {
+ page: results.page,
+ total: results.total,
+ results: results.hits
+ };
+ React.renderComponent(Results(results), this.$el[0]);
},
onError: function() {
View
7 core/lib/elasticsearch/query_builders.js
@@ -1,15 +1,16 @@
/**
* Builds a size object
*
- * @param {Number} size
+ * @param {Object} options
* @return {Object}
* @api public
*/
-exports.buildSize = function(size) {
- return {size: size};
+exports.buildSize = function(options) {
+ return {size: options.size, from: options.from || 0};
}
+
/**
* Builds a filter object
*
View
11 test/core/lib/elasticsearch/query_builders_test.js
@@ -6,8 +6,15 @@ var expect = require('chai').expect
describe('QueryBuilders', function() {
describe('.buildSize', function() {
it('is an elasticsearch size object', function() {
- var actual = QueryBuilders.buildSize(10)
- , expected = {size: 10};
+ var actual = QueryBuilders.buildSize({size: 10})
+ , expected = {size: 10, from: 0};
+
+ expect(actual).to.eql(expected);
+ });
+
+ it('is an elasticsearch size object with an explicit `from`', function() {
+ var actual = QueryBuilders.buildSize({size: 10, from: 2})
+ , expected = {size: 10, from: 2};
expect(actual).to.eql(expected);
});
Please sign in to comment.
Something went wrong with that request. Please try again.