-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b74c335
commit 625fdfd
Showing
18 changed files
with
382 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# 2024-10-25 | ||
|
||
## Req call typesense API alway :timeout, but typesense was updated. | ||
|
||
```elixir | ||
** (MatchError) no match of right hand side value: {:error, %Req.TransportError{reason: :timeout}} | ||
(save_it 0.2.0-rc.1) lib/migration/typesense.ex:11: Migration.Typesense.create_collection!/1 | ||
priv/typesense/reset.exs:3: (file) | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Typesense | ||
|
||
## Typesense API Errors | ||
|
||
``` | ||
# 400 Bad Request - The request could not be understood due to malformed syntax. | ||
# 401 Unauthorized - Your API key is wrong. | ||
# 404 Not Found - The requested resource is not found. | ||
# 409 Conflict - When a resource already exists. | ||
# 422 Unprocessable Entity - Request is well-formed, but cannot be processed. | ||
# 503 Service Unavailable - We’re temporarily offline. Please try again later. | ||
``` | ||
|
||
docs: https://typesense.org/docs/27.1/api/api-errors.html#api-errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Migration.Typesense do | ||
def list_collections() do | ||
req = build_request("/collections") | ||
{:ok, res} = Req.get(req) | ||
|
||
res.body | ||
end | ||
|
||
def create_collection!(schema) do | ||
req = build_request("/collections") | ||
{:ok, res} = Req.post(req, json: schema) | ||
|
||
res.body | ||
end | ||
|
||
def delete_collection!(collection_name) do | ||
req = build_request("/collections/#{collection_name}") | ||
{:ok, res} = Req.delete(req) | ||
|
||
res.body | ||
end | ||
|
||
defp get_env() do | ||
url = Application.fetch_env!(:save_it, :typesense_url) | ||
api_key = Application.fetch_env!(:save_it, :typesense_api_key) | ||
|
||
{url, api_key} | ||
end | ||
|
||
defp build_request(path) do | ||
{url, api_key} = get_env() | ||
|
||
Req.new( | ||
base_url: url, | ||
url: path, | ||
headers: [ | ||
{"Content-Type", "application/json"}, | ||
{"X-TYPESENSE-API-KEY", api_key} | ||
] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Migration.Typesense.Note do | ||
alias Migration.Typesense | ||
|
||
@notes_schema %{ | ||
"name" => "notes", | ||
"fields" => [ | ||
# TODO: 第一步先实现文本,今后再考虑图片 | ||
%{"name" => "content", "type" => "string"}, | ||
# references photos.id | ||
# note: 抉择:这个 app 核心是给予图片的视觉笔记,暂时不考虑单独 text 的笔记 | ||
# %{"name" => "photo_id", "type" => "string"}, | ||
# note: 既然不能实现 RDB reference,那么就直接存储 file_id | ||
%{"name" => "file_id", "type" => "string"}, | ||
%{"name" => "belongs_to_id", "type" => "string"}, | ||
%{"name" => "inserted_at", "type" => "int64"}, | ||
%{"name" => "updated_at", "type" => "int64"} | ||
], | ||
"default_sorting_field" => "inserted_at" | ||
} | ||
|
||
def create_collection!() do | ||
Typesense.create_collection!(@notes_schema) | ||
end | ||
|
||
def reset!() do | ||
Typesense.delete_collection!(@notes_schema["name"]) | ||
Typesense.create_collection!(@notes_schema) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Migration.Typesense.Photo do | ||
alias Migration.Typesense | ||
|
||
@photos_schema %{ | ||
"name" => "photos", | ||
"fields" => [ | ||
# image: base64 encoded string | ||
%{"name" => "image", "type" => "image", "store" => false}, | ||
%{ | ||
"name" => "image_embedding", | ||
"type" => "float[]", | ||
"embed" => %{ | ||
"from" => ["image"], | ||
"model_config" => %{ | ||
"model_name" => "ts/clip-vit-b-p32" | ||
} | ||
} | ||
}, | ||
%{"name" => "caption", "type" => "string", "optional" => true, "facet" => false}, | ||
# "telegram://<bot_id>/<file_id>" | ||
# TODO: 不能再简单的 reset 了,reset 会导致数据丢失,应该合理 migrate 数据 | ||
%{"name" => "url", "type" => "string"}, | ||
# chat.id -> string | ||
%{"name" => "belongs_to_id", "type" => "string"}, | ||
# unix timestamp | ||
%{"name" => "inserted_at", "type" => "int64"} | ||
], | ||
"default_sorting_field" => "inserted_at" | ||
} | ||
|
||
def create_collection!() do | ||
Typesense.create_collection!(@photos_schema) | ||
end | ||
|
||
def reset!() do | ||
Typesense.delete_collection!(@photos_schema["name"]) | ||
Typesense.create_collection!(@photos_schema) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule SaveIt.NoteService do | ||
require Logger | ||
|
||
alias SmallSdk.Typesense | ||
|
||
def create_note!(%{ | ||
content: content, | ||
file_id: file_id, | ||
belongs_to_id: belongs_to_id | ||
}) do | ||
now_unix = DateTime.utc_now() |> DateTime.to_unix() | ||
|
||
note_create_input = | ||
%{ | ||
content: content, | ||
file_id: file_id | ||
} | ||
|> Map.put(:belongs_to_id, Integer.to_string(belongs_to_id)) | ||
|> Map.put(:inserted_at, now_unix) | ||
|> Map.put(:updated_at, now_unix) | ||
|
||
doc = | ||
Typesense.create_document!( | ||
"notes", | ||
note_create_input | ||
) | ||
|
||
Logger.debug("doc: #{inspect(doc)}") | ||
doc | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.