Skip to content

Commit

Permalink
update readme for default_sorting_field
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeyson committed Jul 8, 2024
1 parent 0d2e892 commit e17d364
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ There are 2 ways to create a collection, either via [Ecto schema](https://hexdoc

#### Option 1: using Ecto

In this example, we're adding `person_id` that points to the id of `persons` schema.
In this example, we're adding `persons_id` that points to the id of `persons` schema.

> **Note**: we're using `<TABLE_NAME>_id`. If you have table
> e.g. named `persons`, it'll be `persons_id`.
```elixir
defmodule Person do
Expand All @@ -159,11 +162,11 @@ defmodule Person do
defimpl Jason.Encoder, for: __MODULE__ do
def encode(value, opts) do
value
|> Map.take([:id, :person_id, :name, :country])
|> Map.take([:id, :persons_id, :name, :country])
|> Enum.map(fn {key, val} ->
cond do
key === :id -> {key, to_string(Map.get(value, :id))}
key === :person_id -> {key, Map.get(value, :id)}
key === :persons_id -> {key, Map.get(value, :id)}
true -> {key, val}
end
end)
Expand All @@ -175,15 +178,18 @@ defmodule Person do
schema "persons" do
field(:name, :string)
field(:country, :string)
field(:person_id, :integer, virtual: true)
field(:persons_id, :integer, virtual: true)
end

@impl ExTypesense
def get_field_types do
primary_field = __MODULE__.__schema__(:source) <> "_id"

%{
default_sorting_field: "person_id",
# Or might as well just write persons_id instead. Up to you.
default_sorting_field: primary_field,
fields: [
%{name: "person_id", type: "int32"},
%{name: primary_field, type: "int32"},
%{name: "name", type: "string"},
%{name: "country", type: "string"}
]
Expand All @@ -205,10 +211,10 @@ schema = %{
name: "companies",
fields: [
%{name: "company_name", type: "string"},
%{name: "company_id", type: "int32"},
%{name: "companies_id", type: "int32"},
%{name: "country", type: "string"}
],
default_sorting_field: "company_id"
default_sorting_field: "companies_id"
}

ExTypesense.create_collection(schema)
Expand Down
30 changes: 16 additions & 14 deletions guides/cheatsheet.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ defmodule MyApp.Listings.Company do
defimpl Jason.Encoder, for: __MODULE__ do
def encode(value, opts) do
value
|> Map.take([:company_id, :name, :country])
|> Map.take([:companies_id, :name, :country])
|> Enum.map(fn {key, val} ->
if key === :company_id, do: {key, Map.get(value, :id)}, else: {key, val}
if key === :companies_id, do: {key, Map.get(value, :id)}, else: {key, val}
end)
|> Enum.into(%{})
|> Jason.Encode.map(opts)
Expand All @@ -27,15 +27,17 @@ defmodule MyApp.Listings.Company do
schema "companies" do
field(:name, :string)
field(:country, :string)
field(:company_id, :integer, virtual: true)
field(:companies_id, :integer, virtual: true)
end

@impl ExTypesense
def get_field_types do
primary_field = __MODULE__.__schema__(:source) <> "_id"

%{
default_sorting_field: "company_id",
default_sorting_field: primary_field,
fields: [
%{name: "company_id", type: "int32"},
%{name: primary_field, type: "int32"},
%{name: "name", type: "string"},
%{name: "country", type: "string"}
]
Expand All @@ -48,7 +50,7 @@ iex> ExTypesense.create_collection(Company)
{:ok,
%ExTypesense.Collection{
"created_at" => 1234567890,
"default_sorting_field" => "company_id",
"default_sorting_field" => "companies_id",
"fields" => [...],
"name" => "companies",
"num_documents" => 0,
Expand All @@ -67,17 +69,17 @@ iex> schema =
...> name: "companies",
...> fields: [
...> %{name: "company_name", type: "string"},
...> %{name: "num_employees", type: "int32"},
...> %{name: "companies_id", type: "int32"},
...> %{name: "country", type: "string", facet: true}
...> ],
...> default_sorting_field: "num_employees"
...> default_sorting_field: "companies_id"
...> }

iex> ExTypesense.create_collection(schema)
{:ok,
%ExTypesense.Collection{
"created_at" => 1234567890,
"default_sorting_field" => "num_employees",
"default_sorting_field" => "companies_id",
"fields" => [...],
"name" => "companies",
"num_documents" => 0,
Expand Down Expand Up @@ -110,7 +112,7 @@ iex> ExTypesense.update_collection("companies", schema)
%ExTypesense.Collection{
"created_at" => nil,
"name" => nil,
"default_sorting_field" => nil,
"default_sorting_field" => "companies_id",
"fields" => [...],
"num_documents" => 0,
"symbols_to_index" => [],
Expand All @@ -133,7 +135,7 @@ iex> ExTypesense.create_document(post, :create)
{:ok,
%{
"id" => "12",
"post_id" => 12,
"posts_id" => 12,
"title" => "the quick brown fox",
"collection_name" => "posts"
}
Expand All @@ -152,7 +154,7 @@ iex> ExTypesense.update_document(post, 0)
%{
"id" => "0",
"collection_name" => "posts",
"post_id" => 34,
"posts_id" => 34,
"title" => "test",
"description" => "lorem ipsum"
}
Expand All @@ -170,7 +172,7 @@ iex> ExTypesense.delete_document(Post, 0)
%{
"id" => "0",
"collection_name" => "posts",
"post_id" => 34,
"posts_id" => 34,
"title" => "test",
"description" => "lorem ipsum"
}
Expand Down Expand Up @@ -199,7 +201,7 @@ iex> ExTypesense.search(Post, params)
%{
"id" => "0",
"collection_name" => "posts",
"post_id" => 34,
"posts_id" => 34,
"title" => "test",
"description" => "lorem ipsum"
}
Expand Down
12 changes: 7 additions & 5 deletions lib/ex_typesense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ defmodule ExTypesense do
defimpl Jason.Encoder, for: __MODULE__ do
def encode(value, opts) do
value
|> Map.take([:id, :person_id, :name, :age])
|> Map.take([:id, :persons_id, :name, :age])
|> Enum.map(fn {key, val} ->
cond do
key === :id -> {key, to_string(Map.get(value, :id))}
key === :person_id -> {key, Map.get(value, :id)}
key === :persons_id -> {key, Map.get(value, :id)}
true -> {key, val}
end
end)
Expand All @@ -30,16 +30,18 @@ defmodule ExTypesense do
schema "persons" do
field :name, :string
field :age, :integer
field :person_id, :integer, virtual: true
field :persons_id, :integer, virtual: true
end
@impl ExTypesense
def get_field_types do
primary_field = __MODULE__.__schema__(:source) <> "_id"
%{
default_sorting_field: "person_id",
default_sorting_field: primary_field,
fields:
[
%{name: "person_id", type: "int32"},
%{name: primary_field, type: "int32"},
%{name: "name", type: "string"},
%{name: "age", type: "integer"}
]
Expand Down
24 changes: 7 additions & 17 deletions lib/ex_typesense/collection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ defmodule ExTypesense.Collection do
...> name: "companies",
...> fields: [
...> %{name: "company_name", type: "string"},
...> %{name: "company_id", type: "int32"},
...> %{name: "companies_id", type: "int32"},
...> %{name: "country", type: "string", facet: true}
...> ],
...> default_sorting_field: "company_id"
...> default_sorting_field: "companies_id"
...> }
iex> ExTypesense.create_collection(schema)
%ExTypesense.Collection{
created_at: 1234567890,
default_sorting_field: "company_id",
default_sorting_field: "companies_id",
fields: [...],
name: "companies",
num_documents: 0,
Expand All @@ -193,7 +193,7 @@ defmodule ExTypesense.Collection do
iex> ExTypesense.create_collection(Person)
%ExTypesense.Collection{
created_at: 1234567890,
default_sorting_field: "person_id",
default_sorting_field: "persons_id",
fields: [...],
name: "persons",
num_documents: 0,
Expand Down Expand Up @@ -254,18 +254,7 @@ defmodule ExTypesense.Collection do
%ExTypesense.Collection{
created_at: 1234567890,
name: companies,
default_sorting_field: "company_id",
fields: [...],
num_documents: 0,
symbols_to_index: [],
token_separators: []
}
iex> ExTypesense.update_collection_fields(Company, fields)
%ExTypesense.Collection{
created_at: 1234567890,
name: companies,
default_sorting_field: "company_id",
default_sorting_field: "companies_id",
fields: [...],
num_documents: 0,
symbols_to_index: [],
Expand Down Expand Up @@ -304,7 +293,8 @@ defmodule ExTypesense.Collection do
@doc """
Permanently drops a collection by collection name or module name.
**Note**: dropping a collection does not remove the referenced alias, only the indexed documents.
**Note**: dropping a collection does not remove the referenced
alias, only the indexed documents.
"""
@doc since: "0.1.0"
@spec drop_collection(Connection.t(), name :: String.t() | module()) :: response()
Expand Down
18 changes: 9 additions & 9 deletions lib/ex_typesense/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ defmodule ExTypesense.Document do
...> %{
...> id: "34",
...> collection_name: "posts",
...> post_id: 34,
...> posts_id: 34,
...> title: "the quick brown fox",
...> description: "jumps over the lazy dog"
...> }
Expand All @@ -244,7 +244,7 @@ defmodule ExTypesense.Document do
%{
"id" => "34",
"collection_name" => "posts",
"post_id" => 34,
"posts_id" => 34,
"title" => "the quick brown fox",
"description" => "jumps over the lazy dog"
}
Expand Down Expand Up @@ -284,24 +284,24 @@ defmodule ExTypesense.Document do
...> %{
...> id: "94",
...> collection_name: "posts",
...> post_id: 94,
...> posts_id: 94,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> updated_post =
...> %{
...> id: "94",
...> collection_name: "posts",
...> post_id: 94,
...> posts_id: 94,
...> title: "test"
...> }
iex> ExTypesense.update_document(updated_post)
{:ok,
%{
"id" => "94",
"collection_name" => "posts",
"post_id" => 94,
"title" => "test"
"posts_id" => 94,
"title" => "sample post"
}
}
"""
Expand Down Expand Up @@ -386,7 +386,7 @@ defmodule ExTypesense.Document do
{:ok,
%{
"id" => "1",
"post_id" => 1,
"posts_id" => 1,
"title" => "our first post",
"collection_name" => "posts"
}
Expand All @@ -403,15 +403,15 @@ defmodule ExTypesense.Document do
...> %{
...> id: "12",
...> collection_name: "posts",
...> post_id: 22,
...> posts_id: 22,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> ExTypesense.delete_document({"posts", 12})
{:ok,
%{
"id" => "12",
"post_id" => 22,
"posts_id" => 22,
"title" => "the quick brown fox",
"collection_name" => "posts"
}
Expand Down

0 comments on commit e17d364

Please sign in to comment.