Typesense Firestore 搜索扩展教程

Typesense Firestore 搜索扩展教程

firestore-typesense-searchFirebase Extension to automatically push Firestore documents to Typesense for full-text search with typo tolerance, faceting, and more项目地址:https://gitcode.com/gh_mirrors/fi/firestore-typesense-search

1. 项目的目录结构及介绍

firestore-typesense-search/
├── functions/
│   ├── index.js
│   ├── package.json
│   └── ...
├── extension.yaml
├── README.md
└── ...
  • functions/: 包含云函数代码,用于同步 Firestore 文档到 Typesense。
    • index.js: 主入口文件,定义了同步逻辑。
    • package.json: 依赖管理文件。
  • extension.yaml: 扩展配置文件,定义了扩展的参数和行为。
  • README.md: 项目说明文档。

2. 项目的启动文件介绍

functions/index.js

这是项目的启动文件,主要负责以下功能:

  • 监听 Firestore 集合的变化(创建、更新、删除)。
  • 将 Firestore 文档同步到 Typesense。
  • 提供数据回填功能。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Typesense = require('typesense');

admin.initializeApp();

const typesenseClient = new Typesense.Client({
  nodes: [
    {
      host: 'your-typesense-host',
      port: '8108',
      protocol: 'http',
    },
  ],
  apiKey: 'your-typesense-api-key',
});

exports.syncFirestoreToTypesense = functions.firestore
  .document('books/{bookID}')
  .onWrite(async (change, context) => {
    const book = change.after.exists ? change.after.data() : null;
    const bookId = context.params.bookID;

    if (book) {
      await typesenseClient.collections('books').documents().upsert({
        id: bookId,
        ...book,
      });
    } else {
      await typesenseClient.collections('books').documents(bookId).delete();
    }
  });

3. 项目的配置文件介绍

extension.yaml

这是扩展的配置文件,定义了扩展的参数和行为。

name: firestore-typesense-search
version: 0.1.0
spec:
  params:
    - name: FIRESTORE_COLLECTION
      description: Firestore collection that needs to be indexed into Typesense
      default: books
    - name: TYPESENSE_HOSTS
      description: A comma-separated list of Typesense Hosts
      default: your-typesense-host
    - name: TYPESENSE_API_KEY
      description: Typesense API Key
      default: your-typesense-api-key
  resources:
    - name: syncFirestoreToTypesense
      type: firebaseextensions.v1beta.function
      description: Sync Firestore documents to Typesense
      entryPoint: syncFirestoreToTypesense
      location: us-central1
      runtime: nodejs14
      eventTrigger:
        resource: projects/${PROJECT_ID}/databases/(default)/documents/${FIRESTORE_COLLECTION}/{bookID}
        eventType: providers/cloud.firestore/eventTypes/document.write
  • FIRESTORE_COLLECTION: 需要索引到 Typesense 的 Firestore 集合。
  • TYPESENSE_HOSTS: Typesense 主机的逗号分隔列表。
  • TYPESENSE_API_KEY: Typesense API 密钥。

通过这些配置,可以灵活地设置扩展的行为,以适应不同的项目需求。

firestore-typesense-searchFirebase Extension to automatically push Firestore documents to Typesense for full-text search with typo tolerance, faceting, and more项目地址:https://gitcode.com/gh_mirrors/fi/firestore-typesense-search

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

史多苹Thomas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值