0

我在cloudant中有一个看起来像这样的数据库

count word

4     a         
1     a boy
1     a boy goes

我想运行这样的查询

word: *boy*

我如何在 cloudant 中做到这一点?我尝试了以下但没有奏效

{
  "selector": {
    "word": "*boy*"
  },
  "fields": [

  ],
  "sort": [
    {
      "_id": "asc"
    }
  ]
}
4

1 回答 1

2

您可以使用$regex条件运算符

{
 "selector": {
   "word": {
    "$regex": "boy"
   }
 },
 "fields": [
 ],
 "sort": [
  {
   "_id": "asc"
  }
 ]
}

从文档:

与文档字段匹配的正则表达式模式。仅当字段是字符串值并且与提供的正则表达式匹配时才匹配。

因为正则表达式不适用于索引,所以如果您的数据集相当大,您应该考虑使用Cloudant Search而不是 Cloudant Query。

创建一个定义word文档属性索引的设计文档:

{
 "_id": "_design/search_example",
 "indexes": {
   "word": {
    "index": "function(doc){if (doc.word) {index('word', doc.word, {'store': true});}}"
  }
 }
}

运行搜索:

GET https://$HOST/$DATABASE/_design/search_example/_search/word?q=word:boy HTTP/1.1

结果将包括在文档属性中包含指定字符串的所有word文档。要退回文件,请使用?q=word:boy&include_docs=true

希望这可以帮助!

于 2017-03-28T06:51:36.950 回答