Comments (4)
A feature that I think might help, would be to add a queryTerms array to the MiniSearch results (that in the case above would only contain some for the result with ID 4): this way, one could define custom logic based on either the number of query terms matched, or the number terms matched in the results.
@lucaong thanks for your insights here; I hadn't thought about the terms list in filter and will see if that satisfies my needs. Per your comment here, it might not; I might really care about minimum terms in the query, not in the document. But you're right that it's not going to be the same for everyone.
I like the idea mentioned here as well because I was yesterday wishing that I also had access, in the result, to how the terms match. That is: I might want to filter out a result if all the matches are fuzzy, or by prefix, rather than whole term matches.
from minisearch.
Hi @subsetpark ,
yours is an interesting idea. One reason why I am not sure whether to implement it as a feature though, is that there is already an easy way to implement this on top of MiniSearch
, which does not rely on generating all combinations. You could perform a standard search, and use the filter
option to only retain results where the terms
array is longer than your "minimum should match":
const miniSearch = new MiniSearch({
fields: ['text']
})
const documents = [
{ id: 1, text: 'Something ordinary' },
{ id: 2, text: 'Something extraordinary' },
{ id: 3, text: 'Something truly extraordinary' },
{ id: 4, text: 'Something somehow strange' }
]
miniSearch.addAll(documents)
miniSearch.search("something truly extraordinary", {
combineWith: 'OR',
filter: (result) => result.terms.length >= 2
})
/* =>
[
{
id: 3,
score: 8.584958632398532,
terms: [ 'something', 'truly', 'extraordinary' ],
match: { something: [Array], truly: [Array], extraordinary: [Array] }
},
{
id: 2,
score: 2.5275597943271197,
terms: [ 'something', 'extraordinary' ],
match: { something: [Array], extraordinary: [Array] }
}
]
*/
As you can see, even though we are combining with OR
(and therefore all four documents in the example should match at least one term), only the results that match at least 2 terms are returned.
There is one situation with "minimum should match" that can be confusing, which occurs when using fuzzy or prefix match: the terms
array reflects the matched terms in the result documents, not in the search query. Therefore, one might meet the "minimum should match" even if fewer query terms match, when a single query term expands to multiple matches:
// Using the same data as above
miniSearch.search("some extraordinary", {
combineWith: 'OR',
prefix: true,
filter: (result) => result.terms.length >= 2
})
/* =>
[
{
id: 2,
score: 2.3012539398292353,
terms: [ 'something', 'extraordinary' ],
match: { something: [Array], extraordinary: [Array] }
},
{
id: 3,
score: 2.0778819231808248,
terms: [ 'something', 'extraordinary' ],
match: { something: [Array], extraordinary: [Array] }
},
{
id: 4,
score: 0.6200953275356065,
terms: [ 'somehow', 'something' ],
match: { somehow: [Array], something: [Array] }
}
]
*/
In the results above, one might expect the document with ID 4 to be excluded, as it only matches one query term, but it is included as some
also expands to something
due to prefix search. It's really up to the specific application how this case should be treated, therefore a simple "minimum should match" option is not a one-size-fits-all solution.
A feature that I think might help, would be to add a queryTerms
array to the MiniSearch
results (that in the case above would only contain some
for the result with ID 4): this way, one could define custom logic based on either the number of query terms matched, or the number terms matched in the results.
from minisearch.
Here's a pull request introducing queryTerms
to search results.
from minisearch.
And it's now released on NPM as part of version v6.3.0
. I would therefore consider this issue closed, but @subsetpark feel free to comment further if necessary.
For reference, here is the revised solution, using the new queryTerms
result field:
const miniSearch = new MiniSearch({
fields: ['text']
})
const documents = [
{ id: 1, text: 'Something ordinary' },
{ id: 2, text: 'Something extraordinary' },
{ id: 3, text: 'Something truly extraordinary' },
{ id: 4, text: 'Something somehow strange' }
]
miniSearch.addAll(documents)
miniSearch.search("something truly extraordinary", {
combineWith: 'OR',
filter: (result) => result.queryTerms.length >= 2
})
/* =>
[
{
id: 3,
score: 8.584958632398532,
terms: [ 'something', 'truly', 'extraordinary' ],
queryTerms: [ 'something', 'truly', 'extraordinary' ],
match: { something: [Array], truly: [Array], extraordinary: [Array] }
},
{
id: 2,
score: 2.5275597943271197,
terms: [ 'something', 'extraordinary' ],
queryTerms: [ 'something', 'extraordinary' ],
match: { something: [Array], extraordinary: [Array] }
}
]
*/
// And now also the example using prefix or fuzzy match works as expected:
miniSearch.search("some extraordinary", {
combineWith: 'OR',
prefix: true,
filter: (result) => result.queryTerms.length >= 2
})
/* =>
[
{
id: 2,
score: 2.3012539398292353,
terms: [ 'something', 'extraordinary' ],
queryTerms: [ 'some', 'extraordinary' ],
match: { something: [Array], extraordinary: [Array] }
},
{
id: 3,
score: 2.0778819231808248,
terms: [ 'something', 'extraordinary' ],
queryTerms: [ 'some', 'extraordinary' ],
match: { something: [Array], extraordinary: [Array] }
}
]
*/
Note that by intersecting terms
and queryTerms
you can also find which terms matched exactly.
I hope this solves your case :) Thanks again for opening this issue, I think this is a useful addition to MiniSearch
.
from minisearch.
Related Issues (20)
- Barebones, framework agnostic example HOT 8
- Is it possible to make autoSuggest suggest the entire title of my blogs instead of just one word? HOT 2
- case-sensitive dynamic selection during search HOT 2
- `fuzzy` predicate function? HOT 2
- how to index nested field with its value is an array HOT 6
- about search result HOT 8
- Any way to search across multiple vitepress sites? HOT 4
- How to have a search at least as good as `includes` HOT 3
- How to prevent treating terms separately? HOT 2
- Can `loadJSON` be added as an instance method which merges indices? HOT 3
- Any notification on status of data loading? HOT 1
- Search terms are broken when immediately following unicode whitespace HOT 9
- Get Mini Search version? HOT 2
- Re-index to new documents with unknown changes HOT 4
- Generating (caching) an index during build-time rather than runtime HOT 2
- Typescript with module resolution "NodeNext" + type: "commonjs" fails to import the commonJS module HOT 13
- When there are a lot of markup documents (about 2000). The minisearch search search box is very sluggish, what is the solution? HOT 4
- bm25 and cross-language searching HOT 3
- [Suggestion] Async version of .LoadJSON()? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from minisearch.