0
votes
class Tag(db.Document):
    text = db.StringField(unique=True)

class Post(db.Document):
    user = db.ReferenceField(User, required=True)
    pid = db.SequenceField(required=True, unique=True)
    description = db.StringField()
    title = db.StringField(required=True)
    created = db.DateTimeField(default=utils.time_now, required=True)
    updated = db.DateTimeField(default=utils.time_now, required=True)
    tags = db.ListField(db.ReferenceField(Tag), default=[])
    ratings = db.EmbeddedDocumentListField(Rating, default=[])
    comments = db.EmbeddedDocumentListField(Comment, default=[])
    url = db.URLField()

    meta = {'indexes': [
        {'fields': ['$title', '$description', '$tags.text'],
         'default_language': 'english',
         'weights': {'title': 10, 'description': 5, 'tags': 2}
        }
    ]}

Here are my two documents. The Tag document is Referenced in a Listfield of the Post document. When I try to do a search with the text index I can successfully search on the title and description but not the tags. Does anyone know why. I couldn't find any helpful examples here http://docs.mongoengine.org/guide/text-indexes.html

1

1 Answers

0
votes

A ReferenceField type is only de-referenced on access by some internal MongoEngine magic. As such you cannot use it as an element in the text index (the list saved in the mongodb document will hold only bson ObjectId references).