我想快速搜索我的邮袋数据库。我下面的查询非常慢,即使对于包含1000个项目和字段索引的小型数据集也是如此。我猜这是因为我使用的是正则表达式。我什至尝试将正则表达式作为“ ^” +搜索(只搜索开始),这需要一段时间(10秒)
在以下字段上进行OR搜索的最佳方法是什么?
这里是代码:
db_items.find({
selector: {name: {$regex: RegExp("^"+search, "i")}},
fields: ['_id', 'name','unit_price','category','quantity','item_id'],
sort: ['name']
}
我们在pouchdb中使用pouchdb查找和二级索引时遇到了相同的问题。
我们的pouchdb有5K个项目,按词条搜索确实很慢(在低性能设备上大约需要20秒)。
然后,我们决定将搜索更改为使用allDocs函数。我们还利用了我们文档的ID。基本上,我们只想对具有“ product :: {someProductId}”之类的id的类型为“ product”的文档进行按词搜索。因此,我们进行了如下操作,它极大地提高了我们的搜索性能(在性能低下的设备上,它的速度降至3s)
db.allDocs({
include_docs: true,
attachments: false,
startkey: "product::", // search only in docs starting with product::
endkey: "product::\ufff0"
})
.then(function(result) {
const rows = result.rows.filter(
i =>
i.doc &&
i.doc.name &&
(i.doc.name.toLowerCase().indexOf(term.toLowerCase()) > -1 || i.doc. category.toLowerCase().indexOf(term.toLowerCase()) > -1)
);
return rows
})