Browse Source

add autocomplete route, further query clean up

pull/242/head
Peter Johnson 9 years ago
parent
commit
fa209c9b50
  1. 2
      package.json
  2. 48
      query/autocomplete.js
  3. 11
      routes/v1.js
  4. 63
      test/unit/fixture/autocomplete_linguistic_focus.js
  5. 34
      test/unit/fixture/autocomplete_linguistic_only.js
  6. 5
      test/unit/fixture/search_full_address.js
  7. 5
      test/unit/fixture/search_linguistic_focus.js
  8. 5
      test/unit/fixture/search_linguistic_only.js
  9. 5
      test/unit/fixture/search_partial_address.js
  10. 5
      test/unit/fixture/search_regions_address.js
  11. 56
      test/unit/query/autocomplete.js
  12. 1
      test/unit/run.js

2
package.json

@ -49,7 +49,7 @@
"pelias-config": "^1.0.1",
"pelias-esclient": "0.0.25",
"pelias-logger": "^0.0.8",
"pelias-query": "^1.1.0",
"pelias-query": "^1.3.0",
"pelias-schema": "1.0.0",
"pelias-suggester-pipeline": "2.0.2",
"stats-lite": "^1.0.3",

48
query/autocomplete.js

@ -0,0 +1,48 @@
var peliasQuery = require('pelias-query');
//------------------------------
// autocomplete query
//------------------------------
var query = new peliasQuery.layout.FilteredBooleanQuery();
// mandatory matches
query.score( peliasQuery.view.ngrams, 'must' );
// scoring boost
query.score( peliasQuery.view.phrase );
query.score( peliasQuery.view.focus );
// --------------------------------
/**
map request variables to query variables for all inputs
provided by this HTTP request.
**/
function generateQuery( clean ){
var vs = new peliasQuery.Vars( peliasQuery.defaults );
// input text
vs.var( 'input:name', clean.text );
// always 10 (not user definable due to caching)
vs.var( 'size', 10 );
// focus point
if( clean.lat && clean.lon ){
vs.set({
'focus:point:lat': clean.lat,
'focus:point:lon': clean.lon
});
}
var result = query.render( vs );
console.log( JSON.stringify( result, null, 2 ) );
// @todo: remove this hack
return JSON.parse( JSON.stringify( result ) );
}
module.exports = generateQuery;

11
routes/v1.js

@ -57,6 +57,15 @@ function addRoutes(app, peliasConfig) {
postProc.geocodeJSON(peliasConfig),
postProc.sendJSON
]),
autocomplete: createRouter([
sanitisers.search.middleware,
middleware.types,
controllers.search(null, require('../query/autocomplete')),
postProc.confidenceScores(peliasConfig),
postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig),
postProc.sendJSON
]),
reverse: createRouter([
sanitisers.reverse.middleware,
controllers.search(undefined, reverseQuery),
@ -81,7 +90,7 @@ function addRoutes(app, peliasConfig) {
app.get ( base, routers.index );
app.get ( base + 'attribution', routers.attribution );
app.get ( base + 'place', routers.place );
app.get ( base + 'autocomplete', routers.search );
app.get ( base + 'autocomplete', routers.autocomplete );
app.get ( base + 'search', routers.search );
app.post( base + 'search', routers.search );
app.get ( base + 'reverse', routers.reverse );

63
test/unit/fixture/autocomplete_linguistic_focus.js

@ -0,0 +1,63 @@
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'
}
}]
}
}
}
},
'sort': [ '_score' ],
'size': 10,
'track_scores': true
};

34
test/unit/fixture/autocomplete_linguistic_only.js

@ -0,0 +1,34 @@
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
}
}
}]
}
}
}
},
'sort': [ '_score' ],
'size': 10,
'track_scores': true
};

5
test/unit/fixture/search_full_address.js

@ -116,11 +116,6 @@ module.exports = {
}
}]
}
},
'filter': {
'bool': {
'must': []
}
}
}
},

5
test/unit/fixture/search_linguistic_focus.js

@ -54,11 +54,6 @@ module.exports = {
}
}]
}
},
'filter': {
'bool': {
'must': []
}
}
}
},

5
test/unit/fixture/search_linguistic_only.js

@ -25,11 +25,6 @@ module.exports = {
}
}]
}
},
'filter': {
'bool': {
'must': []
}
}
}
},

5
test/unit/fixture/search_partial_address.js

@ -84,11 +84,6 @@ module.exports = {
}
}]
}
},
'filter': {
'bool': {
'must': []
}
}
}
},

5
test/unit/fixture/search_regions_address.js

@ -100,11 +100,6 @@ module.exports = {
}
}]
}
},
'filter': {
'bool': {
'must': []
}
}
}
},

56
test/unit/query/autocomplete.js

@ -0,0 +1,56 @@
var generate = require('../../../query/autocomplete');
var parser = require('../../../helper/query_parser');
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof generate, 'function', 'valid function');
t.end();
});
};
function foo( a, b ){
console.log( '----------------' );
console.log( JSON.stringify( a, null, 2 ) );
console.log( '----------------' );
console.log( JSON.stringify( b, null, 2 ) );
console.log( '----------------' );
}
module.exports.tests.query = function(test, common) {
test('valid lingustic-only autocomplete', function(t) {
var query = generate({
text: 'test'
});
var expected = require('../fixture/autocomplete_linguistic_only');
t.deepEqual(query, expected, 'valid autocomplete query');
t.end();
});
test('autocomplete + focus', function(t) {
var query = generate({
text: 'test',
lat: 29.49136,
lon: -82.50622
});
var expected = require('../fixture/autocomplete_linguistic_focus');
t.deepEqual(query, expected, 'valid autocomplete query');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('autocomplete query ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -15,6 +15,7 @@ var tests = [
require('./query/types'),
require('./query/sort'),
require('./query/search'),
require('./query/autocomplete'),
require('./query/reverse'),
require('./helper/query_parser'),
require('./helper/geojsonify'),

Loading…
Cancel
Save