Implementing Search in APIs

Implementing Search in APIs

Last updated: 3/8/2025

1 hour
Medium

šŸ” Implementing Search in APIs

🧐 Introduction

Search functionality is essential in modern APIs, enabling users to efficiently locate and retrieve relevant data.
An effective search system: āœ… Enhances user experience – Users can find data quickly.
āœ… Improves data accessibility – Allows seamless navigation of large datasets.
āœ… Optimizes performance – Reduces server load with efficient querying.

In this lesson, we will:

  • Understand different types of search techniques.
  • Learn about fuzzy search and how it handles typos.
  • Explore how Elasticsearch works for advanced searching.
  • Implement a search feature in an API.

šŸ“Œ 1. Types of Search Techniques 🧩

APIs implement search in different ways, depending on data structure, query complexity, and performance requirements.

āœ… 1ļøāƒ£ Simple Query-Based Search

šŸ”¹ Searches performed using query parameters (GET /items?q=laptop).
šŸ”¹ Works well for structured data and small datasets.
šŸ”¹ Can be implemented using a database WHERE clause.

Example (SQL Query):

SELECT * FROM products WHERE name LIKE '%laptop%';

āœ… 2ļøāƒ£ Full-Text Search

šŸ”¹ Performs comprehensive text searches across large text fields.
šŸ”¹ Supports partial matches, phrase searches, and logical operations.
šŸ”¹ Typically implemented using Elasticsearch, Solr, or PostgreSQL full-text search.

Example (PostgreSQL Full-Text Search):

SELECT * FROM products WHERE to_tsvector(name) @@ to_tsquery('gaming & laptop');

āœ… 3ļøāƒ£ Fuzzy Search (Handling Typos)

šŸ”¹ Allows approximate matching, useful for handling: āœ” Typos
āœ” Misspellings
āœ” Variations in user input

šŸ”¹ Uses Levenshtein distance (edit distance) to calculate similarity between words.

Example: Searching for "exmple" should return "example".

Fuzzy Search in SQL (PostgreSQL - pg_trgm extension):

SELECT * FROM products WHERE similarity(name, 'laptop') > 0.5 ORDER BY similarity(name, 'laptop') DESC;

šŸ“Œ 2. Deep Dive into Fuzzy Search šŸ“Œ

How Does Fuzzy Search Work?

Fuzzy search is based on Levenshtein distance, which calculates how many character changes are needed to match two words.

Word 1Word 2Edit Distance
exampleexmple1 (missing "a")
laptoplapotp1 (swapped "t" and "o")
gaminggamong1 (typo in "n")

Example using Elasticsearch Fuzzy Search:

{ "query": { "fuzzy": { "name": { "value": "exmple", "fuzziness": "AUTO" } } } }

āœ” Fuzziness Levels:

  • "AUTO": Adjusts allowed typos based on query length.
  • 1: Allows 1-character typo.
  • 2: Allows 2-character typos.

šŸ“Œ 3. Understanding Elasticsearch šŸ“š

What is Elasticsearch?

Elasticsearch is a distributed search engine designed for fast and scalable full-text search.

šŸ”¹ How Elasticsearch Works:

1ļøāƒ£ Indexing → Converts documents into an efficient searchable format.
2ļøāƒ£ Analyzers → Breaks text into tokens for better matching.
3ļøāƒ£ Queries → Allows complex searches (match, fuzzy, wildcard, etc.).
4ļøāƒ£ Scoring → Ranks results based on relevance to the search query.


šŸ“Œ 4. Implementing Search with Elasticsearch in an API

āœ… Step 1: Install Elasticsearch Client

Run:

npm install @elastic/elasticsearch express

āœ… Step 2: Set Up Elasticsearch Client in Node.js

const { Client } = require('@elastic/elasticsearch'); const client = new Client({ node: 'http://localhost:9200' }); async function createIndex() { await client.indices.create({ index: 'products', body: { mappings: { properties: { name: { type: 'text' }, description: { type: 'text' } } } } }); } createIndex().catch(console.error);

āœ” Creates an Elasticsearch index for storing product data.


āœ… Step 3: Index Sample Data in Elasticsearch

async function addProducts() { await client.index({ index: 'products', body: { name: 'Gaming Laptop', description: 'A powerful laptop for gaming and work' } }); } addProducts().catch(console.error);

āœ” Adds a product document to Elasticsearch.


āœ… Step 4: Implement Search Route in Express.js

const express = require('express'); const app = express(); app.get('/search', async (req, res) => { const { q } = req.query; try { const { body } = await client.search({ index: 'products', body: { query: { multi_match: { query: q, fields: ['name', 'description'], fuzziness: "AUTO" } } } }); res.json(body.hits.hits); } catch (error) { res.status(500).json({ error: error.message }); } }); app.listen(3033, () => console.log('Server running on port 3033'));

āœ” Searches for products using fuzzy matching and multi-field search.


šŸ“Œ 5. Other Search Mechanisms šŸ”§

āœ… Prefix Search

  • Finds words starting with a specific prefix.
{ "query": { "prefix": { "name": "lap" } } }

āœ… Wildcard Search

  • Matches words with pattern-based queries.
{ "query": { "wildcard": { "name": "lap*op" } } }

āœ… Geospatial Search

  • Used for location-based searches (e.g., find nearby stores).
{ "query": { "geo_distance": { "distance": "10km", "location": { "lat": 40.7128, "lon": -74.0060 } } } }

šŸ“Œ 6. Best Practices for Search Performance 🌟

āœ… 1ļøāƒ£ Proper Indexing

  • Use correct field types (text, keyword, geo_point).
  • Preprocess data to remove stop words and normalize text.

āœ… 2ļøāƒ£ Optimized Queries

  • Use filters instead of queries for faster lookups.
  • Combine full-text search with keyword filters.

āœ… 3ļøāƒ£ Security Measures

  • Sanitize user input to prevent query injection.
  • Implement rate limiting for API search endpoints.

āœ… 4ļøāƒ£ Pagination & Sorting

  • Use from & size in Elasticsearch to paginate results.
{ "from": 0, "size": 10, "query": { "match": { "name": "laptop" } } }

šŸŽÆ Summary

āœ… Implemented different search techniques (simple, full-text, fuzzy).
āœ… Learned how Elasticsearch indexes and retrieves data efficiently.
āœ… Built an API with search capabilities using Express.js & Elasticsearch.
āœ… Explored advanced search mechanisms (wildcard, prefix, geospatial).