Algoliax v0.2.0 Algoliax behaviour View Source
Algoliax is wrapper for Algolia api
Configuration
Algoliax needs only :api_key
and application_id
config. These configs can either be on config files or using environment varialble "ALGOLIA_API_KEY"
and "ALGOLIA_APPLICATION_ID"
.
config :algoliax,
api_key: "",
application_id: ""
Usage
:index_name
: specificy the index where the object will be added on. Required:object_id
: specify the attribute used to as algolia objectID. Default:id
.:repo
: Specify an Ecto repo to be use to fecth records. Defaultnil
:preloads
: Specify preloads for a given schema. Default[]
:cursor_field
: specify the column to be used to order and go through a given table. Default:id
Any valid Algolia settings, using snake case or camel case. Ex: Algolia attributeForFaceting
can be configured with :attribute_for_faceting
On first call to Algolia, we check that the settings on Algolia are up to date.
Attributes
Objects send to Algolia are built using the attributes defined in the module using attribute/1
, attributes/1
or attribute/2
Example
defmodule People do
use Algoliax,
index_name: :people,
object_id: :reference,
attribute_for_faceting: ["age"],
custom_ranking: ["desc(update_at)"]
defstruct reference: nil, last_name: nil, first_name: nil, age: nil
attributes([:first_name, :last_name, :age])
attribute(:updated_at, ~U[2019-07-18 08:45:56.639380Z] |> DateTime.to_unix())
attribute :full_name do
Map.get(model, :first_name, "") <> " " <> Map.get(model, :last_name, "")
end
attribute :nickname do
Map.get(model, :first_name, "") |> String.downcase()
end
end
Link to this section Summary
Functions
Define an attribute to be added to the indexed object with a value taken from the model (map/struct)
Define an attributes to be indexed with a computed value without or with model access
Define multiple attributes to be added to the indexed object with a value taken from the model (map/struct)
Generate a secured api key with filter
Callbacks
Configure index
Delete index
Delete object from algolia. By passing the model, the object is retreived using the object_id configured
Fetch object from algolia. By passing the model, the object is retreived using the object_id configured
Get index settings from Algolia
Reindex all objects (Ecto specific)
Reindex a part of object by providing an Ecto query (Ecto specific)
Reindex atomicly (Ecto specific)
Add/update object. The object is added/updated to algolia with the object_id configured.
Save multiple object at once
Search for index values
Search for facet values
Check if current object must be indexed or not. By default it's always true. To override this behaviour overide this function in your model
Link to this section Functions
Define an attribute to be added to the indexed object with a value taken from the model (map/struct)
Example
attribute :id
Define an attributes to be indexed with a computed value without or with model access
Example without model access
The model is not available.
attribute :utc_now, DateTime.utc_now()
Example with model access
The model is available inside the block.
attribute :uppcase_name do
model.name |> String.upcase()
end
Define multiple attributes to be added to the indexed object with a value taken from the model (map/struct)
Example
attributes :id, :inserted_at
Generate a secured api key with filter
Examples
Algoliax.generate_secured_api_key("reference:10")
Algoliax.generate_secured_api_key("reference:10 OR nickname:john")
Link to this section Callbacks
Configure index
Delete index
Delete object from algolia. By passing the model, the object is retreived using the object_id configured
Example
people = %People{reference: 10, last_name: "Doe", first_name: "John", age: 20}
People.delete_object(people)
Fetch object from algolia. By passing the model, the object is retreived using the object_id configured
Example
people = %People{reference: 10, last_name: "Doe", first_name: "John", age: 20}
People.get_object(people)
Get index settings from Algolia
Reindex all objects (Ecto specific)
Example
People.reindex(query)
Available options:
:force_delete
: delete objects whereto_be_indexed?
isfalse
reindex(query, opts)
View Sourcereindex(query :: Ecto.Query.t(), opts :: Keyword.t()) :: {:ok, map()} | {:error, map()}
Reindex a part of object by providing an Ecto query (Ecto specific)
Example
import Ecto.Query
query = from(
p in People,
where: p.age > 45
)
People.reindex(query)
Available options:
:force_delete
: delete objects that are in query and whereto_be_indexed?
is false
Reindex atomicly (Ecto specific)
Add/update object. The object is added/updated to algolia with the object_id configured.
Example
people = %People{reference: 10, last_name: "Doe", first_name: "John", age: 20},
People.save_object(people)
Save multiple object at once
Options
:force_delete
- iftrue
will trigger a "deleteObject" on object that must not be indexed. Defaultfalse
Example
peoples = [
%People{reference: 10, last_name: "Doe", first_name: "John", age: 20},
%People{reference: 89, last_name: "Einstein", first_name: "Albert", age: 65}
]
People.save_objects(peoples)
People.save_objects(peoples, force_delete: true)
Search for index values
Example
iex> People.search("John")
{:ok,
%{
"exhaustiveNbHits" => true,
"hits" => [
%{
"_highlightResult" => %{
"full_name" => %{
"fullyHighlighted" => false,
"matchLevel" => "full",
"matchedWords" => ["john"],
"value" => "Pierre <em>Jon</em>es"
}
},
"age" => 69,
"first_name" => "Pierre",
"full_name" => "Pierre Jones",
"indexed_at" => 1570908223,
"last_name" => "Jones",
"objectID" => "b563deb6-2a06-4428-8e5a-ca1ecc08f4e2"
},
%{
"_highlightResult" => %{
"full_name" => %{
"fullyHighlighted" => false,
"matchLevel" => "full",
"matchedWords" => ["john"],
"value" => "Glennie <em>Jon</em>es"
}
},
"age" => 27,
"first_name" => "Glennie",
"full_name" => "Glennie Jones",
"indexed_at" => 1570908223,
"last_name" => "Jones",
"objectID" => "58e8ff8d-2794-41e1-a4ef-6f8db8d432b6"
},
...
],
"hitsPerPage" => 20,
"nbHits" => 16,
"nbPages" => 1,
"page" => 0,
"params" => "query=john",
"processingTimeMS" => 1,
"query" => "john"
}}
Search for facet values
Example
iex> People.search_facet("age")
{:ok,
%{
"exhaustiveFacetsCount" => true,
"facetHits" => [
%{"count" => 22, "highlighted" => "46", "value" => "46"},
%{"count" => 21, "highlighted" => "38", "value" => "38"},
%{"count" => 19, "highlighted" => "54", "value" => "54"},
%{"count" => 19, "highlighted" => "99", "value" => "99"},
%{"count" => 18, "highlighted" => "36", "value" => "36"},
%{"count" => 18, "highlighted" => "45", "value" => "45"},
%{"count" => 18, "highlighted" => "52", "value" => "52"},
%{"count" => 18, "highlighted" => "56", "value" => "56"},
%{"count" => 18, "highlighted" => "59", "value" => "59"},
%{"count" => 18, "highlighted" => "86", "value" => "86"}
],
"processingTimeMS" => 1
}}
to_be_indexed?(model)
View Sourceto_be_indexed?(model :: map()) :: true | false
Check if current object must be indexed or not. By default it's always true. To override this behaviour overide this function in your model
Example
defmodule People do
use Algoliax,
index_name: :people,
attribute_for_faceting: ["age"],
custom_ranking: ["desc(update_at)"],
object_id: :reference
#....
def to_be_indexed?(model) do
model.age > 50
end
end