npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@namancam9/opensearch

v1.0.0

Published

Full-text search engine for Node.js powered by BM25 ranking. Supports SQLite, PostgreSQL, and MySQL.

Readme

@namancam9/opensearch

Thư viện tìm kiếm toàn văn bản (full-text search) cho Node.js, sử dụng thuật toán xếp hạng BM25. Hỗ trợ SQLite, PostgreSQL và MySQL.

Cài đặt

npm install @namancam9/opensearch

SQLite hoạt động ngay mà không cần cấu hình gì thêm. Với MySQL hoặc PostgreSQL, cài thêm driver tương ứng:

# MySQL
npm install mysql2

# PostgreSQL
npm install pg

Hướng dẫn sử dụng

1. Định nghĩa mapping

Mapping cho engine biết những trường nào cần được đánh index và mức độ quan trọng của mỗi trường.

const { InvertedIndex } = require('@namancam9/opensearch');

const mapping = {
  id: 'id',
  fields: {
    title:       { type: 'text', weight: 3 },
    description: { type: 'text', weight: 1 },
  },
};

weight kiểm soát mức độ ảnh hưởng của một kết quả khớp trong trường đó đến điểm số. Kết quả khớp trong title (weight 3) có điểm cao gấp 3 lần so với cùng từ đó trong description (weight 1).

2. Tạo index

SQLite (mặc định — không cần cấu hình, không cần DB ngoài)

const index = await InvertedIndex.create({ mapping });

Dữ liệu được lưu vào file opensearch.db theo mặc định. Có thể thay đổi đường dẫn hoặc dùng in-memory:

const index = await InvertedIndex.create({ mapping, dbPath: './my-search.db' });
const index = await InvertedIndex.create({ mapping, dbPath: ':memory:' });

MySQL

const { InvertedIndex, MySQLAdapter } = require('@namancam9/opensearch');

const adapter = await MySQLAdapter.create({
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: 'your_password',
  database: 'your_database',
});
const index = await InvertedIndex.create({ mapping, adapter });

PostgreSQL

const { InvertedIndex, PostgreSQLAdapter } = require('@namancam9/opensearch');

const adapter = await PostgreSQLAdapter.create({
  host: 'localhost',
  port: 5432,
  user: 'postgres',
  password: 'your_password',
  database: 'your_database',
});
const index = await InvertedIndex.create({ mapping, adapter });

3. Đánh index tài liệu

buildAll — xóa toàn bộ và xây dựng lại index:

await index.buildAll([
  { id: '1', title: 'iPhone 15 Pro Max', description: 'Điện thoại hàng đầu của Apple' },
  { id: '2', title: 'Samsung Galaxy S24', description: 'Điện thoại Samsung tích hợp AI' },
  { id: '3', title: 'MacBook Pro 16', description: 'Laptop Apple dành cho chuyên gia' },
]);

add — thêm một tài liệu:

await index.add({
  id: '4',
  title: 'iPad Pro',
  description: 'Máy tính bảng Apple chip M2',
});

update — cập nhật tài liệu đã có:

await index.update({
  id: '1',
  title: 'iPhone 16 Pro Max',
  description: 'Apple flagship với chip A18',
});

remove — xóa tài liệu theo ID:

await index.remove('2');

4. Tìm kiếm

const results = await index.search('apple laptop');
// [
//   { id: '3', score: 7.42 },
//   { id: '1', score: 1.23 },
//   { id: '4', score: 0.98 },
// ]

Kết quả được sắp xếp theo mức độ liên quan (điểm cao nhất trước). BM25 tính đến:

  • Tần suất từ (TF) — từ xuất hiện càng nhiều trong tài liệu thì điểm càng cao
  • Tần suất nghịch đảo tài liệu (IDF) — từ hiếm gặp có điểm cao hơn từ phổ biến
  • Độ dài tài liệu — tài liệu ngắn hơn có cùng từ khớp sẽ được xếp hạng cao hơn
  • Trọng số trường (weight) — kết quả khớp ở trường có weight cao sẽ cho điểm nhiều hơn

5. Đóng kết nối

Luôn đóng kết nối sau khi sử dụng xong để giải phóng tài nguyên:

await index.close();

API

| Phương thức | Mô tả | |-------------|-------| | InvertedIndex.create({ mapping, dbPath?, adapter? }) | Tạo index | | index.buildAll(documents) | Xóa và xây dựng lại toàn bộ index | | index.add(document) | Thêm một tài liệu | | index.update(document) | Cập nhật một tài liệu | | index.remove(id) | Xóa tài liệu theo ID | | index.search(query) | Trả về [{ id, score }] sắp xếp theo mức độ liên quan | | index.close() | Đóng kết nối DB |

Adapters

| Adapter | Driver | Cài đặt | |---------|--------|---------| | SQLiteAdapter | sql.js (tích hợp sẵn) | có sẵn | | MySQLAdapter | mysql2 | npm install mysql2 | | PostgreSQLAdapter | pg | npm install pg |

Tất cả các bảng đều có tiền tố opensearch_ để tránh xung đột với các bảng hiện có trong database của bạn.

Giấy phép

ISC