Skip to content

Instantly share code, notes, and snippets.

@nod
Last active February 3, 2016 21:16
Show Gist options
  • Save nod/547ff7d0a91ad251fb4a to your computer and use it in GitHub Desktop.
Save nod/547ff7d0a91ad251fb4a to your computer and use it in GitHub Desktop.
Use text search in mongo 3.2+ with pymongo
# For pymongo against mongodb 3.2...
# Given a collection of documents like
# {'textfield': 'cool stuff in a doc', 'other': 'fields...'}
#
# To perform the query, and get results sorted by the textual score:
db.my_collection.create_index([('textfield','text')])
cursor = db.my_collection.find(
{'textfield': 'some string query'},
{'_txtscr': {'$meta': 'textScore'}
).sort([('_txtscr', {'$meta':'textScore'})])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment