All Projects → WTTJ → algoliax

WTTJ / algoliax

Licence: BSD-2-Clause license
Algolia integration to elixir application

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to algoliax

Paper trail
Track and record all the changes in your database with Ecto. Revert back to anytime in history.
Stars: ✭ 380 (+900%)
Mutual labels:  phoenix, ecto
Ecto morph
morph your Ecto capabilities into the s t r a t o s p h e r e !
Stars: ✭ 72 (+89.47%)
Mutual labels:  phoenix, ecto
Kaffy
Powerfully simple admin package for phoenix applications
Stars: ✭ 617 (+1523.68%)
Mutual labels:  phoenix, ecto
one plus n detector
Elixir library to help you detect 1+n queries in applications using Ecto
Stars: ✭ 20 (-47.37%)
Mutual labels:  phoenix, ecto
Mipha
Proj Elixir Forum build with phoenix 1.5.
Stars: ✭ 153 (+302.63%)
Mutual labels:  phoenix, ecto
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+865.79%)
Mutual labels:  phoenix, ecto
Wallaby
Concurrent browser tests with elixir
Stars: ✭ 1,143 (+2907.89%)
Mutual labels:  phoenix, ecto
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (-34.21%)
Mutual labels:  phoenix, ecto
Phoenix live dashboard
Realtime dashboard with metrics, request logging, plus storage, OS and VM insights
Stars: ✭ 1,657 (+4260.53%)
Mutual labels:  phoenix, ecto
Filterable
Filtering from incoming params in Elixir/Ecto/Phoenix with easy to use DSL.
Stars: ✭ 83 (+118.42%)
Mutual labels:  phoenix, ecto
ecto generator
Generate Ecto schemas from existing database in Phoenix - Elixir
Stars: ✭ 20 (-47.37%)
Mutual labels:  phoenix, ecto
Formex
A better form library for Phoenix
Stars: ✭ 206 (+442.11%)
Mutual labels:  phoenix, ecto
pretty print formatter
Pretty Print Formatter for Elixir Logger module -- Colorize Ecto's SQL ouput 🖌️
Stars: ✭ 22 (-42.11%)
Mutual labels:  phoenix, ecto
query builder
Compose Ecto queries without effort
Stars: ✭ 56 (+47.37%)
Mutual labels:  phoenix, ecto
ex sieve
Implement dynamic filtering and sorting API for Ecto queries
Stars: ✭ 37 (-2.63%)
Mutual labels:  phoenix, ecto
Query
Query adds tools to aid the use of Ecto in web settings.
Stars: ✭ 23 (-39.47%)
Mutual labels:  phoenix, ecto
contextual
🌈 Generate your Ecto contexts using this macro and eliminate boilerplate
Stars: ✭ 18 (-52.63%)
Mutual labels:  phoenix, ecto
ecto nested changeset
Helpers for manipulating nested Ecto changesets
Stars: ✭ 23 (-39.47%)
Mutual labels:  phoenix, ecto
Polymorphic embed
Polymorphic embeds in Ecto
Stars: ✭ 80 (+110.53%)
Mutual labels:  phoenix, ecto
Phoenix Ecto Encryption Example
🔐 A detailed example for how to encrypt data in a Phoenix (Elixir) App before inserting into a database using Ecto Types
Stars: ✭ 166 (+336.84%)
Mutual labels:  phoenix, ecto

Algoliax

CircleCI Module Version Hex Docs Total Download License Last Updated

This package let you easily integrate Algolia to your Elixir application. It can be used with built in Elixir struct or with Ecto schemas.

Installation

The package can be installed by adding :algoliax to your list of dependencies in mix.exs:

def deps do
  [
    {:algoliax, "~> 0.7.0"}
  ]
end

If using with Ecto schemas, Algoliax requires :ecto.

Configuration

Algoliax needs only :api_key and :application_id config. These configs can either be on config files or using environment variables ALGOLIA_API_KEY and ALGOLIA_APPLICATION_ID.

config :algoliax,
  api_key: "<API_KEY>",
  application_id: "<APPLICATION_ID>",
  batch_size: 500,
  recv_timeout: 5000

Usage

