@@ -7,28 +7,12 @@ const getDatabase = require('../../database');
77// Get the project formatter
88const projectFormatter = require ( '../../utils/project-formatter' ) ;
99// Get auxiliar functions
10- const { parseJSON, getConfig } = require ( '../../utils/auxiliar-functions' ) ;
10+ const { parseJSON, getConfig, parseType , getSearchQuery } = require ( '../../utils/auxiliar-functions' ) ;
1111// Standard HTTP response status codes
1212const { BAD_REQUEST , INTERNAL_SERVER_ERROR } = require ( '../../utils/status-codes' ) ;
1313
1414const projectRouter = Router ( ) ;
1515
16- // Convert a string input into int, float or boolean type if possible
17- const parseType = input => {
18- // Booleans
19- if ( input === 'false' ) return false ;
20- if ( input === 'true' ) return true ;
21- // Numbers
22- if ( + input ) return + input ;
23- // Other strings
24- return input ;
25- } ;
26-
27- // Escape all regex sensible characters
28- const escapeRegExp = input => {
29- return input . replace ( / [ - [ / \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' ) ;
30- } ;
31-
3216// Root
3317projectRouter . route ( '/' ) . get (
3418 handler ( {
@@ -42,24 +26,10 @@ projectRouter.route('/').get(
4226 // Look for the search text in the accession and some metadata/pdbInfo fields
4327 const search = request . query . search ;
4428 if ( search ) {
45- // trim() removes surrounding white spaces
46- const tsearch = escapeRegExp ( search . trim ( ) ) ;
47- // $regex is a mongo command to search for regular expressions inside fields
48- // $options: 'i' stands for the search to be case insensitive
49- if ( ! finder . $and ) finder . $and = [ ] ;
50- finder . $and . push ( {
51- $or : [
52- { accession : { $regex : tsearch , $options : 'i' } } ,
53- { 'metadata.NAME' : { $regex : tsearch , $options : 'i' } } ,
54- { 'metadata.DESCRIPTION' : { $regex : tsearch , $options : 'i' } } ,
55- { 'metadata.AUTHORS' : { $regex : tsearch , $options : 'i' } } ,
56- { 'metadata.GROUPS' : { $regex : tsearch , $options : 'i' } } ,
57- { 'metadata.REFERENCES' : { $regex : tsearch , $options : 'i' } } ,
58- { 'metadata.INCHIKEYS' : { $regex : tsearch , $options : 'i' } } ,
59- { 'metadata.PDBIDS' : { $regex : tsearch , $options : 'i' } } ,
60- { 'metadata.SYSKEYS' : { $regex : tsearch , $options : 'i' } } ,
61- ] ,
62- } ) ;
29+ // Parse the search text to a predefined smart query
30+ const searchQuert = getSearchQuery ( search ) ;
31+ if ( ! finder . $and ) finder . $and = [ searchQuert ] ;
32+ else finder . $and . push ( searchQuert ) ;
6333 }
6434 // Then, filter by 'filter' parameters
6535 // Look for a specified value in any database field
0 commit comments