Typesense 开源搜索引擎教程

Typesense 开源搜索引擎教程

typesenseOpen Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences项目地址:https://gitcode.com/gh_mirrors/ty/typesense

项目介绍

Typesense 是一个现代、快速、开源的搜索引擎,旨在为开发者提供一个易于使用的搜索解决方案。它是一个开源的 Algolia 和 Pinecone 的替代品,也是一个比 Elasticsearch 更易于使用的替代品。Typesense 使用最新的算法,充分利用了硬件能力和机器学习的最新进展,提供了即搜即得、自动完成、分面导航、地理搜索、模糊搜索、向量搜索和语义搜索等功能。

项目快速启动

环境准备

在开始之前,请确保您的系统满足以下依赖要求:

  • C++11 兼容的编译器(GCC >= 4.9.0, Apple Clang >= 8.0, Clang >= 3.9.0)
  • Snappy
  • zlib
  • OpenSSL (>=1.0.2)
  • curl
  • ICU

构建 Typesense

  1. 克隆项目仓库:

    git clone https://github.com/typesense/typesense.git
    cd typesense
    
  2. 安装依赖:

    sudo apt-get install -y build-essential snappy zlib1g-dev libssl-dev libcurl4-openssl-dev libicu-dev
    
  3. 使用 Bazel 构建 Typesense:

    bazel build //:typesense-server
    

启动 Typesense 服务器

构建完成后,您可以启动 Typesense 服务器: bash ./bazel-bin/typesense-server --data-dir=/path/to/data --api-key=your_api_key

应用案例和最佳实践

案例一:xkcd 漫画搜索

Typesense 可以用于构建一个按主题浏览 xkcd 漫画的应用。以下是一个简单的示例:

const Typesense = require('typesense');

const client = new Typesense.Client({
  nodes: [{
    host: 'localhost',
    port: '8108',
    protocol: 'http'
  }],
  apiKey: 'your_api_key',
  connectionTimeoutSeconds: 2
});

const schema = {
  name: 'comics',
  fields: [
    { name: 'title', type: 'string' },
    { name: 'transcript', type: 'string' },
    { name: 'num', type: 'int32' }
  ]
};

client.collections().create(schema).then(() => {
  console.log('Collection created');
});

案例二:电子商务商店前端

使用 Typesense 和 Next.js 构建一个电子商务商店前端,提供快速且耐错的搜索体验。

import TypesenseInstantSearchAdapter from 'typesense-instantsearch-adapter';

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: 'your_api_key',
    nodes: [
      {
        host: 'localhost',
        port: '8108',
        protocol: 'http'
      }
    ]
  },
  additionalSearchParameters: {
    queryBy: 'title,description'
  }
});

const searchClient = typesenseInstantsearchAdapter.searchClient;

const search = instantsearch({
  indexName: 'products',
  searchClient
});

search.addWidgets([
  instantsearch.widgets.searchBox({
    container: '#searchbox'
  }),
  instantsearch.widgets.hits({
    container: '#hits',
    templates: {
      item: `
        <div>
          <strong>{{#helpers.highlight}}{ "attribute": "title" }{{/helpers.highlight}}</strong>
          <p>{{#helpers.highlight}}{ "attribute": "description" }{{/helpers.highlight}}</p>
        </div>
      `
    }
  })
]);

search.start();

典型生态项目

Typesense Python 客户端

Typesense 提供了官方的 Python 客户端,方便 Python 开发者与 Typesense 服务器进行交互。

from typesense import Client

client = Client({
  'nodes': [{
    'host': 'localhost',
    'port': '8108',
    'protocol': 'http'

typesenseOpen Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences项目地址:https://gitcode.com/gh_mirrors/ty/typesense

  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

解雁淞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值