-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Search): Initial Algolia implementation
- Loading branch information
Showing
20 changed files
with
207 additions
and
9 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
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,21 @@ | ||
defmodule CF.Algolia.SpeakersIndex do | ||
use Algoliax.Indexer, | ||
index_name: :get_index_name, | ||
repo: DB.Repo, | ||
schemas: [DB.Schema.Speaker] | ||
|
||
@doc """ | ||
## Examples | ||
iex> CF.Algolia.SpeakersIndex.get_index_name() | ||
:test_speakers | ||
""" | ||
def get_index_name do | ||
String.to_atom("#{Application.get_env(:cf, :deploy_env)}_speakers") | ||
end | ||
|
||
@impl Algoliax.Indexer | ||
def build_object(speaker) do | ||
Map.take(speaker, ~w(id full_name title slug country wikidata_item_id)a) | ||
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,54 @@ | ||
defmodule CF.Algolia.StatementsIndex do | ||
import Ecto.Query | ||
|
||
use Algoliax.Indexer, | ||
index_name: :get_index_name, | ||
repo: DB.Repo, | ||
schemas: [DB.Schema.Statement] | ||
|
||
@doc """ | ||
## Examples | ||
iex> CF.Algolia.StatementsIndex.get_index_name() | ||
:test_statements | ||
""" | ||
def get_index_name do | ||
String.to_atom("#{Application.get_env(:cf, :deploy_env)}_statements") | ||
end | ||
|
||
@doc """ | ||
## Examples | ||
iex> CF.Algolia.StatementsIndex.to_be_indexed?(%DB.Schema.Statement{is_removed: true}) | ||
false | ||
iex> CF.Algolia.StatementsIndex.to_be_indexed?(%DB.Schema.Statement{is_removed: false}) | ||
true | ||
""" | ||
@impl Algoliax.Indexer | ||
def to_be_indexed?(statement) do | ||
not statement.is_removed | ||
end | ||
|
||
@impl Algoliax.Indexer | ||
def build_object(statement) do | ||
statement | ||
|> DB.Repo.preload([:video, :speaker]) | ||
|> Map.update!(:video, &build_video(&1)) | ||
|> Map.update!(:speaker, &build_speaker(&1)) | ||
|> Map.take(~w(id text time video speaker)a) | ||
end | ||
|
||
def reindex_all_speaker_statements(speaker_id) do | ||
DB.Schema.Statement | ||
|> where([s], s.speaker_id == ^speaker_id) | ||
|> DB.Repo.all() | ||
|> save_objects() | ||
end | ||
|
||
defp build_video(video) do | ||
Map.take(video, ~w(id title hash_id youtube_id facebook_id url)a) | ||
end | ||
|
||
defp build_speaker(nil), do: nil | ||
defp build_speaker(speaker), do: CF.Algolia.SpeakersIndex.build_object(speaker) | ||
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,63 @@ | ||
defmodule CF.Algolia.VideosIndex do | ||
import Ecto.Query | ||
|
||
use Algoliax.Indexer, | ||
index_name: :get_index_name, | ||
repo: DB.Repo, | ||
schemas: [ | ||
{DB.Schema.Video, [:speakers]} | ||
] | ||
|
||
@doc """ | ||
## Examples | ||
iex> CF.Algolia.VideosIndex.get_index_name() | ||
:test_videos | ||
""" | ||
def get_index_name do | ||
String.to_atom("#{Application.get_env(:cf, :deploy_env)}_videos") | ||
end | ||
|
||
@doc """ | ||
## Examples | ||
iex> CF.Algolia.VideosIndex.to_be_indexed?(%DB.Schema.Video{unlisted: true}) | ||
false | ||
iex> CF.Algolia.VideosIndex.to_be_indexed?(%DB.Schema.Video{unlisted: false}) | ||
true | ||
""" | ||
@impl Algoliax.Indexer | ||
def to_be_indexed?(video) do | ||
not video.unlisted | ||
end | ||
|
||
@impl Algoliax.Indexer | ||
def build_object(video) do | ||
video | ||
|> DB.Repo.preload(:speakers) | ||
|> Map.update!(:speakers, &update_all_speakers/1) | ||
|> Map.take( | ||
~w(id title hash_id url language is_partner thumbnail youtube_id facebook_id youtube_offset speakers)a | ||
) | ||
end | ||
|
||
def reindex_by_id(video_id) do | ||
DB.Schema.Video | ||
|> preload(:speakers) | ||
|> DB.Repo.get(video_id) | ||
|> save_object() | ||
end | ||
|
||
def reindex_all_speaker_videos(speaker_id) do | ||
DB.Schema.Video | ||
|> join(:inner, [v], s in assoc(v, :speakers)) | ||
|> where([v, s], s.id == ^speaker_id) | ||
|> preload([v, s], speakers: s) | ||
|> DB.Repo.all() | ||
|> save_objects() | ||
end | ||
|
||
defp update_all_speakers(speakers) do | ||
Enum.map(speakers, &CF.Algolia.SpeakersIndex.build_object/1) | ||
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
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,4 @@ | ||
defmodule CF.Algolia.SpeakersIndexTest do | ||
use CF.DataCase | ||
doctest CF.Algolia.SpeakersIndex | ||
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,4 @@ | ||
defmodule CF.Algolia.StatementsIndexTest do | ||
use CF.DataCase | ||
doctest CF.Algolia.StatementsIndex | ||
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,4 @@ | ||
defmodule CF.Algolia.VideosIndexTest do | ||
use CF.DataCase | ||
doctest CF.Algolia.VideosIndex | ||
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
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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use Mix.Config | ||
|
||
import_config "../apps/*/config/config.exs" | ||
import_config "./*.secret.exs" # TODO should filter by env |
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.