简体   繁体   中英

Flask Mongoengine Text Search Cannot resolve Field

Been pulling my hair out trying to get a text index created in MongoEngine. All of my modules appear to be up to date and I do have text search enabled on my mongodb. I can even create a text index on the collection if I use pymongo. However, I would like to stay in MongoEngine. I've tried several iterations of my model, but the following is a scaled back version that fails:

class Situs(db.Document):
    streetname = db.StringField()
    streetnum = db.StringField()
    dscrptn = db.StringField()
    meta = {'indexes':[{'fields': ['$streetname']}]}

Error at runtime is:
mongoengine.errors.LookUpError: Cannot resolve field "$streetname"

Any thoughts or suggestions would greatly be appreciated

It sounds like you are not initializing your mongo engine properly, so it cannot find the fields declaration. Make sure all the imports are there, and you are setting up the flask app as well. The important piece here is the db variable. Try something like:

from flask_mongoengine import MongoEngine
from flask import Flask

app = Flask(__name__)
app.config["SECRET_KEY"] = "MYDARKLITTLESECRETRAINCOAT"
app.config["MONGODB_SETTINGS"] = {'DB': 'mongodb://mongodb.mydb/databasename'}
db = MongoEngine(app)

class Situs(db.Document):
    streetname = db.StringField()
    streetnum = db.StringField()
    dscrptn = db.StringField()
    meta = {'indexes':[{'fields': ['$streetname']}]}


situs = Situs()
print situs

Output should be: Situs object

Let me know if that works or if you are getting some other kind of errors.

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