lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.67k stars 133 forks source link

[Question] how to get an index through id #209

Closed Mister-Hope closed 1 year ago

Mister-Hope commented 1 year ago

Hi, I pass string index to browser and then load the index string with Minisearch.loadJSON.

Is there a way I can get the original index with it's id? The only way I find out is to parse the index string myself, which does the work twice.

lucaong commented 1 year ago

Hi @Mister-Hope , I am not sure I understand what you mean. Could you try to explain again?

Mister-Hope commented 1 year ago

Hi there I add some documents at node side/server and passed the stringify content to client.

As the docs mentioned, i can get the instance back via loadJSON.

What I need is to get any document with its id, like using has method to check whether an id exist. Is there a way to do so?

The problem is that I only get an index string, and the loadJSON parse it internally, so I think that it would be better for me to achieve it witj some methods, instead of parsing index and extracting the content myself.

Mister-Hope commented 1 year ago

Since id is a primary and value-only field, I think it's reasonable for users to get the origin document through their id besides searching😉

lucaong commented 1 year ago

I see now, thanks :)

Unfortunately, it is not possible in general to retrieve a document by its ID. The reason is that MiniSearch only stores the inverted index (from terms to the document ID), and does not store the original documents. This is by design.

That said, one thing that would be useful, and that I can implement, is to retrieve the stored fields (corresponding to the storeFields option) for a document given its ID. While that would not necessarily be the full document, it could help in your case.

sandstrom commented 1 year ago

Small suggestion; you can add the 'suggestions tab' to this repo, if you want to move some things that aren't really issues (more like feature requests) away from the issues group.

Mister-Hope commented 1 year ago

Sure, getting field value with its Id and field(s) name would be enough. My motivation is just based on one point. If a search box with index is loaded, why don't I reuse the search index to get related information in other cases.

lucaong commented 1 year ago

Here is a pull request implementing getStoredFields(documentId): https://github.com/lucaong/minisearch/pull/210

lucaong commented 1 year ago

@Mister-Hope version v6.1.0 is published on NPM, including this feature. I am therefore closing this issue, but feel free to comment on it if necessary.