mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
add comment search
This commit is contained in:
parent
850a0d758e
commit
d9bb4d700f
4 changed files with 83 additions and 18 deletions
|
@ -29,9 +29,6 @@ defmodule Philomena.Comments.Elasticsearch do
|
|||
}
|
||||
end
|
||||
|
||||
# preload([
|
||||
# :user, image: :tags
|
||||
# ])
|
||||
def as_json(comment) do
|
||||
%{
|
||||
id: comment.id,
|
||||
|
|
|
@ -4,19 +4,16 @@ defmodule PhilomenaWeb.CommentController do
|
|||
alias Philomena.{Comments.Comment, Textile.Renderer}
|
||||
import Ecto.Query
|
||||
|
||||
def index(conn, _params) do
|
||||
def index(conn, params) do
|
||||
comments =
|
||||
Comment.search_records(
|
||||
%{
|
||||
query: %{
|
||||
bool: %{
|
||||
must: [
|
||||
%{range: %{posted_at: %{gt: "now-1w"}}},
|
||||
%{term: %{hidden_from_users: false}}
|
||||
]
|
||||
must: parse_search(params) ++ [%{term: %{hidden_from_users: false}}]
|
||||
}
|
||||
},
|
||||
sort: %{posted_at: :desc}
|
||||
sort: parse_sort(params)
|
||||
},
|
||||
conn.assigns.pagination,
|
||||
Comment |> preload([image: [:tags], user: [awards: :badge]])
|
||||
|
@ -31,4 +28,50 @@ defmodule PhilomenaWeb.CommentController do
|
|||
|
||||
render(conn, "index.html", comments: comments)
|
||||
end
|
||||
|
||||
defp parse_search(%{"comment" => comment_params}) do
|
||||
parse_author(comment_params) ++
|
||||
parse_image_id(comment_params) ++
|
||||
parse_body(comment_params)
|
||||
end
|
||||
defp parse_search(_params), do: [%{match_all: %{}}]
|
||||
|
||||
defp parse_author(%{"author" => author}) when author not in [nil, ""] do
|
||||
case String.contains?(author, ["*", "?"]) do
|
||||
true ->
|
||||
[
|
||||
%{wildcard: %{author: author}},
|
||||
%{term: %{anonymous: false}}
|
||||
]
|
||||
|
||||
false ->
|
||||
[
|
||||
%{term: %{author: author}},
|
||||
%{term: %{anonymous: false}}
|
||||
]
|
||||
end
|
||||
end
|
||||
defp parse_author(_params), do: []
|
||||
|
||||
defp parse_image_id(%{"image_id" => image_id}) when image_id not in [nil, ""] do
|
||||
case Integer.parse(image_id) do
|
||||
{image_id, _rest} ->
|
||||
[%{term: %{image_id: image_id}}]
|
||||
|
||||
_error ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
defp parse_image_id(_params), do: []
|
||||
|
||||
defp parse_body(%{"body" => body}) when body not in [nil, ""],
|
||||
do: [%{match: %{body: body}}]
|
||||
defp parse_body(_params), do: []
|
||||
|
||||
defp parse_sort(%{"comment" => %{"sf" => sf, "sd" => sd}}) when sf in ["posted_at", "_score"] and sd in ["desc", "asc"] do
|
||||
%{sf => sd}
|
||||
end
|
||||
defp parse_sort(_params) do
|
||||
%{posted_at: :desc}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,13 +2,38 @@ elixir:
|
|||
route = fn p -> Routes.comment_path(@conn, :index, p) end
|
||||
pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @comments, route: route
|
||||
|
||||
.block
|
||||
.column-layout
|
||||
.column-layout__left
|
||||
.block
|
||||
.block__content
|
||||
h3 Search Comments
|
||||
|
||||
= form_for @conn, Routes.comment_path(@conn, :index), [as: :comment, method: "get", class: "hform"], fn f ->
|
||||
.field = label f, :author, "Author"
|
||||
.field = text_input f, :author, class: "input hform__text", placeholder: "Author (* is wildcard)"
|
||||
|
||||
.field = label f, :image_id, "Image ID"
|
||||
.field = number_input f, :image_id, class: "input hform__text", placeholder: "Image ID"
|
||||
|
||||
.field = label f, :body, "Body"
|
||||
.field = textarea f, :body, class: "input input--wide", placeholder: "Body"
|
||||
|
||||
.field = label f, :sf, "Sort by"
|
||||
.field
|
||||
=> select f, :sf, ["Creation Date": "posted_at", "Relevance": "_score"], class: "input"
|
||||
=> select f, :sd, ["Descending": "desc", "Ascending": "asc"], class: "input"
|
||||
|
||||
.field
|
||||
= submit "Search", class: "button button--state-primary"
|
||||
|
||||
.column-layout__main
|
||||
.block
|
||||
.block__header
|
||||
= pagination
|
||||
|
||||
= for {comment, body} <- @comments do
|
||||
= for {comment, body} <- @comments do
|
||||
= render PhilomenaWeb.CommentView, "_comment_with_image.html", image: comment.image, comment: comment, body: body, conn: @conn
|
||||
|
||||
.block
|
||||
.block
|
||||
.block__header.block__header--light
|
||||
= pagination
|
|
@ -24,4 +24,4 @@
|
|||
|
||||
= if not last_page?(@page) do
|
||||
= link("Next ›", to: next_page_path(@page, @route, params))
|
||||
= link("Last »", to: last_page_path(@page, @route, params))
|
||||
/= link("Last »", to: last_page_path(@page, @route, params))
|
Loading…
Reference in a new issue