-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of manual ngram-based search in MongoDB #993
Open
ml-evs
wants to merge
6
commits into
main
Choose a base branch
from
ml-evs/mongo-fts-ngram
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2945624
Initial noodling with manual ngram index in MongoDB
ml-evs 7cee59e
Add working tests
ml-evs bb82c64
Implement rudimentary ngram-based search with item updates and add tests
ml-evs 6b32637
Rebase
ml-evs 4fc2c6e
Make sure `find_one_and_update` returns the updated doc
ml-evs 34c7a7e
Add a temp. view to compare searches
BenjaminCharmes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"""This module tests fundamental routines around | ||
the n-gram FTS. | ||
|
||
""" | ||
|
||
from pydatalab.mongo import _generate_item_ngrams, _generate_ngrams, create_ngram_item_index | ||
|
||
|
||
def test_ngram_single_field(): | ||
field = "ABCDEF" | ||
ngrams = _generate_ngrams(field, 3) | ||
expected = ["abc", "bcd", "cde", "def"] | ||
assert list(ngrams) == expected | ||
assert all([ngrams[e] == 1 for e in expected]) | ||
|
||
field = "ABC" | ||
ngrams = _generate_ngrams(field, 3) | ||
assert ngrams == {"abc": 1} | ||
|
||
field = "some: punctuation" | ||
ngrams = _generate_ngrams(field, 3) | ||
expected = ["som", "ome", "pun", "unc", "nct", "ctu", "tua", "uat", "ati", "tio", "ion"] | ||
assert list(ngrams) == expected | ||
|
||
field = "What about a full sentence? or: even, two?" | ||
ngrams = _generate_ngrams(field, 3) | ||
expected = [ | ||
"wha", | ||
"hat", | ||
"abo", | ||
"bou", | ||
"out", | ||
"ful", | ||
"ull", | ||
"sen", | ||
"ent", | ||
"nte", | ||
"ten", | ||
"enc", | ||
"nce", | ||
"eve", | ||
"ven", | ||
"two", | ||
] | ||
assert list(ngrams) == expected | ||
assert all([ngrams[e] == 1 for e in expected]) | ||
|
||
|
||
def test_ngram_item(): | ||
item = {"refcode": "ABCDEF"} | ||
assert _generate_item_ngrams(item, {"refcode"}, n=3) == {"abc": 1, "bcd": 1, "cde": 1, "def": 1} | ||
|
||
|
||
def test_ngram_fts_route(client, default_sample_dict, real_mongo_client, database): | ||
default_sample_dict["item_id"] = "ABCDEF" | ||
response = client.post("/new-sample/", json=default_sample_dict) | ||
assert response.status_code == 201 | ||
|
||
# Check that creating the ngram index with existing items works | ||
create_ngram_item_index(real_mongo_client, background=False, filter_top_ngrams=None) | ||
|
||
doc = database.items_fts.find_one({}) | ||
ngrams = set(doc["_fts_ngrams"]) | ||
for ng in ["abc", "bcd", "cde", "def", "sam", "ple"]: | ||
assert ng in ngrams | ||
assert doc["type"] == "samples" | ||
|
||
query_strings = ("ABC", "ABCDEF", "abcd", "cdef") | ||
|
||
for q in query_strings: | ||
response = client.get(f"/search-items-ngram/?query={q}&types=samples") | ||
assert response.status_code == 200 | ||
assert response.json["status"] == "success" | ||
assert len(response.json["items"]) == 1 | ||
assert response.json["items"][0]["item_id"] == "ABCDEF" | ||
|
||
# Check that new items are added to the ngram index | ||
default_sample_dict["item_id"] = "ABCDEF2" | ||
response = client.post("/new-sample/", json=default_sample_dict) | ||
assert response.status_code == 201 | ||
|
||
for q in query_strings: | ||
response = client.get(f"/search-items-ngram/?query={q}&types=samples") | ||
assert response.status_code == 200 | ||
assert response.json["status"] == "success" | ||
assert len(response.json["items"]) == 2 | ||
assert response.json["items"][0]["item_id"] == "ABCDEF" | ||
assert response.json["items"][1]["item_id"] == "ABCDEF2" | ||
|
||
# Check that updates are reflected in the ngram index | ||
# This test also makes sure that the string 'test' is not picked up from the refcode, | ||
# which has an explicit carve out | ||
default_sample_dict["description"] = "test string with punctuation" | ||
update_req = {"item_id": "ABCDEF2", "data": default_sample_dict} | ||
response = client.post("/save-item/", json=update_req) | ||
assert response.status_code == 200 | ||
|
||
query_strings = ("test", "punctuation") | ||
|
||
for q in query_strings: | ||
response = client.get(f"/search-items-ngram/?query={q}&types=samples") | ||
assert response.status_code == 200 | ||
assert response.json["status"] == "success" | ||
assert len(response.json["items"]) == 1 | ||
assert response.json["items"][0]["item_id"] == "ABCDEF2" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only be run on one of the API processes, or there should be a lock