Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

projecting score in $text search #841

Closed
izderadicka opened this issue Mar 24, 2016 · 4 comments
Closed

projecting score in $text search #841

izderadicka opened this issue Mar 24, 2016 · 4 comments

Comments

@izderadicka
Copy link

I cannot get this working with eve

?where={"$text":{"$search":"cap zbynek"}}&projection={"score":{"$meta":"textScore"}}&sort=[("score", {"$meta": "textScore"})]

This is expected to behave as this pymongo code (sorted text search result):

ebooks.find({'$text':{'$search':'Cap Zbynek'}}, {'score': {'$meta': 'textScore'}}).sort([('score', {'$meta': 'textScore'})])

Problem seems to be with projection - in eve it does not seem to be working as $meta projection, but rather as a regular projection - so it basically masks all fields from document except _id.

@izderadicka
Copy link
Author

This is stack trace from current develop branch:

127.0.0.1 - - [25/Mar/2016 08:30:51] "GET /ebooks?where={%22$text%22:{%22$search%22:%22cap%20zbynek%22}}&projection={%22score%22:{%22$meta%22:%22textScore%22}}&sort=[(%27score%27,%20{%27$meta%27:%20%27textScore%27})] HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/ivan/workspace/mybookshelf2/eve/eve/flaskapp.py", line 985, in __call__
    return super(Eve, self).__call__(environ, start_response)
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/endpoints.py", line 54, in collections_endpoint
    response = get(resource, lookup)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/methods/common.py", line 262, in rate_limited
    return f(*args, **kwargs)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/auth.py", line 79, in decorated
    return f(*args, **kwargs)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/methods/common.py", line 968, in decorated
    r = f(resource, **combined_args)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/methods/get.py", line 39, in get
    return get_internal(resource, **lookup)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/methods/get.py", line 112, in get_internal
    return _perform_find(resource, lookup)
  File "/home/ivan/workspace/mybookshelf2/eve/eve/methods/get.py", line 187, in _perform_find
    for document in cursor:
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/pymongo/cursor.py", line 1090, in next
    if len(self.__data) or self._refresh():
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/pymongo/cursor.py", line 1012, in _refresh
    self.__read_concern))
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/pymongo/cursor.py", line 905, in __send_message
    helpers._check_command_response(doc['data'][0])
  File "/home/ivan/.virtualenvs/eve/lib/python3.4/site-packages/pymongo/helpers.py", line 196, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: must have $meta projection for all $meta sort keys

@nicolaiarocci
Copy link
Member

Try setting allow_unknown = True for the resource. Problem is, your projection is actually "injecting" a field in the document, which is not included in the endpoint schema so it gets filtered out and the subsequent sort on that field fails.

@izderadicka
Copy link
Author

OK all_unknown helped in this case, but it'll also open PUT and PATCH to any fields, right? This is not exactly what I need. Or can this be limited just to GET method?

@nicolaiarocci
Copy link
Member

No it won't. What you can do however is setup a dedicated endpoint with read-only access and allows_unknown activated. Use this endpoint for client-side text searches; the write-enabled one for PUT, POST and PATCH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants