Last active
October 3, 2021 15:18
-
-
Save cpatrick/5719077 to your computer and use it in GitHub Desktop.
Sample Python for running a full-text search using PyMongo
This file contains hidden or 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
from pymongo import Connection | |
if __name__ == '__main__': | |
# Connect to mongo | |
conn = Connection() | |
db = conn['canepi'] | |
# Set the search term | |
term = 'foo' | |
# Run the search | |
results = db.command('text', 'healthmap', search=term) | |
# Print the results | |
print(results) |
Awesome @legel!
how to do multiple text search, for eg: search for word1 and word2 together in the text sentence?
@saamkhya
I ended up with the following for scoring the documents with multiple words:
cursor = collection.find(
{'$text': {'$search': 'some words'}},
{'score': {'$meta': 'textScore'}})
# Sort by 'score' field.
cursor.sort([('score', {'$meta': 'textScore'})])
Make sure you index the collection first.
Source: https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/cursor.py#L658
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks @legel!