在Restheart中查询文档(文本搜索)

时间:2018-09-18 13:38:13

标签: mongodb restheart

我创建了一个集合,并在mondo shell中将字段索引为“文本”。 然后,我使用以下参数查询文档:

localhost:8080/db/tags?filter={"$text":{"$search":"some text"}}

并得到错误:

http status code 500
http status description "Internal Server Error"
message "Query failed with error code 2 and error message 'Too many text expressions' on server 127.0.0.1:27017"

但是在mongo shell中,一切正常。我对此查询有严格的回应:

db.tags.find({"$text":{"$search":"some text"}})

怎么了?我通过这些教程做了所有事情:

https://docs.mongodb.com/manual/reference/operator/query/text/#text-query-examples

https://softinstigate.atlassian.net/wiki/spaces/RH/pages/10747996/Query+Documents#QueryDocuments-filteringFiltering

1 个答案:

答案 0 :(得分:1)

我建议使用RESTHeart而不是Mongo shell重新创建索引。

首先,使用外壳删除索引,然后按照以下示例创建新的索引:https://restheart.org/learn/indexes/

例如,您可以使用以下内容为集合tags创建命名索引:

PUT /db/tags/_indexes/<index_id> {"keys": {"title": "text" }}

HTTP/1.1 201 Created

然后请告诉我们是否可行。

已更新

以下是我在RESTHeart上成功测试全文搜索的操作(顺便说一句,我正在使用httpie客户端,但是curl的工作原理类似):

  1. cd进入RESTHeart文件夹
  2. 使用docker-compose up启动dockerized RESTHeart

然后发出以下命令:

创建数据库

http -a admin:changeit PUT http://localhost:8080/mydb

创建收藏集

http -a admin:changeit PUT http://localhost:8080/mydb/sample

发布示例文档

http -a admin:changeit POST http://localhost:8080/mydb/sample < sample.json

创建一个名为“ about”的集合索引,该索引使用JSON文档中的“ about”元素进行文本索引和搜索

http -j -a admin:changeit PUT http://localhost:8080/mydb/sample/_indexes/about keys:='{"about":"text"}}'

检查索引

http -a admin:changeit GET http://localhost:8080/mydb/sample/_indexes

执行成功的全文搜索

http -a admin:changeit GET http://localhost:8080/mydb/sample?filter='{"$text":{"$search":"\"Consequat fugiat commodo irure\""}}'

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Location, ETag, Auth-Token, Auth-Token-Valid-Until, Auth-Token-Location, X-Powered-By
Auth-Token: 240ym6d4sbxujjl8qa0ezslyfcpx7tmrjo4lfqmcxx9uirjjcs
Auth-Token-Location: /_authtokens/admin
Auth-Token-Valid-Until: 2018-09-19T07:46:37.717Z
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 793
Content-Type: application/json
Date: Wed, 19 Sep 2018 07:31:37 GMT
ETag: 5ba11d9aa7b11b0006d54c5f
X-Powered-By: restheart.org

{
    "_embedded": [
        {
            "_etag": {
                "$oid": "5ba11db6a7b11b0006d54c61"
            }, 
            "_id": "5ba11d3ffb91c9eb48eee9f2", 
            "about": "Nulla pariatur eu dolor ad fugiat cillum. Ex consectetur id velit officia veniam pariatur nisi ea et nisi aliquip reprehenderit adipisicing incididunt. Exercitation esse mollit in pariatur eiusmod veniam quis est consequat ad. Fugiat eu excepteur fugiat incididunt et. Consequat fugiat commodo irure id magna in magna minim non anim amet. Officia ipsum veniam excepteur consequat labore.", 
            "address": "923 Sheffield Avenue, Wescosville, California, 2455", 
            "age": 20, 
            "balance": "$1,335.75", 
            "company": "FLEXIGEN", 
            "email": "terra.gross@flexigen.name", 
            "eyeColor": "blue", 
            "favoriteFruit": "strawberry", 
            "friends": [
                {
                    "id": 0, 
                    "name": "May Rowland"
                }, 
                {
                    "id": 1, 
                    "name": "Schmidt Herman"
                }, 
                {
                    "id": 2, 
                    "name": "Saundra Shepard"
                }
            ], 
            "greeting": "Hello, Terra! You have 6 unread messages.", 
            "guid": "c529609b-8535-4aea-a386-d3861e3fe831", 
            "index": 3, 
            "isActive": false, 
            "latitude": "-43.467295", 
            "longitude": "114.929505", 
            "name": {
                "first": "Terra", 
                "last": "Gross"
            }, 
            "phone": "+1 (956) 474-2649", 
            "picture": "http://placehold.it/32x32", 
            "range": [
                0, 
                1, 
                2, 
                3, 
                4, 
                5, 
                6, 
                7, 
                8, 
                9
            ], 
            "registered": "Saturday, February 17, 2018 1:13 PM", 
            "tags": [
                "excepteur", 
                "velit", 
                "sint", 
                "sit", 
                "eu"
            ]
        }
    ], 
    "_etag": {
        "$oid": "5ba11d9aa7b11b0006d54c5f"
    }, 
    "_id": "sample", 
    "_returned": 1
}

您会在附件中找到sample.json.zip文件。

我建议将上述步骤与您的操作进行比较,看看是否存在明显差异。