From 89aa180c8e51cdd4d8217bfda972affdc94efb25 Mon Sep 17 00:00:00 2001 From: mdashlw Date: Mon, 18 Mar 2024 20:25:59 +0300 Subject: [PATCH] parser: don't downcase some fields (#209) * parser: don't downcase some fields * flip condition --- lib/philomena/images/query.ex | 3 ++- lib/philomena/search/parser.ex | 42 +++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index d9f6346e3..815515803 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -82,7 +82,8 @@ defmodule Philomena.Images.Query do aliases: %{ "faved_by" => "favourited_by_users", "faved_by_id" => "favourited_by_user_ids" - } + }, + no_downcase_fields: ~W(file_name) ] end diff --git a/lib/philomena/search/parser.ex b/lib/philomena/search/parser.ex index f89290eeb..aaba8df7f 100644 --- a/lib/philomena/search/parser.ex +++ b/lib/philomena/search/parser.ex @@ -24,6 +24,7 @@ defmodule Philomena.Search.Parser do custom_fields: [], transforms: %{}, aliases: %{}, + no_downcase_fields: [], __fields__: %{}, __data__: nil ] @@ -193,14 +194,19 @@ defmodule Philomena.Search.Parser do # Types which do not support ranges defp field_type(parser, [{LiteralParser, field_name}, range: :eq, literal: value]), - do: {:ok, {%{term: %{field(parser, field_name) => normalize_value(parser, value)}}, []}} + do: + {:ok, + {%{term: %{field(parser, field_name) => normalize_value(parser, field_name, value)}}, []}} defp field_type(parser, [{LiteralParser, field_name}, range: :eq, literal: value, fuzz: fuzz]), do: {:ok, {%{ fuzzy: %{ - field(parser, field_name) => %{value: normalize_value(parser, value), fuzziness: fuzz} + field(parser, field_name) => %{ + value: normalize_value(parser, field_name, value), + fuzziness: fuzz + } } }, []}} @@ -208,21 +214,33 @@ defmodule Philomena.Search.Parser do do: {:ok, {%{match_all: %{}}, []}} defp field_type(parser, [{LiteralParser, field_name}, range: :eq, wildcard: value]), - do: {:ok, {%{wildcard: %{field(parser, field_name) => normalize_value(parser, value)}}, []}} + do: + {:ok, + {%{wildcard: %{field(parser, field_name) => normalize_value(parser, field_name, value)}}, + []}} defp field_type(parser, [{NgramParser, field_name}, range: :eq, literal: value]), do: - {:ok, {%{match_phrase: %{field(parser, field_name) => normalize_value(parser, value)}}, []}} + {:ok, + {%{ + match_phrase: %{field(parser, field_name) => normalize_value(parser, field_name, value)} + }, []}} defp field_type(parser, [{NgramParser, field_name}, range: :eq, literal: value, fuzz: _fuzz]), do: - {:ok, {%{match_phrase: %{field(parser, field_name) => normalize_value(parser, value)}}, []}} + {:ok, + {%{ + match_phrase: %{field(parser, field_name) => normalize_value(parser, field_name, value)} + }, []}} defp field_type(_parser, [{NgramParser, _field_name}, range: :eq, wildcard: "*"]), do: {:ok, {%{match_all: %{}}, []}} defp field_type(parser, [{NgramParser, field_name}, range: :eq, wildcard: value]), - do: {:ok, {%{wildcard: %{field(parser, field_name) => normalize_value(parser, value)}}, []}} + do: + {:ok, + {%{wildcard: %{field(parser, field_name) => normalize_value(parser, field_name, value)}}, + []}} defp field_type(parser, [{BoolParser, field_name}, range: :eq, bool: value]), do: {:ok, {%{term: %{field(parser, field_name) => value}}, []}} @@ -271,10 +289,18 @@ defmodule Philomena.Search.Parser do parser.aliases[field_name] || field_name end - defp normalize_value(_parser, value) do + defp normalize_value(parser, field_name, value) do value |> String.trim() - |> String.downcase() + |> maybe_downcase(parser, field_name) + end + + defp maybe_downcase(value, parser, field_name) do + if Enum.member?(parser.no_downcase_fields, field_name) do + value + else + String.downcase(value) + end end # Flattens the child of a disjunction or conjunction to improve performance.