From 5163da171bd73443329ce0646df0582cd81f36a7 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 30 Dec 2019 14:49:55 -0500 Subject: [PATCH] allow the parser to determine the right type for the default field --- lib/philomena/comments/query.ex | 2 +- lib/philomena/galleries/query.ex | 2 +- lib/philomena/images/query.ex | 2 +- lib/philomena/posts/query.ex | 2 +- lib/philomena/reports/query.ex | 2 +- lib/philomena/tags/query.ex | 2 +- lib/search/term_range_parser.ex | 10 +++++++--- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/philomena/comments/query.ex b/lib/philomena/comments/query.ex index 4a1de471..8d4f504e 100644 --- a/lib/philomena/comments/query.ex +++ b/lib/philomena/comments/query.ex @@ -46,7 +46,7 @@ defmodule Philomena.Comments.Query do literal_fields = ~W(image_id) ngram_fields = ~W(body) custom_fields = ~W(author user_id) - default_field = "body" + default_field = {"body", :ngram} transforms = %{ "user_id" => &Philomena.Comments.Query.user_id_transform/2, "author" => &Philomena.Comments.Query.author_transform/2 diff --git a/lib/philomena/galleries/query.ex b/lib/philomena/galleries/query.ex index c13ecefe..97c6bc1b 100644 --- a/lib/philomena/galleries/query.ex +++ b/lib/philomena/galleries/query.ex @@ -5,7 +5,7 @@ defmodule Philomena.Galleries.Query do literal_fields = ~W(title user image_ids watcher_ids) date_fields = ~W(created_at updated_at) ngram_fields = ~W(description) - default_field = "title" + default_field = {"title", :term} aliases = %{ "user" => "creator" } diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index 35c10b8c..8e909b6e 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -65,7 +65,7 @@ defmodule Philomena.Images.Query do literal_fields = ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format) ngram_fields = ~W(description) custom_fields = ~W(gallery_id) - default_field = "namespaced_tags.name" + default_field = {"namespaced_tags.name", :term} transforms = %{ "gallery_id" => &Philomena.Images.Query.gallery_id_transform/2 } diff --git a/lib/philomena/posts/query.ex b/lib/philomena/posts/query.ex index c626f7fa..a0f65577 100644 --- a/lib/philomena/posts/query.ex +++ b/lib/philomena/posts/query.ex @@ -46,7 +46,7 @@ defmodule Philomena.Posts.Query do literal_fields = ~W(forum_id topic_id) ngram_fields = ~W(body subject) custom_fields = ~W(author user_id) - default_field = "body" + default_field = {"body", :ngram} transforms = %{ "user_id" => &Philomena.Posts.Query.user_id_transform/2, "author" => &Philomena.Posts.Query.author_transform/2 diff --git a/lib/philomena/reports/query.ex b/lib/philomena/reports/query.ex index 3f49efec..6fd8d4db 100644 --- a/lib/philomena/reports/query.ex +++ b/lib/philomena/reports/query.ex @@ -7,7 +7,7 @@ defmodule Philomena.Reports.Query do ip_fields = ~W(ip) bool_fields = ~W(open) ngram_fields = ~W(reason) - default_field = "reason" + default_field = {"reason", :ngram} @parser Parser.parser( int_fields: int_fields, diff --git a/lib/philomena/tags/query.ex b/lib/philomena/tags/query.ex index 1c1615a6..5b677cef 100644 --- a/lib/philomena/tags/query.ex +++ b/lib/philomena/tags/query.ex @@ -5,7 +5,7 @@ defmodule Philomena.Tags.Query do literal_fields = ~W(slug name name_in_namespace namespace implies alias_of implied_by aliases category analyzed_name) bool_fields = ~W(aliased) ngram_fields = ~W(description short_description) - default_field = "analyzed_name" + default_field = {"analyzed_name", :ngram} aliases = %{ "implies" => "implied_tags", "implied_by" => "implied_by_tags", diff --git a/lib/search/term_range_parser.ex b/lib/search/term_range_parser.ex index bde09c9d..a67a27e3 100644 --- a/lib/search/term_range_parser.ex +++ b/lib/search/term_range_parser.ex @@ -1,18 +1,22 @@ defmodule Search.TermRangeParser do alias Search.LiteralParser + alias Search.NgramParser # Unfortunately, we can't use NimbleParsec here. It requires # the compiler, and we're not in a macro environment. - def parse(input, fields, default_field) do + def parse(input, fields, {default_field, type}) do tokens = Enum.find_value(fields, fn {f, p} -> field(input, f, p) end) - tokens || [{LiteralParser, default_field}, range: :eq, value: input] + tokens || [{parser(type), default_field}, range: :eq, value: input] end + defp parser(:term), do: LiteralParser + defp parser(:ngram), do: NgramParser + defp field(input, field_name, field_parser) do field_sz = byte_size(field_name) @@ -33,4 +37,4 @@ defmodule Search.TermRangeParser do nil end end -end \ No newline at end of file +end