【发布时间】:2014-04-29 06:56:59
【问题描述】:
我正在使用 Ydn-Db-Fulltext 来允许用户在 HTML5 应用程序中搜索本地联系人数据库。到目前为止,在搜索人名时,它效果很好,很智能,并且可以立即返回结果。
这是一个包含联系人方法数组的联系人对象示例:
{
"source": "COMPANY",
"ownerPin": "12345",
"name": "brian wilkins",
"dateUpdated": "2014-03-18T14:41:05.217Z",
"dateAdded": "2014-03-18T14:41:05.217Z",
"isFavorite": false,
"socialId": "54321",
"id": "1",
"deleted": false,
"edited": false,
"favorite": false,
"contactMethods": [
{
"id": "4321",
"contactKey": "12321",
"contactId": "1",
"value": "brian.wilkins@geemail.com",
"kind": "email",
"ownerPin": "12345",
"isPrimary": false
},
{
"id": "5432",
"contactKey": "2",
"contactId": "1",
"kind": "phone",
"ownerPin": "12345",
"isPrimary": false
},
{
"id": "23",
"contactKey": "333",
"contactId": "1",
"value": "112345",
"kind": "extension",
"ownerPin": "12345",
"isPrimary": false
}
]
}
要在“名称”属性上创建索引,我将 fullTextCatalog 设置如下:
fullTextCatalogs: [{
name: 'name',
lang: 'en',
sources: [
{
storeName: 'contacts',
keyPath: 'id',
weight: 1.0
}, {
storeName: 'contacts',
keyPath: 'name',
weight: 0.5
}
]
}],
stores: [
{
name: 'contacts',
keyPath: 'id',
autoIncrement: true
}
]
};
this.db = new ydn.db.Storage('thedatabase', db_schema);
我可以按姓名或 id(键)搜索并获取匹配的联系人列表。似乎很少存储在内存中。每次搜索都会查询本地后备 indexedDB 数据库。
挑战在于,我还希望能够基于电子邮件地址和扩展名进行搜索,它们存储在contactMethods 数组中的contactMethods 属性中。 “value”属性是我们根据contactMethod类型存储电子邮件地址和/或扩展名的地方。
我尝试将contactMethods 添加为辅助可搜索对象存储,但这导致搜索“Brian”返回两个结果,包括包含姓名的联系人和包含电子邮件地址的contactMethod。理想情况下,我希望获取contactId(联系人的外键)并使用它来提取实际的联系人对象,但这似乎会产生非常昂贵的开销并抵消这个出色搜索工具的好处。
有没有办法索引不在父级的对象属性?我怎样才能以一种既能扩展又不会耗尽所有资源的方式来解决这个问题?
this.db.get(entry.storeName, entry.primaryKey).done(function(x) {
this.textContent += ' [Full name: ' + x.name + ']'; // this is in the contact
this.textContent += ' [email: ' + x.value + ']'; // but this is in the contactMethod
}, span);
【问题讨论】:
-
感谢@Kyaw 的标记帮助。我正在阅读schema documentation 更多内容,这听起来像是父->子一对多的关系,因为数组是我需要的,因为数组是我需要的,其中一个关键元素是名称和另一个是电子邮件地址,或类似的东西。如果我在别人之前弄清楚,我肯定会自我回答。 :)
标签: javascript full-text-search indexeddb full-text-indexing ydn-db