From 1df1a0765c315d12881ec37f9040d1cca98ef911 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Fri, 3 Jun 2016 15:46:21 -0400 Subject: [PATCH] Add categories filter to search query --- query/search.js | 7 ++ .../fixture/search_with_category_filtering.js | 93 +++++++++++++++++++ test/unit/query/search.js | 12 +++ 3 files changed, 112 insertions(+) create mode 100644 test/unit/fixture/search_with_category_filtering.js diff --git a/query/search.js b/query/search.js index 92a9db00..5ab96248 100644 --- a/query/search.js +++ b/query/search.js @@ -44,6 +44,8 @@ query.filter( peliasQuery.view.boundary_circle ); query.filter( peliasQuery.view.boundary_rect ); query.filter( peliasQuery.view.sources ); query.filter( peliasQuery.view.layers ); +query.filter( peliasQuery.view.categories ); + // -------------------------------- /** @@ -63,6 +65,11 @@ function generateQuery( clean ){ // layers vs.var( 'layers', clean.layers); + // categories + if (clean.categories) { + vs.var('input:categories', clean.categories); + } + // size if( clean.querySize ) { vs.var( 'size', clean.querySize ); diff --git a/test/unit/fixture/search_with_category_filtering.js b/test/unit/fixture/search_with_category_filtering.js new file mode 100644 index 00000000..57014821 --- /dev/null +++ b/test/unit/fixture/search_with_category_filtering.js @@ -0,0 +1,93 @@ + +module.exports = { + 'query': { + 'filtered': { + 'query': { + 'bool': { + 'must': [{ + 'match': { + 'name.default': { + 'query': 'test', + 'boost': 1, + 'analyzer': 'peliasIndexOneEdgeGram' + } + } + }], + 'should': [{ + 'match': { + 'phrase.default': { + 'query': 'test', + 'analyzer': 'peliasPhrase', + 'type': 'phrase', + 'boost': 1, + 'slop': 2 + } + } + }, { + 'function_score': { + 'query': { + 'match': { + 'phrase.default': { + 'query': 'test', + 'analyzer': 'peliasPhrase', + 'type': 'phrase', + 'slop': 2, + 'boost': 1 + } + } + }, + 'max_boost': 20, + 'score_mode': 'first', + 'boost_mode': 'replace', + 'functions': [{ + 'field_value_factor': { + 'modifier': 'log1p', + 'field': 'popularity', + 'missing': 1 + }, + 'weight': 1 + }] + } + }, { + 'function_score': { + 'query': { + 'match': { + 'phrase.default': { + 'query': 'test', + 'analyzer': 'peliasPhrase', + 'type': 'phrase', + 'slop': 2, + 'boost': 1 + } + } + }, + 'max_boost': 20, + 'score_mode': 'first', + 'boost_mode': 'replace', + 'functions': [{ + 'field_value_factor': { + 'modifier': 'log1p', + 'field': 'population', + 'missing': 1 + }, + 'weight': 2 + }] + } + }] + } + }, + 'filter': { + 'bool': { + 'must': [{ + 'terms': { + 'category': ['retail','food'] + } + }] + } + } + } + }, + 'sort': [ '_score' ], + 'size': 20, + 'track_scores': true +}; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index 4ff414c0..61d00984 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -159,6 +159,18 @@ module.exports.tests.query = function(test, common) { t.end(); }); + test('categories filter', function(t) { + var query = generate({ + 'text': 'test', + 'categories': ['retail','food'] + }); + + var compiled = JSON.parse( JSON.stringify( query ) ); + var expected = require('../fixture/search_with_category_filtering'); + + t.deepEqual(compiled, expected, 'valid search query with category filtering'); + t.end(); + }); }; module.exports.all = function (tape, common) {