【问题标题】:Cloudant: How to perform wildcard searches on text fieldsCloudant:如何对文本字段执行通配符搜索
【发布时间】:2017-03-27 19:57:07
【问题描述】:

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

count word

4     a         
1     a boy
1     a boy goes

我想运行这样的查询

word: *boy*

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

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

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

【问题讨论】:

    标签: lucene wildcard cloudant python-cloudant


    【解决方案1】:

    您可以使用$regexcondition operator

    {
     "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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 2022-01-11
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多