allow the parser to determine the right type for the default field

This commit is contained in:
byte[] 2019-12-30 14:49:55 -05:00
parent dd5d553c0d
commit 5163da171b
7 changed files with 13 additions and 9 deletions

View file

@ -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

View file

@ -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"
}

View file

@ -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
}

View file

@ -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

View file

@ -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,

View file

@ -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",

View file

@ -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
end