diff --git a/lib/search/literal_parser.ex b/lib/search/literal_parser.ex index c5eee148..21e0cf2e 100644 --- a/lib/search/literal_parser.ex +++ b/lib/search/literal_parser.ex @@ -1,11 +1,16 @@ defmodule Search.LiteralParser do import NimbleParsec + import Search.Helpers - defp trim([term]), do: String.trim(term) + float = + ascii_string([?0..?9], min: 1) + |> optional(ascii_char('.') |> ascii_string([?0..?9], min: 1)) + |> reduce({List, :to_string, []}) + |> reduce(:to_number) edit_distance = ignore(string("~")) - |> integer(min: 1) + |> concat(float) |> unwrap_and_tag(:fuzz) |> eos() @@ -24,7 +29,6 @@ defmodule Search.LiteralParser do ]) |> repeat() |> reduce({List, :to_string, []}) - |> reduce(:trim) |> unwrap_and_tag(:literal) |> optional(edit_distance) |> eos() @@ -40,7 +44,6 @@ defmodule Search.LiteralParser do ]) |> repeat() |> reduce({List, :to_string, []}) - |> reduce(:trim) |> unwrap_and_tag(:wildcard) |> ignore(optional(edit_distance)) |> eos() diff --git a/lib/search/parser.ex b/lib/search/parser.ex index 5f5422fb..e80466a1 100644 --- a/lib/search/parser.ex +++ b/lib/search/parser.ex @@ -42,7 +42,14 @@ defmodule Search.Parser do %{parser | __fields__: Map.new(fields)} end - def parse(%Parser{} = parser, input, context \\ nil) do + def parse(parser, input, context \\ nil) + + # Empty search should emit a match_none. + def parse(_parser, "", _context) do + {:ok, %{match_none: %{}}} + end + + def parse(%Parser{} = parser, input, context) do parser = %{parser | __data__: context} with {:ok, tokens, _1, _2, _3, _4} <- Lexer.lex(input), @@ -51,7 +58,7 @@ defmodule Search.Parser do {:ok, tree} else {:ok, {_tree, tokens}} -> - {:error, "Junk at end of expression: " <> debug_tokens(tokens)} + {:error, "junk at end of expression: " <> debug_tokens(tokens)} {:error, msg, start_pos, _1, _2, _3} -> {:error, msg <> ", starting at: " <> start_pos} @@ -60,7 +67,7 @@ defmodule Search.Parser do {:error, msg} _ -> - {:error, "Search parsing error."} + {:error, "unknown parsing error"} end end @@ -113,8 +120,8 @@ defmodule Search.Parser do {:ok, {child, r_tokens}} -> {:ok, {%{bool: %{must_not: child}}, r_tokens}} - err -> - err + value -> + value end end @@ -128,8 +135,8 @@ defmodule Search.Parser do {:ok, {_child, _tokens}} -> {:error, "Imbalanced parentheses."} - err -> - err + value -> + value end end @@ -245,7 +252,7 @@ defmodule Search.Parser do defp field_type(parser, [{DateParser, field_name}, range: :eq, date: [lower, upper]]), - do: {:ok, {%{range: %{field(parser, field_name) => %{gte: lower, lte: upper}}}, []}} + do: {:ok, {%{range: %{field(parser, field_name) => %{gte: lower, lt: upper}}}, []}} defp field_type(parser, [{DateParser, field_name}, range: r, date: [_lower, upper]]) when r in [:lte, :gt], do: {:ok, {%{range: %{field(parser, field_name) => %{r => upper}}}, []}} diff --git a/lib/search/term_range_parser.ex b/lib/search/term_range_parser.ex index e8aa0717..bde09c9d 100644 --- a/lib/search/term_range_parser.ex +++ b/lib/search/term_range_parser.ex @@ -18,17 +18,17 @@ defmodule Search.TermRangeParser do case input do <<^field_name::binary-size(field_sz), ":", value::binary>> -> - [{field_parser, field_name}, range: :eq, value: value] + [{field_parser, field_name}, range: :eq, value: String.trim(value)] <<^field_name::binary-size(field_sz), ".eq:", value::binary>> -> - [{field_parser, field_name}, range: :eq, value: value] + [{field_parser, field_name}, range: :eq, value: String.trim(value)] <<^field_name::binary-size(field_sz), ".gt:", value::binary>> -> - [{field_parser, field_name}, range: :gt, value: value] + [{field_parser, field_name}, range: :gt, value: String.trim(value)] <<^field_name::binary-size(field_sz), ".gte:", value::binary>> -> - [{field_parser, field_name}, range: :gte, value: value] + [{field_parser, field_name}, range: :gte, value: String.trim(value)] <<^field_name::binary-size(field_sz), ".lt:", value::binary>> -> - [{field_parser, field_name}, range: :lt, value: value] + [{field_parser, field_name}, range: :lt, value: String.trim(value)] <<^field_name::binary-size(field_sz), ".lte:", value::binary>> -> - [{field_parser, field_name}, range: :lte, value: value] + [{field_parser, field_name}, range: :lte, value: String.trim(value)] _ -> nil end