Searching doesn't work with mongoengine
jeffisabelle opened this issue · comments
I'm having troubles with searching functionality. I use flask-superadmin with mongoengine. But I'm not sure if this problem is related to mongoengine as @wojcikstefan stated in #31 (comment)
Here is the trace:
Traceback (most recent call last):
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/werkzeug/contrib/fixers.py", line 125, in __call__
return self.app(environ, start_response)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask_superadmin/base.py", line 37, in inner
return f(self, *args, **kwargs)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask_superadmin/base.py", line 37, in inner
return f(self, *args, **kwargs)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask_superadmin/base.py", line 37, in inner
return f(self, *args, **kwargs)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask_superadmin/base.py", line 37, in inner
return f(self, *args, **kwargs)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask_superadmin/model/base.py", line 311, in list
search_query=search_query)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/flask_superadmin/model/backends/mongoengine/view.py", line 82, in get_list
count = qs.count()
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 108, in count
self._len = super(QuerySet, self).count(with_limit_and_skip)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 339, in count
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1182, in _cursor
self._cursor_obj = self._collection.find(self._query,
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1215, in _query
self._mongo_query = self._query_obj.to_query(self._document)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 92, in to_query
query = query.accept(QueryCompilerVisitor(document))
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 139, in accept
self.children[i] = self.children[i].accept(visitor)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 157, in accept
return visitor.visit_query(self)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/visitor.py", line 80, in visit_query
return transform.query(self.document, **query.query)
File "~/python/.ENVS/flask/lib/python2.7/site-packages/mongoengine/queryset/transform.py", line 59, in query
raise InvalidQueryError(e)
InvalidQueryError: Cannot resolve field "t"
oh i see search text functionality is still in development branch of mongoengine. ver 0.9.x [1]
the problem is probably related to there.
Hey @jeffisabelle! The stack trace on its own doesn't explicitly identify the issue. What MongoEngine docs did you register in the admin and how? Is t
actually a field on one of the documents you defined?
Hi again,
here how my model looks like. I try to set indexes as described here;
http://docs.mongoengine.org/en/latest/guide/text-indexes.html
this document says that i should set field with a $
prefix. But it raises an exception if i use $
prefix.
mongoengine.errors.LookUpError: Cannot resolve field "$title"
class Post(Document):
title = StringField()
slug = StringField()
publish_date = DateTimeField()
score = FloatField()
upvote = IntField()
downvote = IntField()
thumbnail = StringField()
comment_count = IntField(default=0)
dortgen = ReferenceField(Dortgen)
sender = ReferenceField(User)
seq = SequenceField()
replies = ListField(EmbeddedDocumentField(Comment))
meta = {
'allow_inheritance': True,
'indexes': [
{
'fields': ['title'],
'default_language': 'english'
}
]
}
and this is the admin model I use.
class PostModel(model.ModelAdmin):
fields = (
"title", "slug", "publish_date",
"score", "upvote", "downvote", "dortgen",
"url", "domain", "comment_count", "thumbnail"
)
list_display = ("title", "dortgen", "publish_date")
search_fields = ("title")
I'm not sure but, i think its related with my version of mongoengine. The above article states that
I can search objects with QuerySet.search_text
method.
However when i look at my query set objects, they don't seem to have a search_text
method.
Change this line:
search_fields = ("title")
to this one:
search_fields = ("title",)
That comma at the end is necessary to make it a tuple. ("title")
is just a string and since search_fields
is supposed to be an iterable, it treats the value as a list of chars, i.e. search_fields = ("t", "i", "t", "l", "e")
. Hence the error: InvalidQueryError: Cannot resolve field "t"
Oh, that's did the trick. thanks @wojcikstefan!
No problem, you can close this issue now :)