基於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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM