The "丐版" (community) version of mongodb refers to the Community Edition of mongodb. The Community Edition of mongodb full-text search does not support a Chinese engine. The term "丐版" is a playful reference, comparing the Community Edition to a beggar when contrasted with the Enterprise Edition or Altas Search. To achieve Chinese support, you must handle it yourself, implementing your own Chinese word segmentation.

Environment

The backend is nestjs, the ORM is mongoose, and the database is mongodb. GitHub repository

mongodb

To facilitate development and testing, use docker for local deployment. The relevant yml content is as follows

services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: unless-stopped
    ports:
      - 27017:27017
    volumes:
      # 数据文件
      - ./volumes/mongodb/db:/data/db
      # 备份文件
      - ./volumes/mongodb/backup:/data/backup
      # logs 文件
      - ./volumes/mongodb/logs:/data/logs
      # 配置文件
      - ./volumes/mongodb/conf:/etc/mongod.conf
      # init script
      - ./volumes/mongodb/scripts:/docker-entrypoint-initdb.d/
    # command: ["--replSet", "rs0"]
    # rs.initiate({"_id": "rs0", "members":[{"_id": 0, "host": "192.168.10.121:27017"}]});
    environment:
      - TZ=Asia/Shanghai
    networks:
      - dev
networks:
  dev:
    name: local-dev
services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: unless-stopped
    ports:
      - 27017:27017
    volumes:
      # 数据文件
      - ./volumes/mongodb/db:/data/db
      # 备份文件
      - ./volumes/mongodb/backup:/data/backup
      # logs 文件
      - ./volumes/mongodb/logs:/data/logs
      # 配置文件
      - ./volumes/mongodb/conf:/etc/mongod.conf
      # init script
      - ./volumes/mongodb/scripts:/docker-entrypoint-initdb.d/
    command: mongod --replSet rs0
    healthcheck:
      test: |
        mongosh --eval "try { rs.status().ok } catch (e) { rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] }).ok }"
      start_period: 0s
      interval: 500ms
      timeout: 5s
      retries: 5
    # rs.initiate({"_id": "rs0", "members":[{"_id": 0, "host": "192.168.10.121:27017"}]});
    environment:
      - TZ=Asia/Shanghai
    networks:
      - dev
networks:
  dev:
    name: local-dev
docker compose -f docker-compose.yml pull
docker compose -f docker-compose.yml up -d

nestjs

# pnpm add -g @nestjs/cli
# npm i -g @nestjs/cli
nest new fulltext-search
cd fulltext-search
pnpm i
  • Repository initialization
  • Enable strict mode in tsconfig.json
  • monorepo -> app, sub-mongo-change
  • lib -> db
  • mongoose
  • e2e testing
  • ts-mongo migration script for data processing and seeding
  • @node-rs/jieba word segmentation
  • sub-mongo-change change data capture (CDC)
  • Basic query testing
  • Multi-entity detection
  • Other considerations (value objects, word segmentation algorithms, write-back to the same data source, comparison with Elasticsearch, benefits and drawbacks, trade-offs in different scenarios)