Giter VIP home page Giter VIP logo

Comments (4)

subsetpark avatar subsetpark commented on June 9, 2024 1

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.

lucaong avatar lucaong commented on June 9, 2024

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.

lucaong avatar lucaong commented on June 9, 2024

Here's a pull request introducing queryTerms to search results.

from minisearch.

lucaong avatar lucaong commented on June 9, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.