bcdonahoo07
bcdonahoo07

Reputation: 11

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

Upvotes: 1

Views: 1171

Answers (1)

Johnny Gasyna
Johnny Gasyna

Reputation: 481

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.

Upvotes: 1

Related Questions