Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from pelias:master #18

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The API recognizes the following properties under the top-level `api` key in you
|`services`|*no*||Service definitions for [point-in-polygon](https://github.com/pelias/pip-service), [libpostal](https://github.com/whosonfirst/go-whosonfirst-libpostal), [placeholder](https://github.com/pelias/placeholder), and [interpolation](https://github.com/pelias/interpolation) services. For a description of when different Pelias services are recommended or required, see our [services documentation](https://github.com/pelias/documentation/blob/master/services.md).|
|`defaultParameters.focus.point.lon` <br> `defaultParameters.focus.point.lat`|no | |default coordinates for focus point
|`targets.auto_discover`|no|true|Should `sources` and `layers` be automatically discovered by querying Elasticsearch at process startup. (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below).
|`targets.auto_discover_required`|no|false|If set to `true`, type mapping discovery failures will result in a fatal error. This means a valid connection to a working Elasticsearch cluster with a Pelias index is _required_ on startup. Setting this to true can help prevent issues such as incorrect configuration in production environments
|`targets.layers_by_source` <br> `targets.source_aliases` <br> `targets.layer_aliases`|no | |custom values for which `sources` and `layers` the API accepts (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below). We recommend using the `targets.auto_discover:true` configuration instead of setting these manually.
|`customBoosts` | no | `{}` | Allows configuring boosts for specific sources and layers, in order to influence result order. See [Configurable Boosts](#custom-boosts) below for details |
|`autocomplete.exclude_address_length` | no | 0 | As a performance optimization, this optional filter excludes results from the 'address' layer for any queries where the character length of the 'subject' portion of the parsed_text is equal to or less than this many characters in length. Addresses are usually the bulk of the records in Elasticsearch, and searching across all of them for very short text inputs can be slow, with little benefit. Consider setting this to 1 or 2 if you have several million addresses in Pelias. |
Expand Down
19 changes: 15 additions & 4 deletions helper/TypeMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,14 @@ TypeMapping.prototype.loadTargets = function( targetsBlock ){

// load values from either pelias config file or from elasticsearch
TypeMapping.prototype.load = function( done ){
// load targets from config file
this.loadFromConfig();

// load pelias config
const peliasConfigTargets = _.get(
require('pelias-config').generate(require('../schema')),
'api.targets', {}
);

// load targets from config file
this.loadTargets( peliasConfigTargets );

// do not load values from elasticsearch
if( true !== peliasConfigTargets.auto_discover ){
if( 'function' === typeof done ){ done(); }
Expand All @@ -146,6 +144,19 @@ TypeMapping.prototype.load = function( done ){
loadFromElasticsearch(this, done);
};

// load values from either pelias config file or from elasticsearch
TypeMapping.prototype.loadFromConfig = function( done ){

// load pelias config
const peliasConfigTargets = _.get(
require('pelias-config').generate(require('../schema')),
'api.targets', {}
);

// load targets from config file
this.loadTargets( peliasConfigTargets );
};

// replace the contents of an object or array
// while maintaining the original pointer reference
function safeReplace(reference, replacement){
Expand Down
7 changes: 5 additions & 2 deletions helper/type_mapping.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const TypeMapping = require('./TypeMapping');

// instantiate a new type mapping
// instantiate a singleton type mapping
// loading normal defaults from pelias-config happens now
// updating that config from Elasticsearch happens later
// before the webserver is started
var tm = new TypeMapping();
tm.load();
tm.loadFromConfig();

// export singleton
module.exports = tm;
18 changes: 12 additions & 6 deletions helper/type_mapping_discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const logger = require('pelias-logger').get('api:type_mapping_discovery');
/**
* This module allows discovery of the sources and layers used
* in an existing elasticsearch index.
*
*
* note: this will override any previously configured type mappings.
*/

Expand Down Expand Up @@ -43,15 +43,21 @@ module.exports = (tm, done) => {
if( _.has(res, 'hits.total') ) {
totalHits = _.isPlainObject(res.hits.total) ? res.hits.total.value : res.hits.total;
}

// query error
if( err ){ logger.error( err ); }

if( err ){
logger.error( err );

if (peliasConfig.get('api.targets.auto_discover_required') === true) {
process.exit(1);
}
}

// invalid response
else if ( totalHits < 1 ){
logger.error( 'no hits for aggregation' );
}

// valid response
else {

Expand All @@ -78,4 +84,4 @@ module.exports = (tm, done) => {

if ('function' === typeof done) { done(); }
});
};
};
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const logger = require('pelias-logger').get('api');
const type_mapping = require('./helper/type_mapping');

const app = require('./app'),
port = ( process.env.PORT || 3100 ),
host = ( process.env.HOST || undefined );

const server = app.listen( port, host, () => {
// ask server for the actual address and port its listening on
const listenAddress = server.address();
logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` );
let server;

// load Elasticsearch type mappings before starting web server
type_mapping.load(() => {
server = app.listen( port, host, () => {
// ask server for the actual address and port its listening on
const listenAddress = server.address();
logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` );
});
});

function exitHandler() {
Expand Down