Worklight :: JSONStore ::如何使用其他搜索字段

时间:2014-11-14 12:10:24

标签: ibm-mobilefirst jsonstore

我正在使用Worklight6.2,我遇到了一个与JSONStores有关的小问题。

我的应用程序中有几个用于帮助我在第三方数据库上关联我的关系模型。 为了正确使用这个范例,我试图使用几个搜索索引来查找我的商店内的文档。 假设我有一个这方面的商店

var data = {GUID: 'XPTO-XPTZ-FOO', product_name= 'potatos'}

有时我想通过GUID访问我的对象,有时我想通过product_name访问它。 所以我会有一个

var searchField = {GUID: 'string'};
var additionalSearchField = {product_name: 'string'};

事情是,当我使用这个additionalSearchField时,它找不到我的potatos。我想使用additionalSearchField来避免JSONStore重新创建。

我认为我没有按照预期的方式使用其他搜索字段,但我无法理解其概念。

来自IBM文档:

  

其他搜索字段是已编制索引但不属于存储的JSON数据的键。这些字段定义了键,其值(在给定的JSON集合中)被索引并可用于更快地搜索。

http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.1.0/com.ibm.worklight.dev.doc/devref/r_jsonstore_search_fields.html

有人可以帮我理解它们是如何工作的吗?

2 个答案:

答案 0 :(得分:3)

您应该使用 product_name 作为该集合初始化的一部分,因此您将有两个搜索字段 UID product_name ,即:

var collections = {
    products : {
        searchFields: {UID: "string", product_name: "string"}
    }
};


WL.JSONStore.init(collections);

然后你可以继续进行搜索,即:

$("#searchByUID").on("click", function(){
    var needle = $("#search").val();

    var query = {
        UID: needle
    };

    var collectionName = 'products';
    var options = {
        exact: true,
        limit: 10 //max of 10 docs
    }; 

    WL.JSONStore.get(collectionName).find(query, options)
    .then(function (results) {
        // handle your results
    })
    .fail(function (errorObject) {
        // handle error
    });
});

您也可以对 product_name

执行相同的操作
$("#searchByProductName").on("click", function(){
    var needle = $("#search").val();

    var query = {
        product_name: needle
    };

    // ...
});

答案 1 :(得分:3)

以下是用于创建像localStorage这样的键/值存储的其他搜索字段的示例。

var KEY_VALUE_COLLECTION_NAME = 'keyvalue';

var collections = {};

//Define the 'keyvalue' collection and use additional search fields
collections[KEY_VALUE_COLLECTION_NAME] = {
    searchFields : {},
    additionalSearchFields : { key: 'string' }
};

WL.JSONStore.init(collections);
//I skipped the success and failure callbacks

//The code bellow assumes that the 'keyvalue' collection has been initialized.

var value = 'myValue';
var key = 'myKey';
WL.JSONStore.get(KEY_VALUE_COLLECTION_NAME).add({key: value}, {additionalSearchFields: {key: key}});
//I skipped the success and failure callbacks

//The code bellow assumes that add has finished sucesfully.

WL.JSONStore.get(KEY_VALUE_COLLECTION_NAME).find({key: key}, {exact: true, limit: 1});
//Should call the success callback with something like this: 
//[{_id: 1, json: {value: 'myValue'}}]

查看商店in the docs的内部表示。

如果我们想象我们添加了searchField1searchField2而不是我上面使用的空对象,它将如下所示:

+-----+-----------------------------+--------------+--------------+----------------------------------------+
| _id | key (additionalSearchField) | searchField1 | searchField2 | json                                   |
+-----+-----------------------------+--------------+--------------+----------------------------------------+
| 1   | myKey                       | a            | b            | {searchField1: 'a', searchField2: 'b'} |
+-----+-----------------------------+--------------+--------------+----------------------------------------+

请注意myKey key(附加搜索字段)的值不是存储的JSON对象的一部分。这就是文档说的原因:

  

其他搜索字段是已编制索引但未编制索引的键   存储的JSON数据的一部分。

创建表后,没有简单的方法可以更改表的列(即搜索字段和其他搜索字段)。这就是你遇到这个问题的原因:-2 PROVISION_TABLE_SEARCH_FIELDS_MISMATCH。要更改要编制索引的内容,您需要编写一些迁移代码以将数据移动到新集合,或使用removeCollectiondestroy API删除集合。

相关问题