Search API Endpoints
General Searchβ
Performs an advanced search across specified model collections using MongoDB aggregation.
Requestβ
- Method: GET
- Endpoint:
/api/search - Query Parameters:
type(required): The model/collection name to search insearch(required): Search term or query stringindex(optional): Specific index to use for searchpaths(optional): Specific fields to search withinparentCompany(optional): Filter results by parent companypage(optional): Page number for paginationlimit(optional): Number of items per page
Responseβ
{
"data": {
"docs": [...],
"totalDocs": number,
"limit": number,
"page": number,
"totalPages": number,
"hasNextPage": boolean,
"nextPage": number,
"hasPrevPage": boolean,
"prevPage": number,
"pagingCounter": number
},
"code": 200
}
Error Responsesβ
- 400 Bad Request
- When
typeis missing:"Search failed: Model type is required" - When
searchis missing:"Search failed: Search term is required" - When model type is invalid:
"Search failed: Invalid model type: {modelName}"
- When
- 500 Internal Server Error
- When search operation fails:
"Search failed: {error message}"
- When search operation fails:
MongoDB Text Searchβ
Performs a text search using MongoDB's built-in text search capabilities.
Requestβ
- Method: GET
- Endpoint:
/api/mongo-search - Query Parameters:
type(required): The model/collection name to search insearch(optional): Text to search forparentCompany(optional): Filter results by parent companypage(optional): Page number for paginationlimit(optional): Number of items per page
Responseβ
{
"docs": [...],
"totalDocs": number,
"limit": number,
"page": number,
"totalPages": number,
"hasNextPage": boolean,
"nextPage": number,
"hasPrevPage": boolean,
"prevPage": number,
"pagingCounter": number
}
Error Responseβ
- 500 Internal Server Error
{
"error": "Search operation failed",
"details": "error message"
}
Notesβ
- The general search endpoint uses MongoDB aggregation pipeline for more complex search operations
- The MongoDB text search endpoint uses built-in text search functionality and includes relevance scoring
- Both endpoints support pagination
- Results are sorted by creation date (descending) by default
- Text search results include a relevance score when search term is provided
Search Query Renderer
The renderSearchQuery utility function is responsible for constructing MongoDB aggregation pipelines for advanced search operations across different models.
Function Signatureβ
renderSearchQuery(modelName: string, searchTerm: string, options: {
parentCompany?: string,
index?: string,
paths?: string[]
}): Object[]
Parametersβ
-
modelName (string, required)
- The name of the model/collection to search in
- Used to determine specific search configurations for different models
-
searchTerm (string, required)
- The search term or query string to search for
- Can be partial words or complete phrases
-
options (object)
-
parentCompany (string, optional)
- Company identifier to filter results by company
- Used for multi-tenant data separation
-
index (string, optional)
- Specific MongoDB index to use for the search
- Can optimize search performance for specific use cases
-
paths (string[], optional)
- Array of field paths to search within
- Limits the search to specific fields in the document
-
Return Valueβ
Returns an array of MongoDB aggregation pipeline stages that can be used with Model.aggregate().
Usage Exampleβ
const searchQuery = renderSearchQuery("Customer", "John Doe", {
parentCompany: "company123",
paths: ["firstName", "lastName", "email"],
});
const aggregate = Customer.aggregate(searchQuery);
const results = await Customer.aggregatePaginate(aggregate, options);
Industry-Specific Examplesβ
Trucking Industryβ
// Search for trucks by make, model, or VIN
const truckSearch = renderSearchQuery("Vehicle", "Peterbilt 579", {
paths: ["make", "model", "vin"],
});
Service Industry (HVAC, Plumbing, Electrical)β
// Search for service tickets by description or customer info
const serviceSearch = renderSearchQuery("ServiceTicket", "AC repair", {
paths: ["description", "serviceType", "customerName"],
});
Search Behaviorβ
-
The function constructs a search pipeline that:
- Filters by parent company if specified
- Performs text matching based on the provided search term
- Applies any model-specific search configurations
- Orders results by relevance
-
Search capabilities include:
- Partial word matching
- Case-insensitive search
- Multi-field search when paths are specified
- Company-specific data isolation
Performance Considerationsβ
- Using the
indexparameter can significantly improve search performance - Limiting
pathsto only necessary fields can optimize query execution - The function supports MongoDB's aggregation pipeline optimizations
- Consider adding specific indexes for frequently searched fields
Security Notesβ
- The function automatically handles company isolation through the
parentCompanyparameter - Input sanitization should be performed before passing search terms
- Access control should be implemented at the API route level
Related Componentsβ
- Used by the main search controller (
/controllers/search/index.js) - Works with MongoDB aggregation framework
- Integrates with the pagination system