Skip to content

Commit

Permalink
can lookup projects by slug_keyword #250
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Jul 30, 2024
1 parent 45488c4 commit 5c7e62e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/controllers/v1/projects_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ProjectsController = class ProjectsController {
bool: {
should: [
{ term: { slug: req.query.q } },
{ term: { slug_keyword: req.query.q } },
{ match: { title_autocomplete: { query: req.query.q, operator: "and" } } },
{ match: { title: { query: req.query.q, operator: "and" } } }
]
Expand Down Expand Up @@ -233,7 +234,8 @@ const ProjectsController = class ProjectsController {
bool: {
should: [
{ terms: { id: numericIDs } },
{ terms: { slug: ids } }
{ terms: { slug: ids } },
{ terms: { slug_keyword: ids } }
]
}
},
Expand Down
1 change: 1 addition & 0 deletions lib/controllers/v1/taxa_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,7 @@ TaxaController.suggest = async req => {
if ( response.queryTaxon ) {
response.queryTaxon = new Taxon( response.queryTaxon );
response.queryTaxon.prepareForResponse( localeOpts );
await TaxaController.assignPlaces( [response.queryTaxon] );
}
}
if ( req.suggestParams.place_id ) {
Expand Down
8 changes: 6 additions & 2 deletions lib/models/es_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ const ESModel = class ESModel {
if ( options.searchSlug ) {
const numericIDs = _.filter( ids, id => Number( id ) );
if ( _.isEmpty( numericIDs ) ) {
optionsFilters.push( { terms: { slug: ids } } );
should = [
{ terms: { slug: ids } },
{ terms: { slug_keyword: ids } }
];
} else {
should = [
{ terms: { id: numericIDs } },
{ terms: { slug: ids } }
{ terms: { slug: ids } },
{ terms: { slug_keyword: ids } }
];
}
} else {
Expand Down
1 change: 1 addition & 0 deletions openapi/schema/response/conservation_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = Joi.object( ).keys( {
status: Joi.string( ),
status_name: Joi.string( ),
taxon_id: Joi.number( ).integer( ),
user_id: Joi.number( ).integer( ).valid( null ),
url: Joi.string( ).valid( null )
} ).unknown( false ).meta( { className: "ConservationStatus" } )
.valid( null );
10 changes: 9 additions & 1 deletion schema/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@
"min_species_ancestry": "1,2,3,4,5",
"rank_level": 10
},
"project_ids": [ 543 ],
"project_ids": [ 543, 2024072601 ],
"private_geojson": { "type": "Point", "coordinates": [ 2, 1 ] }
},
{
Expand Down Expand Up @@ -1359,6 +1359,14 @@
}
],
"prefers_user_trust": false
},
{
"id": 2024072601,
"title": "Proyecto águilas",
"title_autocomplete": "Proyecto águilas",
"title_exact": "Proyecto águilas",
"slug": "proyecto-águilas",
"slug_keyword": "proyecto-águilas"
}
]
},
Expand Down
3 changes: 3 additions & 0 deletions schema/indices/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@
"analyzer": "keyword_analyzer",
"type": "text"
},
"slug_keyword": {
"type": "keyword"
},
"spam": {
"type": "boolean"
},
Expand Down
15 changes: 15 additions & 0 deletions test/integration/v1/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,21 @@ describe( "Observations", ( ) => {
.expect( 422, done );
} );

it( "raises an error for missing projects", function ( done ) {
request( this.app ).get( "/v1/observations?project_id=nonsense" )
.expect( res => {
expect( res.error.text ).to.eq( "{\"error\":\"Unknown project_id: [nonsense]\",\"status\":422}" );
} ).expect( "Content-Type", /json/ )
.expect( 422, done );
} );

it( "looks up projects with non-ASCII characters", function ( done ) {
request( this.app ).get( "/v1/observations?project_id=proyecto-águilas" )
.expect( res => {
expect( res.body.total_results ).to.eq( 1 );
} ).expect( 200, done );
} );

it( "return iconic taxon names", function ( done ) {
request( this.app ).get( "/v1/observations?id=1" )
.expect( res => {
Expand Down
8 changes: 8 additions & 0 deletions test/integration/v1/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ describe( "Projects", ( ) => {
.expect( 200, done );
} );

it( "returns projects by slug with non-ASCII characters", function ( done ) {
request( this.app ).get( "/v1/projects/proyecto-águilas" )
.expect( res => {
expect( res.body.results[0].slug ).to.eq( "proyecto-águilas" );
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );

it( "returns an error if too many IDs are requested", function ( done ) {
const ids = [];
const count = 101;
Expand Down

0 comments on commit 5c7e62e

Please sign in to comment.