From 555ebcbb867b88be066e3bdb1debb614d1e774d8 Mon Sep 17 00:00:00 2001 From: Severyn Kozak Date: Mon, 11 May 2015 15:49:40 -0400 Subject: [PATCH] Add category filtration to /search. query/category_filter.js -Define a function that handles injecting category-filtration logic into elasticsearch queries. -Abstract it out into a standalone module since it's going to be used by both `query/search.js` and `query/reverse.js`. sanitiser/search.js, test/unit/sanitiser/search.js -Add category filtration to `/search`. -Fixes (partly) #128 --- query/category_filter.js | 16 ++++++++++++++++ query/search.js | 3 ++- sanitiser/search.js | 1 + test/unit/sanitiser/search.js | 3 ++- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 query/category_filter.js diff --git a/query/category_filter.js b/query/category_filter.js new file mode 100644 index 00000000..af25a8e5 --- /dev/null +++ b/query/category_filter.js @@ -0,0 +1,16 @@ +/** + * Inject the logic for filtering results by category into an elasticsearch + * query. + */ + +'use strict'; + +module.exports = function ( query, params ){ + if( params.categories && params.categories.length > 0 ){ + query.query.filtered.filter.bool.must.push({ + terms: { + category: params.categories + } + }); + } +}; diff --git a/query/search.js b/query/search.js index 4308e57c..d930415d 100644 --- a/query/search.js +++ b/query/search.js @@ -1,5 +1,6 @@ var queries = require('geopipes-elasticsearch-backend').queries, + categoryFilter = require('./category_filter'), sort = require('../query/sort'); function generate( params ){ @@ -44,7 +45,7 @@ function generate( params ){ } query.sort = query.sort.concat( sort( params ) ); - + categoryFilter( query, params ); return query; } diff --git a/sanitiser/search.js b/sanitiser/search.js index d226dbd9..4fd14163 100644 --- a/sanitiser/search.js +++ b/sanitiser/search.js @@ -5,6 +5,7 @@ var _sanitize = require('../sanitiser/_sanitize'), size: require('../sanitiser/_size'), layers: require('../sanitiser/_layers'), details: require('../sanitiser/_details'), + categories: require('./_categories'), latlonzoom: require('../sanitiser/_geo') }; diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 37763165..44aeaad7 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -8,6 +8,7 @@ var search = require('../../../sanitiser/search'), layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], size: 10, + categories: [], details: true }, sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; @@ -425,4 +426,4 @@ module.exports.all = function (tape, common) { for( var testCase in module.exports.tests ){ module.exports.tests[testCase](test, common); } -}; \ No newline at end of file +};