-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get all documents? #144
Comments
Hi @sfeast , In general, it's often easier to filter outside of MiniSearch if you do not need to perform a full-text search. Something like: documents.filter((doc) => doc.val >= minVal) That said, if you really want to do that within MiniSearch, one way to do that is by creating a dummy field that always has a certain value: const miniSearch = new MiniSearch({
fields: ['dummy', 'val', /* ...your other fields here */],
storeFields: ['val'],
extractField: (document, fieldName) => {
// Create a dummy field that always have the value "xxx"
if (fieldName === 'dummy') { return 'xxx' }
return document[fieldName]
}
})
// Searching for "xxx" in field "dummy" should return all documents
miniSearch.search('xxx', {
fields: ['dummy'],
filter: (result) => result.val >= minVal
}) If, instead of Now, this is admittedly a little hacky, but it should get it done. |
Thanks @lucaong - both current options are workable for me & I appreciate the detailed example 🙇 One last question - with this approach:
|
Happy to help :) Yes, that’s correct, in the first option that would be your own copy of the documents. |
I am looking for an alternative to fuse.js that optionally returns the original list given an empty search query |
Hi @samuelstroschein , const documents = [/* your documents…*/]
const docsById = documents.reduce((byId, doc) => {
byId[doc.id]
return byId
}, {})
const miniSearch = new MiniSearch({
fields: [/* your fields… */]
})
const search = (query, options = {}) => {
if (query == null) { return [] }
if (query.trim().length === 0) {
return documents
} else {
return miniSearch.search(query, options).map((result) => docsById[result.id])
}
}
// Usage
search('')
// => …all documents
search('something')
// => documents matching 'something' As you can see, I had to make some choices that depend on the use case, such as:
Each of these decisions could vary depending on the specific needs of a project. Therefore, also considering that it is simple to define such a wrapper function, I think it is better that |
Implement the option with a callback instead of a boolean flag. const search = new MiniSearch({
fields: [/* your fields… */],
returnOriginalDocumentsWhen: (searchQuery) => searchQuery === ""
})
Hmm, I mean that's why people install an npm package in the first place. I don't want to write code. The discussion in the other package is quite big, indicating that a lot of people want that feature. EditActually what it really needs is just a filter instead of search function.
|
It's unfortunately more complicated than that. For example, how should documents be sorted? It would seem reasonable to return them in the original order, but what if one defines a Similarly, since Moreover, at the moment Of course, it is theoretically possible to implement options for each of these choices, but that would make the API surface huge, and hard to learn. Instead, these details are better defined in code. The reason why code is better than configuration in this case, is that configuration is something that has to be learned for each and every library, while code is general purpose: for a configuration option to be ergonomic, it has to save the developer a non-trivial amount of code or cognitive load. If it generates more open questions, it is not worth, because learning all the implications takes more effort than taking control of the issue with code.
I would say, one does not want to write code at the wrong level of abstraction. What I mean is: even when using a library, one does have to write code. The point is that one normally prefers to avoid writing code that pertains to the internal details of the problem solved by the library, and instead focus on code pertaining to the higher level goal of the application. Therefore, a library has to choose its own boundaries and goals. It would be absolutely appropriate to build a library on top of
I understand and respect the fact that many people have this need. As a matter of fact, even some of my own apps have the same need. But apart from using In sum, I do agree with you that yours is a common need. My opinion though, is that such need is better served by writing some thin layer of code, like the example I provided, than by adding more configuration options. But it is perfectly reasonable to disagree with that, and such thin layer can be packaged in a library for convenience. |
Thank you for the in-depth reply and explanation. I overread the stated goal of "[...] enables developers to build [turn-key opinionated solutions] on top of its core API, but does not provide them out of the box" and was looking (expecting) a drop-in replacement for fuse.js. On a side note, I have a question regarding your i18n workflow at megaloop. Can you send me a DM on Twitter or via email? |
Is there a way to get all documents returned as results?
For example:
miniSearch.search("")
returns an empty array, but I'm looking for a way to get the opposite, all documents.
A use case I have is that I want to only filter by a numeric range in some cases. Something like this:
but that currently returns nothing since no results are given to the filter.
I know it's not the best use of this library as mentioned here - #119 (comment) however it's just one of several scenarios I'm using it for & would be great to be able to leverage it as well for this.
& awesome library btw 🙌 🙏
The text was updated successfully, but these errors were encountered: