question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Removing items by id

See original GitHub issue

I’ve got documents that look like this:

const tasks = [
{ id: 1, title: "clean the house" }, 
{ id: 2, title: "eat food" }
]

If the title of the task with id = 1 changes, I’d like to update that change in the index. However, in my current application, I don’t have access to the entire old version of the document. I just know the id and the new values for the title field.

In order to remove an item in Minisearch, it looks like I need to pass the whole document that I originally added. Is there a way I can remove an item by id? If so, I can just remove by id and then add the new document.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
priyadarshycommented, May 4, 2020

Thank you for the detailed help and explanation of why this isn’t supported. Your decision making makes total sense to me. I’m happy to implement this within my application in order support removals, I was just avoiding it incase there was a way to remove items by id.

Thank you for building this awesome library. Before I implemented minisearch, I experimented with fuse, lunr and flexsearch. Minisearch had the best documentation and the it’s search UX felt like it worked nicely out of the box, like a user would expect in most web apps. I found the rest of libraries to be very academic (lunr), hard to get started with (flexsearch), or slow (fuse). Thank you!

3reactions
lucaongcommented, Nov 23, 2022

@mashpie @clibu @lukevp @rfox12 @priyadarshy I revive this old thread to announce that the newly released v6.0.0-beta.1 (soon to be v6.0.0) includes a new method discard (and related methods discardAll and replace) which removes a document by ID.

The reasons given in this thread for why remove takes a full document are still valid, but discard takes a different approach, made possible by the internal design of MiniSearch. Instead of immediately removing a document from the index (which would only be feasible given the original document), discard marks the current document version as discarded, so it is immediately ignored by searches, but does not mutate the index. Instead, that is left to a vacuuming process, ran by default after a certain number of documents are discarded. Vacuuming traverses the index and removes references to discarded documents, allowing memory to be released. See the documentation here: https://lucaong.github.io/minisearch/classes/_minisearch_.minisearch.html#discard

This provides the feature requested by this issue, while avoiding the drawbacks of a naive implementation: the original documents are not stored, and use cases that do not need to remove documents incur no overhead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove element by id - javascript - Stack Overflow
Running this in the chrome console only seems to delete one element at a time when using document.getElementsByClassName("my-elements").remove ...
Read more >
Remove element by id in JavaScript? - Tutorialspoint
To remove element by id, you can use remove(). Example. Following is the code − <!DOCTYPE html> <html lang="en"> <head> <meta ...
Read more >
Element.remove() - Web APIs - MDN Web Docs
The Element.remove() method removes the element from the DOM.
Read more >
How to Remove an Element From an Array by ID in JavaScript
To remove an element from an array by ID in JavaScript, use the findIndex() method to find the index of the object with...
Read more >
How To Remove Element By Id In Javascript - Detailed Guide
You can remove element by Id in javascript using the document.getelementbyid("element_id").remove() method. In this tutorial, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found