简体   繁体   English

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

[英]Worklight :: JSONStore :: How to work with additional search fields

I'm using Worklight6.2 and I have a small problem related with JSONStores. 我正在使用Worklight6.2,但我有一个与JSONStores相关的小问题。

I have several on my application to aid me relating to my relational model on a third party database. 我有几个应用程序可以帮助我与第三方数据库上的关系模型相关。 To properly work with this paradigm I'm trying to use several search indexes to find documents inside my store. 为了正确使用此范例,我尝试使用多个搜索索引在商店内查找文档。 Let's say I have a store with this aspect 假设我有一家这方面的商店

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

Sometimes I want to access my object by GUID some other times I want to access it by product_name. 有时我想通过GUID访问我的对象,有时我想通过product_name访问它。 So I would have a 所以我会有一个

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

Thing is, when I use this additionalSearchField it doesn't find my potatos. 问题是,当我使用这个AdditionalSearchField时,找不到我的土豆。 I would like to use additionalSearchField to avoid JSONStore recreations. 我想使用AdditionalSearchField避免JSONStore重新创建。

I think that I'm not using additional Search Fields the way they were intended, but I'm having trouble wrapping my head about its concept. 我认为我没有按照预期的方式使用其他“搜索字段”,但是我很难理解它的概念。

From IBM Documentation: 从IBM文档:

Additional search fields are keys that are indexed but that are not part of the JSON data that is stored. 其他搜索字段是已索引但不属于存储的JSON数据的一部分的键。 These fields define the key whose values (in a given JSON collection) are indexed and can be used to search more quickly. 这些字段定义了其索引值(在给定的JSON集合中)的键,可用于更快地进行搜索。

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

Can someone help me understand how they work? 有人可以帮助我了解他们的工作方式吗?

You should use the product_name as part of the initialization of that collection, so you will have two search fields UID and product_name , ie: 您应该在集合的初始化过程中使用product_name ,因此您将拥有两个搜索字段UIDproduct_name ,即:

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


WL.JSONStore.init(collections);

Then you can go ahead and do the searching, ie: 然后,您可以继续进行搜索,即:

$("#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
    });
});

You could also do the same for the product_name 您也可以对product_name进行相同的操作

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

    var query = {
        product_name: needle
    };

    // ...
});

Here's an example of additional search fields being used to create a key/value store like localStorage . 这是一个用于创建键/值存储(如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'}}]

Take a look at the internal representation of the store in the docs . 看一下docs中商店的内部表示。

If we imagine we added searchField1 and searchField2 instead of the empty object I used above, it would look like this: 如果我们想象我们添加了searchField1searchField2而不是我上面使用的空对象,它将看起来像这样:

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

Notice that myKey the value of the key (additional search field) is not part of the JSON object stored. 注意myKey所述的值key (附加搜索字段)没有存储在JSON对象的一部分。 That's why the documentation says: 这就是为什么文档说:

Additional search fields are keys that are indexed but that are not part of the JSON data that is stored. 其他搜索字段是已索引但不属于存储的JSON数据的一部分的键。

There's no easy way to change the columns of the table (ie search fields and additional search fields) once it has been created. 创建表后,没有简单的方法来更改表的列(即搜索字段和其他搜索字段)。 That's why you run into this: -2 PROVISION_TABLE_SEARCH_FIELDS_MISMATCH . 这就是为什么您遇到以下问题: -2 PROVISION_TABLE_SEARCH_FIELDS_MISMATCH To change what is being indexed you need to write some migration code to move data to a new collection or remove the collection with the removeCollection or destroy APIs. 要更改被索引的内容,您需要编写一些迁移代码以将数据移至新集合或使用removeCollection删除集合或destroy API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM