Git Product home page Git Product logo

Comments (2)

farzher avatar farzher commented on June 18, 2024 1

how to add custom weight to each key

the readme shows how to do weighted searches. you write a scoreFn that works however you want.

let objects = [{title:'Favorite Color', desc:'Chrome'}, {title:'Google Chrome', desc:'Launch Chrome'}]
let results = fuzzysort.go('chr', objects, {
  keys: ['title', 'desc'],
  // Create a custom combined score to sort by. -100 to the desc score makes it a worse match
  scoreFn: scores => {
    const titlescore = scores[0]?.score       || -1000
    const descscore  = scores[1]?.score - 100 || -1000
    return Math.max(titlescore, descscore)
  }
})

var bestResult = results[0]
// When using multiple `keys`, results are different. They're indexable to get each normal result
fuzzysort.highlight(bestResult[0]) // 'Google <b>Chr</b>ome'
fuzzysort.highlight(bestResult[1]) // 'Launch <b>Chr</b>ome'
bestResult.obj.title // 'Google Chrome'

how to search through these nested tags?

idk, you can't do that. combining all the tags into a single combined_tags string probably works fine

let objects = [
  {title:'Favorite Color', desc:'Chrome'},
  {title:'Google Chrome', desc:'Launch Chrome', tags:[{title:'browser'}, {title:'program'}]}
]

// combine optional tags into a single string we can search
for(const obj of objects) obj.combined_tags = obj.tags?.map(t => t.title).join(' ')

let results = fuzzysort.go('chr', objects, {
  keys: ['title', 'desc', 'combined_tags'],
  scoreFn: scores => {
    const titlescore = scores[0]?.score       || -1000
    const descscore  = scores[1]?.score - 100 || -1000
    const tagscore   = scores[2]?.score - 100 || -1000
    return Math.max(titlescore, descscore, tagscore)
  }
})
console.log(results)

from fuzzysort.

farzher avatar farzher commented on June 18, 2024

this all now just works in fuzzysort v3

let items = [{title: 'sup', description: 'not much', tags: ['funny']}]
fuzzysort.go('u', items, {
  keys: ['title', 'description', obj => obj.tags?.join()],
  scoreFn: r => r[0].score * 1.00 + r[1].score * .60 + r[2].score * .40,
})
``

from fuzzysort.

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.