基于rust 编写的高性能全文检索服务

sonic 是基于rust 编写的一个高性能,可简单替换es 的方案,没有提供http 接口,而是走的tcp 协议,目前已经
有了好多语言的包装库,可以方便使用

参考使用

  • 环境准备
 
version: "3"
services:
   sonic:
      image:  valeriansaliou/sonic:v1.3.0
      ports: 
      - "1491:1491"
      volumes:
      - "./config.cfg:/etc/sonic.cfg"
   meilisearch:
      image:  getmeili/meilisearch
      ports: 
      - "7700:7700"
  • golang 代码
package main
import (
   "fmt"
   "os"
   "github.com/expectedsh/go-sonic/sonic"
   "github.com/meilisearch/meilisearch-go"
)
func main() {
   ingester, err := sonic.NewIngester("localhost", 1491, "dalong")
   if err != nil {
      panic(err)
   }
   _ = ingester.BulkPush("movies", "general", 3, []sonic.IngestBulkRecord{
      {Object: "id:6ab56b4kk3", Text: "Star wars"},
      {Object: "id:5hg67f8dg5", Text: "Spider man"},
      {Object: "id:1m2n3b4vf6", Text: "Batman"},
      {Object: "id:1111111", Text: "荣锋亮 测试应用 技术测试"},
      {Object: "id:68d96h5h9d0", Text: "This is another movie"},
   })
   _ = ingester.BulkPush("movies", "general", 3, []sonic.IngestBulkRecord{
      {Object: "id:6ab56b4kk3", Text: "Star wars"},
      {Object: "id:5hg67f8dg5", Text: "Spider man"},
      {Object: "id:1m2n3b4vf6", Text: "Batman"},
      {Object: "id:1111112", Text: "111荣锋亮 测试应用 技术测试"},
      {Object: "id:68d96h5h9d0", Text: "This is another movie"},
   })
   _ = ingester.BulkPush("movies", "general", 3, []sonic.IngestBulkRecord{
      {Object: "id:6ab56b4kk3", Text: "Star wars"},
      {Object: "id:5hg67f8dg5", Text: "Spider man"},
      {Object: "id:1m2n3b4vf6", Text: "Batman"},
      {Object: "id:1111113", Text: "111荣锋亮 测试应用 技术测试"},
      {Object: "id:68d96h5h9d0", Text: "This is another movie"},
   })
   search, err := sonic.NewSearch("localhost", 1491, "dalong")
   if err != nil {
      panic(err)
   }
   results, _ := search.Query("movies", "general", "测", 10, 0)
   fmt.Println(results)
   var client = meilisearch.NewClient(meilisearch.Config{
      Host: "http://127.0.0.1:7700",
   })
   // Create an index if your index does not already exist
   if index, _ := client.Indexes().Get("books"); index.UID == "" {
      fmt.Println("create index")
      _, err = client.Indexes().Create(meilisearch.CreateIndexRequest{
         UID: "books",
      })
      if err != nil {
         fmt.Println(err)
         os.Exit(1)
      }
   } else {
      fmt.Println("index is created")
   }
   documents := []map[string]interface{}{
      {"book_id": 123, "title": "Pride and Prejudice"},
      {"book_id": 456, "title": "Le Petit Prince"},
      {"book_id": 1, "title": "Alice In Wonderland"},
      {"book_id": 1344, "title": "The Hobbit"},
      {"book_id": 1345, "title": "荣锋亮 测试 应用"},
      {"book_id": 1346, "title": "荣锋亮 测试 应用"},
      {"book_id": 1347, "title": "荣锋亮 测试 使用应用"},
      {"book_id": 1348, "title": `摘要:johnfercher/maroto 借鉴了bootstrap 的网格模式,使用了gofpdf 生成pdf,是一个很不错的golang pdf 工具 有一个问题是不支持中文(因为配置写的的原因)看到网上有一个中国人fork添加了AddUTF8Font 支持,这样 中文就可以显示了,以下是一个参考的使 `},
      {"book_id": 4, "title": "Harry Potter and the Half-Blood Prince"},
      {"book_id": 42, "title": "The Hitchhiker's Guide to the Galaxy"},
   }
   updateRes, err := client.Documents("books").AddOrUpdate(documents) // => { "updateId": 0 }
   if err != nil {
      fmt.Println(err)
      os.Exit(1)
   }
   fmt.Println(updateRes.UpdateID)
   searchRes, err := client.Search("books").Search(meilisearch.SearchRequest{
      Query: "使用",
      Limit: 10,
   })
   if err != nil {
      fmt.Println(err)
      os.Exit(1)
   }
   fmt.Println(searchRes.Hits)
}

说明

基于rust 开发的全文检索引擎还是比较多的,MeiliSearch 也是一个类似的,同时比较活跃,提供的功能也比较多

参考资料

https://github.com/valeriansaliou/sonic
https://github.com/expectedsh/go-sonic
https://github.com/tantivy-search/tantivy
https://github.com/meilisearch/MeiliSearch

posted on   荣锋亮  阅读(1642)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-12-03 polynote 安装试用
2018-12-03 openresty redis all in one docker demo
2018-12-03 hasura graphql-engine &&patroni docker-compose 环境运行
2018-12-03 使用patroni 解决hasura graphql-engine pg 数据库ha的问题
2018-12-03 使用patroni 构建高可用的pg 数据库
2018-12-03 zsh:no matches found 问题解决
2017-12-03 caddy quic 协议试用&& 几个问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示