Skip to content

Commit

Permalink
increase shard_size for some observers requests; fix bug with some ob…
Browse files Browse the repository at this point in the history
…servation endpoints when id is the only param #407; add rank_level to taxa fetch #367
  • Loading branch information
pleary committed Nov 3, 2023
1 parent 444364f commit 0da3917
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/controllers/v1/observations_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ ObservationsController.elasticResults = async function observationElasticResults
&& ( _.isEmpty( query.inverse_filters ) || excludingOnlyUserIDs )
&& _.isEmpty( query.grouped_inverse_filters )
&& _.isEqual( query.sort, { created_at: "desc" } )
&& _.isEmpty( req.query.aggs )
) {
const sourceParams = util.sourceParams( returnOnlyID ? ["id"] : {
includes: opts.includes,
Expand Down Expand Up @@ -1238,8 +1239,8 @@ ObservationsController.observationsObserverCounts = async req => {
};
// attempting to account for inaccurate counts for queries with a small size
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-shard-size
if ( ( ( aggSize * 1.5 ) + 10 ) < 200 ) {
countQuery.aggs.top_observers.terms.shard_size = 200;
if ( ( ( aggSize * 1.5 ) + 10 ) < 500 ) {
countQuery.aggs.top_observers.terms.shard_size = 500;
}
}
countQuery.per_page = 0;
Expand Down Expand Up @@ -1289,8 +1290,8 @@ ObservationsController.observationsSpeciesObserverCounts = async req => {
};
// attempting to account for inaccurate counts for queries with a small size
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-shard-size
if ( ( ( aggSize * 1.5 ) + 10 ) < 200 ) {
countQuery.aggs.user_taxa.terms.shard_size = 200;
if ( ( ( aggSize * 1.5 ) + 10 ) < 500 ) {
countQuery.aggs.user_taxa.terms.shard_size = 500;
}
if ( aggSize > 0 ) {
countQuery.aggs.user_taxa.aggs = {
Expand Down
3 changes: 3 additions & 0 deletions lib/controllers/v1/taxa_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ TaxaController.show = async req => {
throw util.httpError( 422, "Too many IDs" );
}
const filters = [{ terms: { id: ids } }];
if ( req.query.rank_level ) {
filters.push( esClient.termFilter( "rank_level", req.query.rank_level ) );
}
return TaxaController.searchQuery( req, {
filters,
details: true,
Expand Down
1 change: 1 addition & 0 deletions lib/views/swagger_v1.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,7 @@ paths:
corresponding taxa. A maximum of 30 results will be returned
parameters:
- $ref: "#/parameters/path_multi_id"
- $ref: "#/parameters/rank_level"
tags:
- Taxa
responses:
Expand Down
1 change: 1 addition & 0 deletions openapi/paths/v2/taxa/{id}.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = sendWrapper => {
.meta( { in: "path" } )
.required( )
),
transform( Joi.array( ).items( Joi.number( ) ).label( "rank_level" ).meta( { in: "query" } ) ),
transform( Joi.string( ).label( "fields" ).meta( { in: "query" } ) ),
transform( Joi.string( ).label( "X-HTTP-Method-Override" ).meta( { in: "header" } ) )
],
Expand Down
2 changes: 1 addition & 1 deletion openapi/schema/request/taxa_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = Joi.object( ).keys( {
"variety",
"form"
) ),
rank_level: Joi.number( ),
rank_level: Joi.array( ).items( Joi.number( ) ),
id_above: Joi.number( ).integer( ),
id_below: Joi.number( ).integer( ),
per_page: Joi.number( ).integer( ),
Expand Down
7 changes: 7 additions & 0 deletions test/integration/v1/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,13 @@ describe( "Observations", ( ) => {
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );

it( "can filter on id alone", function ( done ) {
request( this.app ).get( "/v1/observations/observers?id=1" ).expect( res => {
expect( res.body.total_results ).to.eq( 1 );
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );
} );

describe( "species_counts", ( ) => {
Expand Down

0 comments on commit 0da3917

Please sign in to comment.