Browse Source

Set centerpoint of viewport in search query

This simply reuses the focus:point:{lat|lon} variables, but sets them
using the centerpoint of the viewport. Eventually we should calculate a
radius and use that here.
pull/304/head
Julian Simioni 9 years ago
parent
commit
57fb960471
  1. 14
      query/search.js
  2. 105
      test/unit/fixture/search_linguistic_viewport.js
  3. 18
      test/unit/query/search.js

14
query/search.js

@ -69,13 +69,15 @@ function generateQuery( clean ){
}
// focus viewport
// @todo: change these to the correct request variable names
// @todo: calculate the centroid from the viewport box
if( clean.focus && clean.focus.viewport ){
var vp = clean.focus.viewport;
if( check.number(clean['focus.viewport.min_lat']) &&
check.number(clean['focus.viewport.max_lat']) &&
check.number(clean['focus.viewport.min_lon']) &&
check.number(clean['focus.viewport.max_lon']) ) {
// calculate the centroid from the viewport box
// simply set focus:point:lat/lon, until we improve this with a radius
vs.set({
'focus:point:lat': vp.min_lat + ( vp.max_lat - vp.min_lat ) / 2,
'focus:point:lon': vp.min_lon + ( vp.max_lon - vp.min_lon ) / 2
'focus:point:lat': clean['focus.viewport.min_lat'] + ( clean['focus.viewport.max_lat'] - clean['focus.viewport.min_lat'] ) / 2,
'focus:point:lon': clean['focus.viewport.min_lon'] + ( clean['focus.viewport.max_lon'] - clean['focus.viewport.min_lon'] ) / 2
});
}

105
test/unit/fixture/search_linguistic_viewport.js

@ -0,0 +1,105 @@
module.exports = {
'query': {
'filtered': {
'query': {
'bool': {
'must': [{
'match': {
'name.default': {
'query': 'test',
'boost': 1,
'analyzer': 'peliasOneEdgeGram'
}
}
}],
'should': [{
'match': {
'phrase.default': {
'query': 'test',
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 2
}
}
}, {
'function_score': {
'query': {
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 2,
'query': 'test'
}
}
},
'functions': [{
'linear': {
'center_point': {
'origin': {
'lat': 29.49136,
'lon': -82.50622
},
'offset': '1km',
'scale': '50km',
'decay': 0.5
}
}
}],
'score_mode': 'avg',
'boost_mode': 'replace'
}
},
{
'function_score': {
'query': {
'filtered': {
'filter': {
'exists': {
'field': 'popularity'
}
}
}
},
'max_boost': 2,
'score_mode': 'first',
'boost_mode': 'replace',
'filter': {
'or': [
{
'type': {
'value': 'admin0'
}
},
{
'type': {
'value': 'admin1'
}
},
{
'type': {
'value': 'admin2'
}
}
]
},
'functions': [{
'field_value_factor': {
'modifier': 'sqrt',
'field': 'popularity'
},
'weight': 1
}]
}
}]
}
}
}
},
'sort': [ '_sort' ],
'size': 10,
'track_scores': true
};

18
test/unit/query/search.js

@ -79,6 +79,24 @@ module.exports.tests.query = function(test, common) {
t.end();
});
test('search search + viewport', function(t) {
var query = generate({
text: 'test', size: 10,
'focus.viewport.min_lat': 28.49136,
'focus.viewport.max_lat': 30.49136,
'focus.viewport.min_lon': -87.50622,
'focus.viewport.max_lon': -77.50622,
layers: ['test']
});
var compiled = JSON.parse( JSON.stringify( query ) );
var expected = require('../fixture/search_linguistic_viewport');
expected.sort = sort;
t.deepEqual(compiled, expected, 'valid search query');
t.end();
});
test('search search + focus on null island', function(t) {
var query = generate({
text: 'test', size: 10,

Loading…
Cancel
Save