Browse Source

Add categories filter to search query

pull/546/head
Diana Shkolnikov 9 years ago
parent
commit
1df1a0765c
  1. 7
      query/search.js
  2. 93
      test/unit/fixture/search_with_category_filtering.js
  3. 12
      test/unit/query/search.js

7
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 );

93
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
};

12
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) {

Loading…
Cancel
Save