Introduction to Search-Index
`search-index` is a powerful, serverless, and dependency-free search engine library built in JavaScript. It allows for fast, efficient indexing and searching of data in your JavaScript applications. Thanks to its lightweight and ease of use, it has become a popular choice among developers.
API Overview
Below are some of the most useful APIs provided by `search-index`, along with examples and usage instructions.
Creating a Search Index
Create a new search index instance:
const si = require('search-index')();
Adding Documents
Add documents to the index:
const documents = [
{ id: 1, name: 'First Document', content: 'This is the first document.' },
{ id: 2, name: 'Second Document', content: 'This is the second document.' }
];
si.PUT(documents);
Searching Documents
Perform a search within the index:
si.SEARCH({ query: [{ AND: 'first' }] })
.then(result => console.log(result));
Faceted Search
Using faceted search to filter results:
si.SEARCH({
query: [{ AND: 'document' }],
facets: { name: {} }
}).then(result => console.log(result));
Deleting Documents
Removing documents from the index:
si.DELETE([{ field: 'id', value: 1 }])
.then(result => console.log(result));
Updating Documents
Update existing documents in the index:
const updatedDoc = { id: 2, name: 'Updated Document', content: 'This document has been updated.' };
si.DELETE([{ field: 'id', value: 2 }])
.then(() => si.PUT([updatedDoc]))
.then(result => console.log(result));
App Example
Here is a simple app using `search-index`:
const si = require('search-index')();
const express = require('express');
const app = express();
app.use(express.json());
const documents = [
{ id: 1, name: 'First Document', content: 'This is the first document.' },
{ id: 2, name: 'Second Document', content: 'This is the second document.' }
];
si.PUT(documents);
app.get('/search', (req, res) => {
const query = req.query.q;
si.SEARCH({ query: [{ AND: query.split(' ') }] })
.then(result => res.send(result))
.catch(err => res.status(500).send(err));
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
This example sets up an Express server that indexes some documents and serves a search endpoint where users can query the indexed documents.
Hash: 07534e86a2d26ab55b75caebec8d5b7409f9869e89e9e955397792feb01c2444