Indexeddb: search using wildcards

做~自己de王妃 提交于 2019-12-10 13:17:50

问题


I was wondering if it is possible to perform a search on an indexeddb object store using wildcards. It would be handy to find all object with a key beginning with '555' for example


回答1:


This is possible out of the box using either compound keys or key fragments. The way keys work in IndexedDB is that you generate a "keyRange" object and pass it to your cursor invocation. The keyrange passes info like "start at A and end at Z, inclusive."

By nature, there is partial matching built into this; the downside is that your cursor will return any keys that come between your keys and you might have to filter down those results further.

Say you have these words as keys in a object store:

  • Aardvark
  • Apple
  • Google
  • Microsoft

The key range "A to Z, inclusive" would return all of these but "Ap to Z, inclusive" would return just the last three.

Another technique I've used to implement this is by passing a "filter" function to my methods that invoke IndexedDB. Inside the methods onsuccess callback, pass the result (event.target.result) through your filter function and if it returns true then call your method invoker's onsuccess callback.




回答2:


Yes it is feasible to use wildcards, sort of.

I can't yet vote or even comment on previous answers (hmmm...) so I'll just repeat user2025527's answer as it totally worked for my needs.

Use the bounds method and specify the base value for the 1st argument and the same value plus an extra character for the 2nd argument.

In most cases the extra character character should be the last one in your charset: \uffff

But you're free to decide what constitutes the limit, especially when dealing with localization.

Lest say you have the following values in your index:

  • A
  • AB
  • B
  • BA
  • BB
  • C

To find everything stating with "BA" you should use

var range = IDBKeyRange.bound("BA", "BA" + '\uffff');



回答3:


It isn't possible by default, but my library i wrote for the indexeddb supports it. Try linq2indexeddb.




回答4:


Search is also possible with wildcards in indexeddb see link IndexedDB Fuzzy Search

for wildcard below should work: var range = IDBKeyRange.bound("555", "555" + '\uffff');

Or else can use Linq2indexeddb library to make use of like.



来源:https://stackoverflow.com/questions/9791219/indexeddb-search-using-wildcards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!