MongoEngine
latest
  • 1. Tutorial
  • 2. User Guide
    • 2.1. Installing MongoEngine
    • 2.2. Connecting to MongoDB
    • 2.3. Defining documents
    • 2.4. Documents instances
    • 2.5. Querying the database
    • 2.6. Document Validation
    • 2.7. GridFS
    • 2.8. Signals
    • 2.9. Text Search
      • 2.9.1. Defining a Document with text index
      • 2.9.2. Querying
      • 2.9.3. Ordering by text score
    • 2.10. Documents migration
    • 2.11. Logging/Monitoring
    • 2.12. Use mongomock for testing
  • 3. API Reference
  • 4. Changelog
  • 5. Frequently Asked Questions
  • 6. Django Support
MongoEngine
  • »
  • 2. User Guide »
  • 2.9. Text Search
  • Edit on GitHub

2.9. Text Search¶

After MongoDB 2.4 version, supports search documents by text indexes.

2.9.1. Defining a Document with text index¶

Use the $ prefix to set a text index, Look the declaration:

class News(Document):
    title = StringField()
    content = StringField()
    is_active = BooleanField()

    meta = {'indexes': [
        {'fields': ['$title', "$content"],
         'default_language': 'english',
         'weights': {'title': 10, 'content': 2}
        }
    ]}

2.9.2. Querying¶

Saving a document:

News(title="Using mongodb text search",
     content="Testing text search").save()

News(title="MongoEngine 0.9 released",
     content="Various improvements").save()

Next, start a text search using QuerySet.search_text method:

document = News.objects.search_text('testing').first()
document.title # may be: "Using mongodb text search"

document = News.objects.search_text('released').first()
document.title # may be: "MongoEngine 0.9 released"

2.9.3. Ordering by text score¶

objects = News.objects.search_text('mongo').order_by('$text_score')
Next Previous

© Copyright 2009, MongoEngine Authors Revision ac8ba504.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: latest
Versions
latest
stable
Downloads
On Read the Docs
Project Home
Builds