defmodule People do
  use Algoliax.Indexer,
    index_name: :algoliax_people,
    object_id: :reference,
    algolia: [
      attributes_for_faceting: ["age"],
      searchable_attributes: ["full_name"],
      custom_ranking: ["desc(updated_at)"]
    ]

  defstruct reference: nil, last_name: nil, first_name: nil, age: nil
end

Overridable functions:

  • to_be_indexed/1 which take the model struct in parameter: allows to choose to index or not the current model
defmodule People do
  ...

  @impl Algoliax
  def to_be_indexed?(person) do
    person.age > 20
  end
end

# This object will be indexed
people1 = %People{reference: 10, last_name: "Doe", first_name: "John", age: 13}

# This object will not be indexed
people2 = %People{reference: 87, last_name: "Fred", first_name: "Al", age: 70}
  • build_object/1 which take the model struct/map in parameter and should return a Map: allow to add attributes to the indexed object. By default the object contains only an ObjectID.
defmodule People do
  ...

  @impl Algoliax
  def build_object(person) do
    %{
      age: person.age,
      now: Date.utc_today()
    }
  end
end

Index name at runtime

It's possible to define an index name at runtime, useful if index_name depends on environment or comes from an environment variable.

To do this just define a function with an arity of 0 that will be used as index_name

defmodule People do
  use Algoliax.Indexer,
    index_name: :algoliax_people,
    object_id: :reference,
    algolia: [...]

  def algoliax_people do
    System.get_env("PEOPLE_INDEX_NAME")
  end
end

Index functions

# Get people index settings
People.get_settings()

# Delete index
People.delete_index()

# Configure index
People.configure_index()

Object functions

# Save object
People.save_object(people1)

# Save multiple objects
People.save_objects([people1, people2])

# Save multiple objects, and ensure object that they can't be indexed anymore are deleted from the index
People.save_objects([people1, people2], force_delete: true)

# Get object
People.get_object(people1)

# Delete object
People.delete_object(people1)

Search functions

# search in index
People.search("john")

# search facet
People.search_facet("age")

Ecto specific

First you will need to add the Repo to the algoliax config:

use Algoliax.Indexer,
  index_name: :algoliax_people,
  object_id: :reference
  repo: MyApp.Repo,
  algolia: [...]

If using Agoliax with an Ecto schema it is possible to use reindex functions. Reindex will go through all entries in the corresponding table (or part if query is provided). Algoliax will save_objects by batch of 500. batch_size can be configured

config :algoliax,
  batch_size: 250

NOTE: Algoliax use by default the id column to order and go through the table. (cf Custom order column)

import Ecto.Query

# Reindex all
People.reindex()

# Reindex all people with age greater than 20
query = from(p in People, where: p.age > 20)
People.reindex(query)

# Reindex can also `force_delete`
query = from(p in People, where: p.age > 20)
People.reindex(query, force_delete: true)
People.reindex(force_delete: true)

# Reindex atomically (create a temporary index and move it to initial index)
People.reindex_atomic()
Custom cursor column

If you don't have an id column, you can change it by setting the cursor_field option either in the global settings or in schema specific settings.

Make sure this column ensure a consistent order even when new records are created.

  • Using the global config:
config :algoliax,
  batch_size: 250,
  cursor_field: :reference
  • Indexer specific:
defmodule People do
  use Algoliax.Indexer,
    index_name: :algoliax_people,
    object_id: :reference,
    repo: MyApp.Repo,
    cursor_field: :inserted_at,
    algolia: [...]
end

Replicas configuration

Replicas can be configured using :replicas options. This option accepts the following :index_name, :algolia and :inherit. Use inherit: true on the replica if you want it to inherit from the primary settings, if custom settings in :algolia they will be merged.

use Algoliax.Indexer,
  index_name: :algoliax_people,
  object_id: :reference,
  repo: MyApp.Repo,
  algolia: [
    attributes_for_faceting: ["age"],
    searchable_attributes: ["full_name"],
  ],
  replicas: [
    [index_name: :algoliax_by_age_asc, inherit: true, algolia: [ranking: ["asc(age)"]]],
    [index_name: :algoliax_by_age_desc, inherit: false, algolia: [ranking: ["desc(age)"]]]
  ]

Copyright and License

Copyright (c) 2020 CORUSCANT (welcome to the jungle) - https://www.welcometothejungle.com

This library is licensed under the BSD-2-Clause.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].