How to Use Full Text Search in API: Simple Guide
To use
full text search in an API, you typically add a query parameter that accepts the search text and then use a database or search engine that supports full text indexing to find matching results. The API endpoint processes the search query and returns relevant data based on text relevance.Syntax
Full text search in an API usually involves a GET request with a query parameter for the search term. The server uses this term to perform a full text search on the data source.
GET /items?search=keyword: The endpoint/itemsaccepts asearchparameter.- The server uses the
searchvalue to query the database or search engine. - The response returns items matching the search text, often sorted by relevance.
http
GET /items?search=coffee
Example
This example shows a simple Node.js Express API that uses a full text search on an in-memory list of items. It filters items containing the search term (case-insensitive) and returns matching results.
javascript
import express from 'express'; const app = express(); const port = 3000; const items = [ { id: 1, name: 'Coffee Maker' }, { id: 2, name: 'Tea Kettle' }, { id: 3, name: 'Espresso Machine' }, { id: 4, name: 'Coffee Beans' } ]; app.get('/items', (req, res) => { const search = req.query.search?.toString().toLowerCase() || ''; if (!search) { return res.json(items); } const results = items.filter(item => item.name.toLowerCase().includes(search)); res.json(results); }); app.listen(port, () => { console.log(`API running at http://localhost:${port}`); });
Output
API running at http://localhost:3000
Common Pitfalls
Common mistakes when implementing full text search in APIs include:
- Not sanitizing or validating the search input, which can cause security issues.
- Using simple substring matching instead of proper full text search features, leading to poor relevance.
- Ignoring case sensitivity, which can miss matches.
- Not indexing the database for full text search, causing slow queries.
Use database full text indexes or search engines like Elasticsearch for better performance and relevance.
javascript
/* Wrong: Simple substring match without sanitization */ app.get('/items', (req, res) => { const search = req.query.search; const results = items.filter(item => item.name.includes(search)); res.json(results); }); /* Right: Lowercase and check for empty input */ app.get('/items', (req, res) => { const search = req.query.search?.toString().toLowerCase() || ''; if (!search) return res.json(items); const results = items.filter(item => item.name.toLowerCase().includes(search)); res.json(results); });
Quick Reference
- Use
GETrequests with asearchquery parameter. - Sanitize and normalize search input (e.g., lowercase).
- Use database full text indexes or search engines for large data.
- Return results sorted by relevance if possible.
- Handle empty or missing search terms gracefully.
Key Takeaways
Use a query parameter like
search to accept full text search input in your API.Normalize and sanitize search input to avoid errors and security issues.
For large datasets, rely on database full text indexes or dedicated search engines.
Return search results sorted by relevance for better user experience.
Handle empty or missing search queries by returning all or default results.