diff --git a/README.md b/README.md index 231fcf8..1260a55 100644 --- a/README.md +++ b/README.md @@ -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 `_id`. If you have table +> e.g. named `persons`, it'll be `persons_id`. ```elixir defmodule Person do @@ -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) @@ -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"} ] @@ -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) diff --git a/guides/cheatsheet.cheatmd b/guides/cheatsheet.cheatmd index 4c8b867..1be467f 100644 --- a/guides/cheatsheet.cheatmd +++ b/guides/cheatsheet.cheatmd @@ -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) @@ -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"} ] @@ -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, @@ -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, @@ -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" => [], @@ -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" } @@ -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" } @@ -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" } @@ -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" } diff --git a/lib/ex_typesense.ex b/lib/ex_typesense.ex index c6976eb..2afe415 100644 --- a/lib/ex_typesense.ex +++ b/lib/ex_typesense.ex @@ -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) @@ -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"} ] diff --git a/lib/ex_typesense/collection.ex b/lib/ex_typesense/collection.ex index 204063e..0bb2049 100644 --- a/lib/ex_typesense/collection.ex +++ b/lib/ex_typesense/collection.ex @@ -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, @@ -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, @@ -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: [], @@ -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() diff --git a/lib/ex_typesense/document.ex b/lib/ex_typesense/document.ex index 7d89b33..a4d520e 100644 --- a/lib/ex_typesense/document.ex +++ b/lib/ex_typesense/document.ex @@ -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" ...> } @@ -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" } @@ -284,7 +284,7 @@ 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) @@ -292,7 +292,7 @@ defmodule ExTypesense.Document do ...> %{ ...> id: "94", ...> collection_name: "posts", - ...> post_id: 94, + ...> posts_id: 94, ...> title: "test" ...> } iex> ExTypesense.update_document(updated_post) @@ -300,8 +300,8 @@ defmodule ExTypesense.Document do %{ "id" => "94", "collection_name" => "posts", - "post_id" => 94, - "title" => "test" + "posts_id" => 94, + "title" => "sample post" } } """ @@ -386,7 +386,7 @@ defmodule ExTypesense.Document do {:ok, %{ "id" => "1", - "post_id" => 1, + "posts_id" => 1, "title" => "our first post", "collection_name" => "posts" } @@ -403,7 +403,7 @@ 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) @@ -411,7 +411,7 @@ defmodule ExTypesense.Document do {:ok, %{ "id" => "12", - "post_id" => 22, + "posts_id" => 22, "title" => "the quick brown fox", "collection_name" => "posts" }