Search v1 (#19)

* Adds description to media items; hooks it up to indexing/media downloading

* Added a search method using postgres fulltext search

* Hooked up search functionality to the search form

* Added persistence to the search form when on search page
This commit is contained in:
Kieran authored and GitHub committed 2024-02-13 17:46:14 -08:00
1 parent 850cf2db73
commit 58771ee37b
25 files changed
+261 -25

No files matched your search

+34
View File
@@ -47,6 +47,40 @@ defmodule Pinchflat.Media do
|> Repo.all()
end
@doc """
Returns a list of media_items that match the search term. Adds a `matching_search_term`
virtual field to the result set.
Returns [%MediaItem{}, ...].
"""
def search(search_term, opts \\ []) do
limit = Keyword.get(opts, :limit, 50)
from(mi in MediaItem,
where: fragment("searchable @@ websearch_to_tsquery(?)", ^search_term),
select_merge: %{
matching_search_term:
fragment(
"""
ts_headline(
'english',
CONCAT(title, ' ', description),
websearch_to_tsquery(?),
'StartSel=[PF_HIGHLIGHT],StopSel=[/PF_HIGHLIGHT]'
)
""",
^search_term
)
},
order_by: {
:desc,
fragment("ts_rank_cd(searchable, websearch_to_tsquery(?), 0)", ^search_term)
},
limit: ^limit
)
|> Repo.all()
end
@doc """
Gets a single media_item.