From 675a7de13bb3cd6a0d50b65a2e4fe6619c925605 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Mon, 26 Aug 2019 19:11:26 -0400 Subject: [PATCH] wildcard support --- lib/philomena/search/parser.ex | 43 ++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/philomena/search/parser.ex b/lib/philomena/search/parser.ex index 71b39e11..c1e35e4d 100644 --- a/lib/philomena/search/parser.ex +++ b/lib/philomena/search/parser.ex @@ -156,16 +156,55 @@ defmodule Philomena.Search.Parser do {%{range: %{field => %{gte: lower, lte: higher}}}, r_tokens} [{:ngram_field, field}, {:eq, _}, {:text, value} | r_tokens] -> - {%{match: %{field => value}}, r_tokens} + value = process_term(value) + + if contains_wildcard?(value) do + {%{wildcard: %{field => unescape_wildcard(value)}}, r_tokens} + else + {%{match: %{field => unescape_regular(value)}}, r_tokens} + end + + [{:literal_field, field}, {:eq, _}, {:text, value} | r_tokens] -> + value = process_term(value) + + if contains_wildcard?(value) do + {%{wildcard: %{field => unescape_wildcard(value)}}, r_tokens} + else + {%{term: %{field => unescape_regular(value)}}, r_tokens} + end [{_field_type, field}, {:eq, _}, {_value_type, value} | r_tokens] -> {%{term: %{field => value}}, r_tokens} [{:default, [text: value]} | r_tokens] -> - {%{term: %{ctx[:default_field] => value}}, r_tokens} + value = process_term(value) + + if contains_wildcard?(value) do + {%{wildcard: %{ctx[:default_field] => unescape_wildcard(value)}}, r_tokens} + else + {%{term: %{ctx[:default_field] => unescape_regular(value)}}, r_tokens} + end _ -> raise ArgumentError, "Expected a term" end end + + defp contains_wildcard?(value) do + String.match?(value, ~r/(? String.trim() |> String.downcase() + end end