Add quotes to numeric strings For numeric strings, we surround them with quotes when passed to $text. This way, Clouseau will use the QueryParser for tokenization rather than TermQuery. This is currently a workaround until we add json parser support to Clouseau.
BugzID: 45572 Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/a8def140 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/a8def140 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/a8def140 Branch: refs/heads/2787-merge-repos Commit: a8def140992cf0d2868c1abce5714ab43e754176 Parents: 29eddf0 Author: Tony Sun <tony....@cloudant.com> Authored: Tue Aug 25 10:26:02 2015 -0700 Committer: Tony Sun <tony....@cloudant.com> Committed: Tue Aug 25 13:23:49 2015 -0700 ---------------------------------------------------------------------- src/mango_selector_text.erl | 3 ++- src/mango_util.erl | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/a8def140/src/mango_selector_text.erl ---------------------------------------------------------------------- diff --git a/src/mango_selector_text.erl b/src/mango_selector_text.erl index bdd82bf..47e25ce 100644 --- a/src/mango_selector_text.erl +++ b/src/mango_selector_text.erl @@ -45,7 +45,8 @@ convert(Path, {[{<<"$default">>, Arg}]}) -> % The $text operator specifies a Lucene syntax query % so we just pull it in directly. convert(Path, {[{<<"$text">>, Query}]}) when is_binary(Query) -> - {op_field, {make_field(Path, Query), value_str(Query)}}; + Term = mango_util:append_quotes(value_str(Query)), + {op_field, {make_field(Path, Query), Term}}; % The MongoDB docs for $all are super confusing and read more % like they screwed up the implementation of this operator http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/a8def140/src/mango_util.erl ---------------------------------------------------------------------- diff --git a/src/mango_util.erl b/src/mango_util.erl index 99e15d5..b12c6e9 100644 --- a/src/mango_util.erl +++ b/src/mango_util.erl @@ -34,6 +34,7 @@ lucene_escape_field/1, lucene_escape_query_value/1, lucene_escape_user/1, + append_quotes/1, has_suffix/2, @@ -45,6 +46,35 @@ -include("mango.hrl"). +%% https://gist.github.com/cad2d2456c518d878d08 +-define(NUMSTRING, {re_pattern,25,0,<<69,82,67,80,71,3,0,0,0,0,0,0,1,0,0,0,25,0, +0,0,0,0,0,0,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,3,19,77,255,255, +255,255,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,77,0,0,0,0,0, +40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,73,94,0,11,0,1,27,78,27, +97,27,78,83,0,19,27,73,27,110,27,102,27,105,27,110,27,105,27,116,27,121,83,2, +137,94,2,131,0,2,94,0,145,0,3,94,0,137,0,4,94,0,9,0,5,58,15,2,3,84,0,9,102,94,0, +7,0,6,27,46,84,0,7,94,0,18,0,7,102,94,0,9,0,8,58,15,2,3,84,0,9,84,0,18,102,94,0, +84,0,9,77,0,0,0,0,0,0,0,0,32,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77, +0,0,0,0,0,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,73,94,0,9,0,10, +58,15,2,3,84,0,9,84,0,84,84,0,137,83,0,121,94,0,115,0,11,27,46,94,0,17,0,12,94, +0,9,0,13,58,15,2,3,84,0,9,84,0,17,102,94,0,84,0,14,77,0,0,0,0,0,0,0,0,32,0,0,0, +32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,0,0,0,0,40,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,73,94,0,9,0,15,58,15,2,3,84,0,9,84,0,84,84,0, +115,83,1,79,94,1,73,0,16,94,0,101,0,17,94,0,93,0,18,27,48,77,0,0,0,0,0,0,0,0,0, +0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,94,0,39,0,19,77,0,0,0,0,0,0,255,3, +126,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,71,84,0,39,102,94,0,7,0,20, +27,46,84,0,7,84,0,93,83,0,141,94,0,135,0,21,27,48,77,0,0,0,0,0,0,0,0,0,0,0,1,0, +0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,102,94,0,39,0,22,77,0,0,0,0,0,0,255,3,126, +0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,71,84,0,39,94,0,7,0,23,27,46,84, +0,7,94,0,39,0,24,77,0,0,0,0,0,0,255,3,126,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,71,84,0,39,84,0,135,84,0,242,77,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,0,0,0,0,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,73,94,0,9,0,25,58,15,2,3,84,0,9,84,1,73,84,2,89,77,0,0,0,0,0,0,0, +0,80,0,0,0,80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,73,84,2,131,84,2,167,77,255, +255,255,255,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,84,3,19, +0>>}). + + open_doc(Db, DocId) -> open_doc(Db, DocId, [deleted]). @@ -310,3 +340,21 @@ join(_Sep, [Item]) -> [Item]; join(Sep, [Item | Rest]) -> [Item, Sep | join(Sep, Rest)]. + + +is_number_string(Subject) -> + case re:run(Subject, ?NUMSTRING) of + nomatch -> + false; + _ -> + true + end. + + +append_quotes(TextValue) -> + case is_number_string(binary_to_list(TextValue)) of + true -> + <<"\"", TextValue/binary, "\"">>; + false -> + TextValue + end.