Skip to content

Commit 4f6e2dd

Browse files
authored
feat: allow ID searches for taxa/autocomplete (#501)
Part of WEB-574
1 parent 21b63d8 commit 4f6e2dd

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

lib/controllers/v1/taxa_controller.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ TaxaController.autocomplete = async req => {
182182
req.query.page = 1;
183183
// Make sure we don't show things that don't match all query tokens
184184
const inverseFilters = [];
185+
const options = {};
185186
const filters = [
186187
{
187188
nested: {
@@ -197,6 +198,14 @@ TaxaController.autocomplete = async req => {
197198
}
198199
}
199200
];
201+
202+
const isID = Number.isInteger( Number( q ) );
203+
204+
if ( isID ) {
205+
options.filters = [];
206+
options.filters.push( { terms: { id: [q] } } );
207+
}
208+
200209
if ( isActive !== null ) {
201210
filters.push( esClient.termFilter( "is_active", isActive ) );
202211
}
@@ -435,7 +444,7 @@ TaxaController.autocomplete = async req => {
435444
size: req.query.per_page
436445
};
437446

438-
const response = await TaxaController.searchQuery( req );
447+
const response = await TaxaController.searchQuery( req, options );
439448
if ( response && response.results && exactResults ) {
440449
const exactResultIDs = _.map( exactResults, "id" );
441450
response.results = _.reject( response.results, r => _.includes( exactResultIDs, r.id ) );

lib/views/swagger_v1.yml.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,12 +2909,12 @@ parameters:
29092909
type: string
29102910
in: query
29112911
required: true
2912-
description: Name must begin with this value
2912+
description: Search by name (must start with this value) or by ID (exact match).
29132913
autocomplete_q_not_required:
29142914
name: q
29152915
type: string
29162916
in: query
2917-
description: Name must begin with this value
2917+
description: Search by name (must start with this value) or by ID (exact match).
29182918
autocomplete_project_id:
29192919
name: project_id
29202920
type: integer

test/integration/v1/taxa.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ describe( "Taxa", ( ) => {
2929
.expect( 200, done );
3030
} );
3131

32+
it( "returns the correct taxon when searching by id", function ( done ) {
33+
request( this.app ).get( "/v1/taxa/autocomplete?q=110" )
34+
.expect( res => {
35+
expect( res.body.page ).to.eq( 1 );
36+
expect( res.body.per_page ).to.eq( 10 );
37+
expect( res.body.total_results ).to.eq( 1 );
38+
expect( res.body.results.length ).to.eq( 1 );
39+
expect( res.body.results[0].id ).to.eq( 110 );
40+
expect( res.body.results[0].name ).to.eq( "Mollusca" );
41+
} ).expect( "Content-Type", /json/ )
42+
.expect( 200, done );
43+
} );
44+
3245
it( "searches japanese characters", function ( done ) {
3346
request( this.app ).get( `/v1/taxa/autocomplete?q=${querystring.escape( "眼紋疏廣蠟蟬" )}` )
3447
.expect( res => {

0 commit comments

Comments
 (0)