diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index d84f135a..e2a2cfc8 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -1,7 +1,7 @@ defmodule Philomena.Images.Query do import Philomena.Search.Parser - defparser "anonymous", + defparser("anonymous", int: ~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count), float: ~W(aspect_ratio wilson_score), @@ -19,8 +19,9 @@ defmodule Philomena.Images.Query do "faved_by_id" => "favourited_by_user_ids" }, default: "namespaced_tags.name" + ) - defparser "user", + defparser("user", int: ~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count), float: ~W(aspect_ratio wilson_score), @@ -33,11 +34,18 @@ defmodule Philomena.Images.Query do %{nested: %{path: :galleries, query: %{term: %{"galleries.id" => value}}}} end, "my" => fn - %{user: %{id: id}}, "faves" -> %{term: %{favourited_by_user_ids: id}} - %{user: %{id: id}}, "upvotes" -> %{term: %{upvoter_ids: id}} - %{user: %{id: id}}, "downvotes" -> %{term: %{downvoter_ids: id}} + %{user: %{id: id}}, "faves" -> + %{term: %{favourited_by_user_ids: id}} + + %{user: %{id: id}}, "upvotes" -> + %{term: %{upvoter_ids: id}} + + %{user: %{id: id}}, "downvotes" -> + %{term: %{downvoter_ids: id}} + %{user: _u}, "watched" -> - %{query: %{match_all: %{}}} # todo + # todo + %{query: %{match_all: %{}}} end }, aliases: %{ @@ -45,13 +53,15 @@ defmodule Philomena.Images.Query do "faved_by_id" => "favourited_by_user_ids" }, default: "namespaced_tags.name" + ) - defparser "moderator", + defparser("moderator", int: ~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id upvoted_by_id downvoted_by_id tag_count true_uploader_id hidden_by_id deleted_by_user-id), float: ~W(aspect_ratio wilson_score), date: ~W(created_at updated_at first_seen_at), - literal: ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format fingerprint upvoted_by downvoted_by true_uploader hidden_by deleted_by_user), + literal: + ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format fingerprint upvoted_by downvoted_by true_uploader hidden_by deleted_by_user), ngram: ~W(description deletion_reason), ip: ~W(ip), bool: ~W(deleted), @@ -61,23 +71,31 @@ defmodule Philomena.Images.Query do %{nested: %{path: :galleries, query: %{term: %{"galleries.id" => value}}}} end, "my" => fn - %{user: %{id: id}}, "faves" -> %{term: %{favourited_by_user_ids: id}} - %{user: %{id: id}}, "upvotes" -> %{term: %{upvoter_ids: id}} - %{user: %{id: id}}, "downvotes" -> %{term: %{downvoter_ids: id}} + %{user: %{id: id}}, "faves" -> + %{term: %{favourited_by_user_ids: id}} + + %{user: %{id: id}}, "upvotes" -> + %{term: %{upvoter_ids: id}} + + %{user: %{id: id}}, "downvotes" -> + %{term: %{downvoter_ids: id}} + %{user: _u}, "watched" -> - %{query: %{match_all: %{}}} # todo + # todo + %{query: %{match_all: %{}}} end }, aliases: %{ - "faved_by" => "favourited_by_users", - "upvoted_by" => "upvoters", - "downvoted_by" => "downvoters", - "faved_by_id" => "favourited_by_user_ids", - "upvoted_by_id" => "upvoter_ids", + "faved_by" => "favourited_by_users", + "upvoted_by" => "upvoters", + "downvoted_by" => "downvoters", + "faved_by_id" => "favourited_by_user_ids", + "upvoted_by_id" => "upvoter_ids", "downvoted_by_id" => "downvoter_ids", - "hidden_by" => "hidden_by_users", - "hidden_by_id" => "hidden_by_user_ids", - "deleted" => "hidden_from_users" + "hidden_by" => "hidden_by_users", + "hidden_by_id" => "hidden_by_user_ids", + "deleted" => "hidden_from_users" }, default: "namespaced_tags.name" + ) end diff --git a/lib/philomena/search/parser.ex b/lib/philomena/search/parser.ex index 57e7eb3d..9467c6ab 100644 --- a/lib/philomena/search/parser.ex +++ b/lib/philomena/search/parser.ex @@ -8,7 +8,7 @@ defmodule Philomena.Search.Parser do import Philomena.Search.Lexer import Philomena.Search.Helpers - deflexer unquote(name), unquote(opts) + deflexer(unquote(name), unquote(opts)) def unquote(:"#{name}_parser")(ctx, input) do with {:ok, tree, _1, _2, _3, _4} <- unquote(:"#{name}_lexer")(input) do @@ -23,12 +23,12 @@ defmodule Philomena.Search.Parser do {tree, []} = unquote(:"#{name}_top")(ctx, tokens) {:ok, tree} - #rescue - # e in ArgumentError -> - # {:error, e.message} + # rescue + # e in ArgumentError -> + # {:error, e.message} - # _ -> - # {:error, "Parsing error."} + # _ -> + # {:error, "Parsing error."} end # @@ -120,7 +120,12 @@ defmodule Philomena.Search.Parser do case tokens do [{:int_field, field}, {:eq, _}, {:int, value}, {:fuzz, _}, {:number, fuzz} | r_tokens] -> {%{ - range: %{unquote(:"#{name}_alias")(field) => %{gte: trunc(value - fuzz), lte: trunc(value + fuzz)}} + range: %{ + unquote(:"#{name}_alias")(field) => %{ + gte: trunc(value - fuzz), + lte: trunc(value + fuzz) + } + } }, r_tokens} [ @@ -131,7 +136,12 @@ defmodule Philomena.Search.Parser do {:number, fuzz} | r_tokens ] -> {%{ - range: %{unquote(:"#{name}_alias")(field) => %{gte: trunc(value - fuzz), lte: trunc(value + fuzz)}} + range: %{ + unquote(:"#{name}_alias")(field) => %{ + gte: trunc(value - fuzz), + lte: trunc(value + fuzz) + } + } }, r_tokens} [ @@ -141,7 +151,8 @@ defmodule Philomena.Search.Parser do {:fuzz, _}, {:number, fuzz} | r_tokens ] -> - {%{fuzzy: %{unquote(:"#{name}_alias")(field) => %{value: value, fuzziness: fuzz}}}, r_tokens} + {%{fuzzy: %{unquote(:"#{name}_alias")(field) => %{value: value, fuzziness: fuzz}}}, + r_tokens} [ {:ngram_field, field}, @@ -150,7 +161,8 @@ defmodule Philomena.Search.Parser do {:fuzz, _}, {:number, fuzz} | r_tokens ] -> - {%{fuzzy: %{unquote(:"#{name}_alias")(field) => %{value: value, fuzziness: fuzz}}}, r_tokens} + {%{fuzzy: %{unquote(:"#{name}_alias")(field) => %{value: value, fuzziness: fuzz}}}, + r_tokens} [{:default, [text: value]}, {:fuzz, _}, {:number, fuzz} | r_tokens] -> {%{fuzzy: %{unquote(default_field) => %{value: value, fuzziness: fuzz}}}, r_tokens} @@ -196,13 +208,15 @@ defmodule Philomena.Search.Parser do defp unquote(:"#{name}_term")(_ctx, tokens) do case tokens do [{:date_field, field}, {:eq, _}, {:date, [lower, higher]} | r_tokens] -> - {%{range: %{unquote(:"#{name}_alias")(field) => %{gte: lower, lte: higher}}}, r_tokens} + {%{range: %{unquote(:"#{name}_alias")(field) => %{gte: lower, lte: higher}}}, + r_tokens} [{:ngram_field, field}, {:eq, _}, {:text, value} | r_tokens] -> value = process_term(value) if contains_wildcard?(value) do - {%{wildcard: %{unquote(:"#{name}_alias")(field) => unescape_wildcard(value)}}, r_tokens} + {%{wildcard: %{unquote(:"#{name}_alias")(field) => unescape_wildcard(value)}}, + r_tokens} else {%{match: %{unquote(:"#{name}_alias")(field) => unescape_regular(value)}}, r_tokens} end @@ -211,7 +225,8 @@ defmodule Philomena.Search.Parser do value = process_term(value) if contains_wildcard?(value) do - {%{wildcard: %{unquote(:"#{name}_alias")(field) => unescape_wildcard(value)}}, r_tokens} + {%{wildcard: %{unquote(:"#{name}_alias")(field) => unescape_wildcard(value)}}, + r_tokens} else {%{term: %{unquote(:"#{name}_alias")(field) => unescape_regular(value)}}, r_tokens} end