Skip to content
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

feat: config options for fuzzy search #898

Merged
merged 21 commits into from
Nov 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions hana/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,39 @@ const StandardFunctions = {
contains: (...args) => args.length > 2 ? `CONTAINS(${args})` : `(CASE WHEN coalesce(locate(${args}),0)>0 THEN TRUE ELSE FALSE END)`,
concat: (...args) => `(${args.map(a => (a.xpr ? `(${a})` : a)).join(' || ')})`,
search: function (ref, arg) {
const csnElements = ref.element ? [ref] : [...ref.list]
let fuzzyString

// default config
const fuzzyIndex = cds.env.hana?.fuzzy || 0.7

// if column specific value is provided, the configuration has to be defined on column level
if (csnElements.some(e => e.element?.['@Search.ranking'] || e.element?.['@Search.fuzzinessThreshold'])) {
const cols = csnElements.map(e => {
// REVISIT: How to do quoting?
let col = `${e.ref.join('.')} FUZZY`

const rank = e.element?.['@Search.ranking']?.['=']
if(rank === 'HIGH') col += ' WEIGHT 0.8'
else if(rank === 'LOW') col += ' WEIGHT 0.3'
else col += ' WEIGHT 0.5' // MEDIUM

col+= ` MINIMAL TOKEN SCORE ${e.element?.['@Search.fuzzinessThreshold'] || fuzzyIndex}`
col+= " SIMILARITY CALCULATION MODE 'search'"
return col
}).join(',')

fuzzyString = `(${cols})`
} else {
fuzzyString = `${ref} FUZZY MINIMAL TOKEN SCORE ${fuzzyIndex} SIMILARITY CALCULATION MODE 'search'`
}



// REVISIT: remove once the protocol adapter only creates vals
if (Array.isArray(arg.xpr)) arg = { val: arg.xpr.filter(a => a.val).map(a => a.val).join(' ') }
// REVISIT: make this more configurable
return (`(CASE WHEN SCORE(${arg} IN ${ref} FUZZY MINIMAL TOKEN SCORE 0.7 SIMILARITY CALCULATION MODE 'search') > 0 THEN TRUE ELSE FALSE END)`)

return (`(CASE WHEN SCORE(${arg} IN ${fuzzyString}) > 0 THEN TRUE ELSE FALSE END)`)
},

// Date and Time Functions
Expand Down