Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Mar 19, 2024
2 parents 0d2a867 + 89aa180 commit 75aea3e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/philomena/images/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 34 additions & 8 deletions lib/philomena/search/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Philomena.Search.Parser do
custom_fields: [],
transforms: %{},
aliases: %{},
no_downcase_fields: [],
__fields__: %{},
__data__: nil
]
Expand Down Expand Up @@ -193,36 +194,53 @@ 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
}
}
}, []}}

defp field_type(_parser, [{LiteralParser, _field_name}, range: :eq, wildcard: "*"]),
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}}, []}}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 75aea3e

Please sign in to comment.