阅读背景:

使用云计算中的视图进行无实例匹配

来源:互联网 

I have a database of documents that have the following data structure:

我有一个有以下数据结构的文档数据库:

{
    "_id": "sampleID",
    "_rev": "sample-rev",
    "added": "2014-09-09 01:05:32",
    "cached": 1,
    "subject": "sample topic",
    "mode": "<samplemode>",
    "protected": 0,
    "added_by": "myname",
    "factoid": "sample factoid"
}

And I have the following view:

我有以下观点:

function(doc) {
    if (doc.subject && doc.factoid && doc.mode){
        emit(doc.subject, doc.factoid);
    }
}

I need to retrieve all docs where the "subject" matches a provided key. I would like it to be non case sensitive. The POST will give me all of the matches I want, but only if the case matches.

我需要检索“subject”与提供的密钥匹配的所有文档。我希望它是非区分大小写的。这个帖子将会给我所有我想要的匹配,但是只有在匹配的情况下。

https://<username>.cloudant.com/<db>/_design/<designdoc>/_view/<view>?include_docs=true

{ "keys" : ["<subject>"] }

I have also tried search indexes without success. This API mentions a regex operator that I haven't been able to get working. It seems like a simple thing, how should I be approaching this?

我也尝试过搜索索引,但没有成功。这个API提到了一个regex操作符,我无法使用它。这看起来很简单,我该怎么做呢?

Regarding the $regex approach, here is where I'm at. The following POST works much like my previous attempts at returning case-sensitive results.

关于$regex方法,这是我的观点。下面的文章与我之前返回区分大小写的结果的尝试非常相似。

https://<username>.cloudant.com/<db>/_find

{
    "selector": {
        "subject": {"$eq": "<subject>"}
    },
    "fields": ["_id", "_rev", "subject", "factoid"]
}

Substituting a $regex operator for the $eq yields the following error:

将$regex操作符替换为$eq,会产生以下错误:

{
"error": "no_usable_index",
"reason": "There is no operator in this selector can used with an index."
}

The reference material for this function is rather slim. It is only mentioned on this page: https://docs.cloudant.com/api/cloudant-query.html

这个函数的参考资料相当少。这个页面上只提到:https://docs.cloudant.com/api/cloudant-query.html

2 个解决方案

#1


0  

One option is to create a secondary index that stores the uppercased search string:

一个选项是创建一个二级索引来存储大写搜索字符串:

function(doc) {
    if (doc.subject){
        emit(doc.subject.toUpperCase());
    }
}

The application can then uppercase the search criteria before sending it to the database:

然后,应用程序可以将搜索条件大写,然后将其发送到数据库:

https://username.cloudant.com/database/_design/designdoc/_view/view?key=“SAMPLE TOPIC"&include_docs=true

Will result in this:

这将导致:

{
    "offset": 0,
    "rows": [
        {
            "doc": {
                "_id": "sampleID",
                "_rev": "1-d77a468b74497771f8d37130a7cf02eb",
                "added": "2014-09-09 01:05:32",
                "added_by": "myname",
                "cached": 1,
                "factoid": "sample factoid",
                "mode": "<samplemode>",
                "protected": 0,
                "subject": "sample topic"
            },
            "id": "sampleID",
            "key": "SAMPLE TOPIC",
            "value": null
        }
    ],
    "total_rows": 1
}

#2


3  

I think using the Cloudant Query $regex operator is the way to go.

我认为使用Cloudant查询$regex运算符是一种方法。

The reason you received that error when you subbed it in was because you cannot use $regex as the base of your selector expression. Here are the relevant bits from their documentation (https://docs.cloudant.com/api.html#condition-operators):

当代入时,您收到该错误的原因是您不能使用$regex作为选择器表达式的基础。以下是他们文档中的相关位(https://docs.cloudant.com/api.html#condition-operators):

However, not all operators can be used as the base or starting point of the selector expression.

但是,并不是所有的操作符都可以用作选择器表达式的基或起点。

You cannot use combination or array logical operators such as $regex as the basis of a query.

不能使用组合或数组逻辑运算符(如$regex)作为查询的基础。

So instead, you could do something like:

所以,你可以这样做:

https://<username>.cloudant.com/<db>/_find

{
    "selector": {
        "_id": {
            "$gt": null
        },
        "subject": {"$regex": "<subject>"}
    },
    "fields": ["_id", "_rev", "subject", "factoid"]
}

This should give you the result you are looking for, however, the documentation does go on to say the following, so be careful on large databases:

但是,这应该会给您带来您想要的结果,但是,文档确实会继续说,所以在大型数据库上要小心:

This expression is always true, enabling the remainder of the selector expression to be applied.

这个表达式总是正确的,使选择器表达式的其余部分得以应用。

Using {"_id": { "$gt":null } } induces a full-table scan, and is not efficient for large databases.

使用{"_id": {" $gt":null}引导一个全表扫描,对于大型数据库来说是无效的。


分享到: