Skip to content

Commit

Permalink
Merge pull request #161 from noi-techpark/main
Browse files Browse the repository at this point in the history
main
  • Loading branch information
RudiThoeni authored Nov 7, 2022
2 parents 195627a + dfdeb67 commit aaaad5d
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 3,196 deletions.
6 changes: 0 additions & 6 deletions geocoder/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ dev:
listen_port: 9088
min_text_length: 1

pelias_listen_port: 8087
#internal pelias microservices(//same port of file ./pelias.json)

#TODO cache_ttl: 0
default_lang: 'en'
min_text_length: 3
Expand All @@ -32,9 +29,6 @@ endpoints:
layer: venue

here:
#hostname: api.here.com
#path: /6.2/geocode.json
##provided by npm heremap
appId: ${HERE_APPID}
appCode: ${HERE_APPCODE}
apiKey: ${HERE_APIKEY}
Expand Down
139 changes: 30 additions & 109 deletions geocoder/index.js
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);
Loading

0 comments on commit aaaad5d

Please sign in to comment.