From 128485c30a341446e5bc49d5802fadb99df297c5 Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 7 Mar 2024 18:42:20 -0800 Subject: [PATCH] Resolved bug where null values in search terms would break search (#57) --- lib/pinchflat/media.ex | 4 ++-- test/pinchflat/media_test.exs | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/pinchflat/media.ex b/lib/pinchflat/media.ex index db304c1..3f2aed1 100644 --- a/lib/pinchflat/media.ex +++ b/lib/pinchflat/media.ex @@ -105,9 +105,9 @@ defmodule Pinchflat.Media do select_merge: %{ matching_search_term: fragment(""" - snippet(media_items_search_index, 0, '[PF_HIGHLIGHT]', '[/PF_HIGHLIGHT]', '...', 20) || + coalesce(snippet(media_items_search_index, 0, '[PF_HIGHLIGHT]', '[/PF_HIGHLIGHT]', '...', 20), '') || ' ' || - snippet(media_items_search_index, 1, '[PF_HIGHLIGHT]', '[/PF_HIGHLIGHT]', '...', 20) + coalesce(snippet(media_items_search_index, 1, '[PF_HIGHLIGHT]', '[/PF_HIGHLIGHT]', '...', 20), '') """) }, order_by: [desc: fragment("rank")], diff --git a/test/pinchflat/media_test.exs b/test/pinchflat/media_test.exs index fc5521f..a4692b4 100644 --- a/test/pinchflat/media_test.exs +++ b/test/pinchflat/media_test.exs @@ -267,6 +267,13 @@ defmodule Pinchflat.MediaTest do assert String.contains?(res.matching_search_term, "The [PF_HIGHLIGHT]quick[/PF_HIGHLIGHT] brown fox") end + test "doesn't set matching_search_term to nil if one of the attributes we search on is null" do + media_item_fixture(%{title: "foobar baz", description: nil}) + + assert [%{matching_search_term: matching_search_term}] = Media.search("baz") + refute is_nil(matching_search_term) + end + test "optionally lets you specify a limit" do media_item_fixture(%{title: "The small gray dog"})