Ever wondered how to supercharge your Node.js application with lightning-fast search capabilities? While traditional database queries struggle with complex search requirements, Typesense offers a powerful solution. This open-source search engine rivals Elasticsearch and Algolia, perfectly suited for Node.js applications.
With basic Node.js knowledge, you'll discover how to implement sophisticated search features including faceted search, geo-search, and real-time indexing. Perfect for building e-commerce platforms, content repositories, or any application needing robust search capabilities. Ready to transform your user experience? Let's explore how to integrate Typesense with Node.js step by step.
Typesense is an open-source search engine built for speed and ease of use. It fixes typos automatically and works in real-time with fast response times. As an alternative to Elasticsearch and Algolia, it offers features like faceted search, geo-search, and instant results. Its simple setup and clear API make it a good choice for adding search to modern applications.
Whether you're building an e-commerce site, content platform, or any app needing quick and accurate search results, Typesense helps you deliver a better search experience to your users.
Typesense powers search across various industries and applications, helping businesses deliver fast and accurate results to their users. Here are some key areas where Typesense excels:
How Typesense Processes Data: A Complete Flow
npm install typesense
Just use this command in terminal to install typesense in your machine
const Typesense = require('typesense');
const client = new Typesense.Client({
nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }],
apiKey: 'YOUR_API_KEY',
connectionTimeoutSeconds: 2,
});
For initialization of typesense, we need to create typesense instances. For the same, we need apiKey, protocol and host.
It is always better to save these credentials using .env and we can reference it here.
client.collections().retrieve()
.then(response => console.log("Connected to Typesense", response))
.catch(error => console.error("Connection failed", error));
This command is used to verify the operational status of your Typesense instance. If the connection is established and active, it will return a list of available collections.
a. Creating a Collection
const schema = {
name: "products",
fields: [
{ name: "name", type: "string", facet: true },
{ name: "price", type: "number", facet: true }
] };
client.collections().create(schema);
This code will create a new collection named "products" with two fields: "name" (string type) and "price" (number type). Both fields are marked as facets, enabling filtering and faceting operations on them.
Experience seamless collaboration and exceptional results.
b. Fetching Collections
client.collections().retrieve().then(console.log);
This code will retrieve metadata about all the collections present in the database and display the information in the console. This metadata may include collection names, schemas, and other relevant details.
c. Updating a Collection
const collection = client.collections('products');
const schema = collection.schema;
schema.fields.push({ name: "description", type: "string" });
collection.update(schema);
d. Deleting a Collection
client.collections('products').delete().then(console.log)
This code will permanently delete the "products" collection and all its data from the database. Exercise caution when using this operation, as data deletion is irreversible.
a. Inserting Bulk Data
const products = [
{ id: "1", title: "Laptop", price: 1000 },
{ id: "2", title: "Phone", price: 500 }
];
client.collections('products').documents().import(products);
This code demonstrates how to insert multiple documents (bulk data) into a Firestore collection named 'products'. In this example, an array called "products" is defined containing two product objects, each with an id, title, and price. The 'import' function is then used to add these product objects as documents into the specified collection.
b. Fetching a Document
client.collections('products').documents('1').retrieve().then(console.log);
This code retrieves a document with the ID '1' from the 'products' collection and then passes the retrieved document data to the console.log function, which will display the data in the console.
client.collections('products').documents('1').update({ price: 1200 });
This code targets the document with the ID '1' within the 'products' collection and updates its 'price' field to the value 1200.
client.collections('products').documents('1').delete().then(console.log);
This code deletes the document with the ID '1' from the 'products' collection. The .then(console.log) portion indicates that once the deletion is successful, a success message will be logged to the console.
a. Simple Search
client.collections('products').documents().search({
q: "Laptop",
query_by: "title"
}).then(console.log);Let's break down the code snippet and explain the roles of `q` and `query_by` within a search operation.
Explanation
`q`: This parameter represents the search query term itself. In the given example, the value of `q` is "Laptop", indicating that you're searching for documents containing the term "Laptop".
`query_by`: This parameter specifies the field or fields within your documents that you want to search against. In this case, `query_by` is set to "title", meaning the search will be performed specifically on the "title" field of your documents.
Why They Are Used
Using `q` allows you to define the keyword or phrase you're looking for.
Using `query_by` focuses the search on specific fields, making it more efficient and targeted. If you don't use `query_by`, the search might be performed across all fields, which could be slower and return less relevant results.
How to Perform the Search
Experience seamless collaboration and exceptional results.
The provided code snippet demonstrates how to execute this search using a client library (presumably for a database or search service). Here's a breakdown of the steps:
Specify the Collection: `client.collections('products')` indicates that you're searching within a collection named "products".
Search the Documents: `.documents().search()` initiates the search operation on the documents within the collection.
Provide Search Parameters: The object `{ q: "Laptop", query_by: "title" }` is passed as an argument to the `search` function. This object contains the search query ("Laptop") and the field to search against ("title").
Handle Results: `.then(console.log)` is used to handle the search results. In this case, the results are simply logged to the console.
Key Point: This search will find and return documents within the "products" collection where the "title" field contains the term "Laptop".
client.collections('products').documents().search({
q: "Laptop Phone",
query_by: "title"
}).then(console.log);
Here in this example, we are searching for multiple keywords, now the q is having Laptop and phone. So here the result should contain both the keywords present
client.collections('products').documents().search({
q: "*",
query_by: "title",
facet_by: "price"
}).then(console.log);
client.collections('products').documents().search({
q: "*",
query_by: "title",
filter_by: "price:>500"
}).then(console.log);
Here, the filter_by is used to filter the documents by conditions such as >, <, >=,<=,:= with numerical values. This will be applicable for single numeric values.
e. ID-Based Filtering (Multiple Values)
client.collections('products').documents().search({
q: "*",
query_by: "title",
filter_by: "id:=[1,2]"
}).then(console.log);
Here, the filter_by is used to filter the documents by conditions such as >, <, >=,<=,:= with numerical values. This will be applicable for single numeric values.
client.collections('products').documents().search({
q: "*",
query_by: "title",
filter_by: "id!=1"
}).then(console.log);
Similarly, If we would like to exclude some values, then we should use this != operator.
I hope you now have a clear understanding of Typesense's capabilities and how it can enhance your Node.js applications. From basic setup to advanced features like faceting and filtering, we've covered the essential tools for building powerful search experiences.
Typesense stands out with its speed, typo tolerance, and simple integration, making it ideal for applications requiring robust search. Whether you're working on an e-commerce platform or content system, you now have the knowledge to implement fast, accurate search that your users will love.