parser: don't downcase some fields (#209)

* parser: don't downcase some fields

* flip condition
This commit is contained in:
mdashlw 2024-03-18 20:25:59 +03:00 committed by GitHub
parent 3590be1429
commit 89aa180c8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 9 deletions

View file

@ -82,7 +82,8 @@ defmodule Philomena.Images.Query do
aliases: %{ aliases: %{
"faved_by" => "favourited_by_users", "faved_by" => "favourited_by_users",
"faved_by_id" => "favourited_by_user_ids" "faved_by_id" => "favourited_by_user_ids"
} },
no_downcase_fields: ~W(file_name)
] ]
end end

View file

@ -24,6 +24,7 @@ defmodule Philomena.Search.Parser do
custom_fields: [], custom_fields: [],
transforms: %{}, transforms: %{},
aliases: %{}, aliases: %{},
no_downcase_fields: [],
__fields__: %{}, __fields__: %{},
__data__: nil __data__: nil
] ]
@ -193,14 +194,19 @@ defmodule Philomena.Search.Parser do
# Types which do not support ranges # Types which do not support ranges
defp field_type(parser, [{LiteralParser, field_name}, range: :eq, literal: value]), 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]), defp field_type(parser, [{LiteralParser, field_name}, range: :eq, literal: value, fuzz: fuzz]),
do: do:
{:ok, {:ok,
{%{ {%{
fuzzy: %{ 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: %{}}, []}} do: {:ok, {%{match_all: %{}}, []}}
defp field_type(parser, [{LiteralParser, field_name}, range: :eq, wildcard: value]), 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]), defp field_type(parser, [{NgramParser, field_name}, range: :eq, literal: value]),
do: 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]), defp field_type(parser, [{NgramParser, field_name}, range: :eq, literal: value, fuzz: _fuzz]),
do: 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: "*"]), defp field_type(_parser, [{NgramParser, _field_name}, range: :eq, wildcard: "*"]),
do: {:ok, {%{match_all: %{}}, []}} do: {:ok, {%{match_all: %{}}, []}}
defp field_type(parser, [{NgramParser, field_name}, range: :eq, wildcard: value]), 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]), defp field_type(parser, [{BoolParser, field_name}, range: :eq, bool: value]),
do: {:ok, {%{term: %{field(parser, field_name) => value}}, []}} do: {:ok, {%{term: %{field(parser, field_name) => value}}, []}}
@ -271,10 +289,18 @@ defmodule Philomena.Search.Parser do
parser.aliases[field_name] || field_name parser.aliases[field_name] || field_name
end end
defp normalize_value(_parser, value) do defp normalize_value(parser, field_name, value) do
value value
|> String.trim() |> 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 end
# Flattens the child of a disjunction or conjunction to improve performance. # Flattens the child of a disjunction or conjunction to improve performance.