2014-09-11 53 views
2

無殼匹配我有具有以下數據結構文檔的數據庫:使用意見Cloudant

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

而且我有以下看法:

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

我需要檢索的所有文檔,其中「主題」匹配提供的密鑰。我希望它不區分大小寫。 POST會給我所有我想要的比賽,但只有在比賽結果匹配的情況下。

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

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

我也嘗試過搜索索引沒有成功。這個API提到了一個我無法工作的regex操作符。這似乎是一件簡單的事情,我該如何處理這件事?

關於$正則表達式的方法,這裏是我在哪裏。以下POST與我之前嘗試返回區分大小寫的結果非常相似。

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

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

代爲$當量一個$正則表達式運算符產生以下錯誤:

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

此函數的參考材料是相當渺茫。它只是提到這個網頁上:https://docs.cloudant.com/api/cloudant-query.html

回答

0

一種選擇是創建一個存儲大寫的搜索字符串二級指標:

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

然後,應用程序可以將它發送到數據庫之前大寫搜索條件:

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

會導致這樣的:

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

我認爲使用Cloudant Query $ regex操作符是一種可行的方法。

您在接受該錯誤時收到該錯誤的原因是因爲您無法使用$ 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.

因此,相反,你可以這樣做:

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

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

這應該給你你正在尋找,但是,文檔結果不下去下面要說的,所以要小心大型數據庫:

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.

+0

爲$正則表達式文檔的URL發生了變化,在這裏你去:https://console.bluemix.net/docs/services/Cloudant/ API /cloudant_query.html#combination-operators – Netsmile 2017-11-23 14:07:40