From d9bb4d700fb93a71ef632fe9f6980b45fdda7d29 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 17 Nov 2019 16:30:20 -0500 Subject: [PATCH] add comment search --- lib/philomena/comments/elasticsearch.ex | 3 - .../controllers/comment_controller.ex | 55 +++++++++++++++++-- .../templates/comment/index.html.slime | 41 +++++++++++--- .../pagination/_pagination.html.slime | 2 +- 4 files changed, 83 insertions(+), 18 deletions(-) diff --git a/lib/philomena/comments/elasticsearch.ex b/lib/philomena/comments/elasticsearch.ex index 2c6dad43..0d05869d 100644 --- a/lib/philomena/comments/elasticsearch.ex +++ b/lib/philomena/comments/elasticsearch.ex @@ -29,9 +29,6 @@ defmodule Philomena.Comments.Elasticsearch do } end - # preload([ - # :user, image: :tags - # ]) def as_json(comment) do %{ id: comment.id, diff --git a/lib/philomena_web/controllers/comment_controller.ex b/lib/philomena_web/controllers/comment_controller.ex index 0d14ce6c..25ecabde 100644 --- a/lib/philomena_web/controllers/comment_controller.ex +++ b/lib/philomena_web/controllers/comment_controller.ex @@ -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 diff --git a/lib/philomena_web/templates/comment/index.html.slime b/lib/philomena_web/templates/comment/index.html.slime index 13968f55..4fe8be5c 100644 --- a/lib/philomena_web/templates/comment/index.html.slime +++ b/lib/philomena_web/templates/comment/index.html.slime @@ -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 - .block__header - = pagination +.column-layout + .column-layout__left + .block + .block__content + h3 Search Comments -= for {comment, body} <- @comments do - = render PhilomenaWeb.CommentView, "_comment_with_image.html", image: comment.image, comment: comment, body: body, conn: @conn + = 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)" -.block - .block__header.block__header--light - = pagination \ No newline at end of file + .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 + = render PhilomenaWeb.CommentView, "_comment_with_image.html", image: comment.image, comment: comment, body: body, conn: @conn + + .block + .block__header.block__header--light + = pagination \ No newline at end of file diff --git a/lib/philomena_web/templates/pagination/_pagination.html.slime b/lib/philomena_web/templates/pagination/_pagination.html.slime index 53741ae8..69234a6a 100644 --- a/lib/philomena_web/templates/pagination/_pagination.html.slime +++ b/lib/philomena_web/templates/pagination/_pagination.html.slime @@ -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)) \ No newline at end of file + /= link("Last »", to: last_page_path(@page, @route, params)) \ No newline at end of file