SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

sort_by parameter ignored for text search #129

Closed cenguix closed 8 years ago

cenguix commented 8 years ago

Hi Andrea, Maurizio and the RestHeart team:

I am working with RestHeart and httpie issuing REST queries. I am aware that the sort_by field works fine for other fields but in the case of text search it is ignored. I tried everything but nothing works. In MongoDB shell it works fine this query:

db.books2.find({'$text':{'$search':'foundation+Hari+Seldon-Mule'}},{'title':1,'score':{'$meta':'textScore'}}).sort({'score':{'$meta':'textScore'}})

In the sort field I cannot put instead {'score':1} because it returns the following error:

Error: error: {
    "waitedMS" : NumberLong(0),
    "ok" : 0,
    "errmsg" : "can't have a non-$meta sort on a $meta projection",
    "code" : 2
} 

Therefore in the sort field I am forced to insert the $meta project operator with the textScore keyword.

I am using httpie for issuing queries because it is very easy to read the json results as compared to curl. The following GET REST operation that I am having trouble is:

http GET 127.0.0.1:8080/books/books2 filter=="{'$text':{'$search':'foundation+Hari+Seldon-Mule'}}" keys=="{'title':1}" keys=="{'score':{'$meta':'textScore'}}" sort_by=="{'score':{'$meta':'textScore'}}" 

And the json result is the following:

{
    "_embedded": {
        "rh:doc": [
            {
                "_id": {
                    "$oid": "56e06f95a4638ceb49c49e43"
                },
                "score": 3.5,
                "title": "Second Foundation"
            },
            {
                "_id": {
                    "$oid": "56e07181a4638ceb49c49e45"
                },
                "score": 2.9166666666666665,
                "title": "Foundation?s Edge"
            },
            {
                "_id": {
                    "$oid": "559eca1919f46b1020bc84ec"
                },
                "score": 2.5,
                "title": "Foundation and Empire"
            },
            {
                "_id": {
                    "$oid": "559ec99e19f46b1020bc84eb"
                },
                "score": 3.25,
                "title": "Foundation"
            }
        ]
    },
    "_id": "books2",
    "_returned": 4
}

As you can see the results are not ordered by score descendently as done in the MongoDB shell.

Please let me know if this is a bug for text search or something that I am missing in the httpie request to RestHeart

ujibang commented 8 years ago

I've checked it, and the problem is that _sortby query parameter does not support json mongodb sort expressions but just field names.

According to the documentation, it has the following format:

sort_by=[+|-]<fieldname>

We will add support for mongodb json sort expression; I'm already working on it and will let you know when ready.

For information refer to jira improvement task https://softinstigate.atlassian.net/browse/RH-190

Thanks

ujibang commented 8 years ago

done.

you can try the snapshot build restheart-2.1.0-20160503.105713-2.tar.gz.

please check if it is working, when confirmed we'll release version 2.0.1 with it.

cenguix commented 8 years ago

Hi Andrea: At least all the tests that I have performed with text search score are ordered descending as done in the Mongodb shell. Well done and thanks for fixing the bug soon. RestHeart Rocks!

ujibang commented 8 years ago

Hi @cenguix

FYI 2.0.1 is out with support for sort_by json expression format

also docs updated https://softinstigate.atlassian.net/wiki/x/XACk#QueryDocuments-Sortbysearchscore