Skip to content

Commit

Permalink
Fix lucene support
Browse files Browse the repository at this point in the history
  • Loading branch information
willholley committed Oct 26, 2023
1 parent c700f12 commit 6f7f750
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/mango/src/mango_selector_text.erl
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ convert(Path, {[{<<"$exists">>, ShouldExist}]}) ->
false -> {op_not, {FieldExists, false}}
end;
convert(Path, {[{<<"$beginsWith">>, Arg}]}) when is_binary(Arg) ->
Prefix = mango_util:lucene_escape_query_value(Arg),
Suffix = <<"*">>,
PrefixSearch = value_str(<<Arg/binary, Suffix/binary>>),
PrefixSearch = <<Prefix/binary, Suffix/binary>>,
{op_field, {make_field(Path, Arg), PrefixSearch}};
% We're not checking the actual type here, just looking for
% anything that has a possibility of matching by checking
Expand Down
50 changes: 37 additions & 13 deletions src/mango/test/03-operator-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
# License for the specific language governing permissions and limitations under
# the License.

from requests.exceptions import HTTPError
import mango
import unittest


class BaseOperatorTests:
class Common(object):
class Common(unittest.TestCase):
def assertUserIds(self, user_ids, docs):
user_ids_returned = list(d["user_id"] for d in docs)
user_ids.sort()
Expand Down Expand Up @@ -140,22 +141,45 @@ def test_exists_false_returns_missing_but_not_null(self):
self.assertGreater(len(docs), 0)
for d in docs:
self.assertNotIn("twitter", d)

def test_beginswith(self):
cases = [
{
"prefix": "New",
"user_ids": [2, 10]
},
{
# test escaped characters - note the space in the test string
"prefix": "New ",
"user_ids": [2, 10]
},
{
# non-string values in documents should not match the prefix,
# but should not error
"prefix": "Foo",
"user_ids": []
},
{
"prefix": " New",
"user_ids": []
}
]

def test_beginswith(self):
docs = self.db.find({"location.state": {"$beginsWith": "New"}})
self.assertEqual(len(docs), 2)
self.assertUserIds([2, 10], docs)
for case in cases:
with self.subTest(prefix=case["prefix"]):
selector = {"location.state": {"$beginsWith": case["prefix"]}}
docs = self.db.find(selector)
self.assertEqual(len(docs), len(case["user_ids"]))
self.assertUserIds(case["user_ids"], docs)

# non-string prefixes should return an error
def test_beginswith_invalid_prefix(self):
docs = self.db.find({"location.state": {"$beginsWith": 123}})
self.assertEqual(len(docs), 2)

# non-string values in documents should not match the prefix,
# but should not error
def test_beginswith_invalid_prefix(self):
docs = self.db.find({"user_id": {"$beginsWith": "Foo"}})
self.assertEqual(len(docs), 0)
cases = [123, True, [], {}]
for prefix in cases:
with self.subTest(prefix=prefix):
with self.assertRaises(HTTPError):
self.db.find({"location.state": {"$beginsWith": prefix}})



class OperatorJSONTests(mango.UserDocsTests, BaseOperatorTests.Common):
Expand Down

0 comments on commit 6f7f750

Please sign in to comment.