简体   繁体   中英

Full text search MongoDB/Mongoengine

The new version of MongoDB allows Full Text Search. That part is running fine for me:

db.collection.runCommand('text',{search:<keyword>})

However, I'm not sure that it is possible to run it through python's mongoengine. Does anyone know if there is a way to run "runCommand" with mongoengine or a workaround?

(I'm using mongoengine for my project, I'd hate to have to drop it for pymongo as it would probably mean recoding many things.)

Thanks!

You can use MongoEngine by using pymongo directly eg:

class MyDoc(Document):
    pass

coll = MyDoc._get_collection()
coll.database.command(
    "text",
    coll.name,
    search="alice", 
    project={"name": 1, "_id": 0}, 
    limit=10)

Pymongo uses keyord arguments for this. See documentation .
In you case db.command('text', 'colection_name', seach='keyword') .

the question is old but I bumped on the same ask. MongoEngine now enables text search directly.

First, create a text index on the required fields:

class MyDoc(Document):
   document_name = StringField()
   document_content = StringField()

   meta = {
      'indexes': [
         {
            'fields': [
               '$document_name',
               '$document_content'
            ]
         }
      ]
   }

Then, documents are searchable using the search_text function:

document = News.objects.search_text('testing')

MongoEngine Text Search documentation for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM