- MongoEngine - Discussion
- MongoEngine - Useful Resources
- MongoEngine - Quick Guide
- MongoEngine - Extensions
- MongoEngine - Text Search
- MongoEngine - Signals
- MongoEngine - GridFS
- MongoEngine - Javascript
- MongoEngine - Atomic Updates
- MongoEngine - Document Inheritance
- MongoEngine - Advanced Queries
- MongoEngine - Aggregation
- MongoEngine - Indexes
- MongoEngine - Custom Query Sets
- MongoEngine - Sorting
- MongoEngine - QuerySet Methods
- MongoEngine - Query Operators
- MongoEngine - Filters
- MongoEngine - Querying Database
- MongoEngine - Add/Delete Document
- MongoEngine - Fields
- MongoEngine - Dynamic Schema
- MongoEngine - Document Class
- MongoEngine - Connecting to MongoDB Database
- MongoEngine - Installation
- MongoEngine - Object Document Mapper
- MongoEngine - MongoDB Compass
- MongoEngine - MongoDB
- MongoEngine - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MongoEngine - Text search
MongoDB supports use of query operators that can perform text search on a string content. As described earper, to set a text index prefix name of index with $ symbol. For a text index, the weight of an indexed field denotes the significance of the field relative to the other indexed fields in terms of the text search score. You can also specify default language in meta dictionary of the class.
List of supported languages can be found at
MongoEngine API consists of search_text() method for QuerySet object. The string to be searched in indexed fields is given as argument.In the following example, we first define a Document class called lang with two string fields, name of language and its features. We also create indexes on both fields with respective weights.
from mongoengine import * con=connect( newdb ) class lang (Document): name=StringField() features=StringField() meta = { indexes : [ { fields : [ $name , "$features"], default_language : engpsh , weights : { name : 2, features : 10} }] } l1=lang() l1.name= C++ l1.features= Object oriented language for OS development l1.save() l2=lang() l2.name= Python l2.features= dynamically typed and object oriented for data science, AI and ML l2.save() l3=lang() l3.name= HTML l3.features= scripting language for web page development l3.save()
In order to perform search for word ‘oriented’, we employ search_text() method as follows −
docs=lang.objects.search_text( oriented ) for doc in docs: print (doc.name)
Output of the above code will be names of languages in whose description the word ‘oriented’ occurs (‘Python and ‘C++’ in this case).
Advertisements