-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from noi-techpark/main
main
- Loading branch information
Showing
16 changed files
with
236 additions
and
3,196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,55 @@ | ||
|
||
process.env.PELIAS_CONFIG = './pelias.json';//PATCH for pelias-api | ||
const AddressParser = require('pelias-parser/parser/AddressParser'); | ||
const apiApp = require('pelias-api/app'); | ||
|
||
const {app, version, config, polling, fetchData, listenLog, _, express, yaml} = require('../base'); | ||
const {resolve} = require('path'); | ||
const {app, version, config, polling, fetchData, listenLog, _, express, yaml} = require(resolve(__dirname,'../base')); | ||
|
||
const {formatters, services, combineResults, textDistance, orderResult} = require('./utils')(config, _); | ||
|
||
app.use(express.json()); | ||
|
||
app.locals.parser = { address: new AddressParser() } | ||
//HACK from pelias-parser/server/... | ||
process.env.PELIAS_CONFIG = `${__dirname}/pelias${config.envId}.json`; | ||
console.log('PELIAS_CONFIG file:', process.env.PELIAS_CONFIG); | ||
|
||
//TODO manage parameter lang for any requests | ||
//PATCH for pelias-api that require this file | ||
const AddressParser = require('pelias-parser/parser/AddressParser'); | ||
const peliasParser = require('pelias-parser/server/routes/parse'); | ||
const peliasApiApp = require('pelias-api/app'); | ||
|
||
app.get('/libpostal/parse', require('pelias-parser/server/routes/parse')) | ||
app.use(express.json()); | ||
|
||
app.get(/^\/placeholder(.*)$/, (req, res) => { | ||
app.locals.parser = { address: new AddressParser() }; | ||
//HACK for endpoint /v1/search?text=... | ||
function reqLog(req, res) { | ||
console.log('[geocoder] ',req.query) | ||
res.json({}); | ||
}); | ||
|
||
app.get('/here', async(req, res) => { | ||
|
||
const response = await services.here(req.query.text); | ||
|
||
res.json(formatters.here(response)); | ||
}) | ||
|
||
//ElasticSearch proxy | ||
app.post(/^\/pelias(.*)$/, (req, res)=> { | ||
|
||
//TODO HERE FILTER REVERSE GEOCODING | ||
|
||
let musts = _.get(req.body, "query.bool.must"); | ||
|
||
if (!musts || musts.length===0) { | ||
res.json( formatters.elasticsearch([]) ); | ||
return false; | ||
} | ||
//UN USEFUL let q_search = _.get(req.body, "query.bool.must[0].match['name.default'].query"); | ||
let texts = [], | ||
lang; | ||
} | ||
|
||
_.forEach(musts, (m, k) => { | ||
|
||
//texts search | ||
let q1 = _.get(m, "constant_score.filter.multi_match.query"); | ||
let q2 = _.get(m, "multi_match.query"); | ||
texts.push(q1 || q2); | ||
app.get('/libpostal/parse', peliasParser); | ||
app.get(/^\/placeholder(.*)$/, reqLog); | ||
app.get(/^\/pip(.*)$/, reqLog); | ||
app.get(/^\/interpolation(.*)$/, reqLog); | ||
|
||
if(!lang) { | ||
//word | ||
let ll = _.get(m, "constant_score.filter.multi_match.fields") | ||
, l1; | ||
if(_.isArray(ll)) { | ||
l1 = ll.pop(); | ||
l1 = l1.split('.').pop(); | ||
} | ||
//phrase | ||
let ll2 = _.get(m, "multi_match.fields") | ||
, l2; | ||
if(_.isArray(ll2)) { | ||
l2 = ll2.pop(); | ||
l2 = l2.split('.').pop(); | ||
} | ||
lang = l1 || l2 || config.default_lang; | ||
} | ||
}); | ||
app.post(/^\/_search(.*)$/, reqLog); | ||
//TODO implement pelias /v1/search | ||
|
||
//let q = _.get(req.body, "query.bool.must[0].constant_score.filter.multi_match.query"); | ||
// let q2 = _.get(req.body, "query.bool.must[1].constant_score.filter.multi_match.query"); | ||
//ElasticSearch internal proxy | ||
app.post(/^\/pelias(.*)$/, (req, res) => { | ||
|
||
let text = texts.join(' '); | ||
|
||
console.log('[geocoder] Pelias Search: "', text,'" lang:',lang);//JSON.stringify(req.body,null,2)) | ||
|
||
//console.log('ELASTIC SEARCH: "'+text+'"') | ||
const {text, lang} = formatters.elasticsearchRequest(req, res); | ||
|
||
if(!_.isString(text) || text.length < Number(config.min_text_length)) { | ||
|
||
res.json( formatters.elasticsearch([]) ) | ||
} | ||
else { | ||
combineResults(text, lang, jsonres => { | ||
|
||
jsonres = orderResult(text, jsonres) | ||
|
||
res.json(jsonres); | ||
|
||
res.json(orderResult(text, jsonres)); | ||
}); | ||
} | ||
}); | ||
|
||
//DEBUG http://localhost:8087/testSearch?text=hotel | ||
app.get('/testSearch', (req,res) => { | ||
|
||
console.log('[geocoder] /testSearch',req.query); | ||
|
||
let lang = req.query.lang || config.default_lang; | ||
|
||
if(!_.isEmpty(req.query.text)) { | ||
|
||
combineResults(req.query.text, lang, jsonres => { | ||
app.use('/tests', express.static('tests')); | ||
|
||
jsonres = orderResult(req.query.text, jsonres) | ||
app.use('/', peliasApiApp); | ||
|
||
res.json({ | ||
rawResult: jsonres.hits.hits.map(hit => { | ||
return { | ||
name: hit._source.name.default, | ||
//layer: hit._source.layer, | ||
rank: textDistance(req.query.text, hit._source.name.default) | ||
} | ||
}), | ||
peliasResult: jsonres | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
//TODO | ||
/*apiApp.get(['/',/geocoder'], async (req, res) => { | ||
res.send({ | ||
status: 'OK', | ||
version | ||
}); | ||
});*/ | ||
|
||
const serverParser = app.listen(config.pelias_listen_port, () => { | ||
console.log('[geocoder-pelias-services] listening on %s', config.pelias_listen_port) | ||
const serverParser = app.listen(config.listen_port, function() { | ||
console.log('internal services paths', app._router.stack.filter(r => r.route).map(r => `${Object.keys(r.route.methods)[0]} ${r.route.path}`) ); | ||
console.log(`listening at http://localhost:${this.address().port}`); | ||
}); | ||
|
||
const serverApi = apiApp.listen( config.listen_port, listenLog); |
Oops, something went wrong.