Skip to content

Commit

Permalink
fix(hana): Allow custom fuzzy search cqn (#620)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Hutzel <daniel.hutzel@sap.com>
  • Loading branch information
BobdenOs and danjoa authored May 2, 2024
1 parent d5d5dbb commit 80383f0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ class HANAService extends SQLService {
if (up in caseOperators) break
continue
}
if (cur.func?.toUpperCase() === 'CONTAINS' && cur.args?.length > 2) return true
if ('_internal' in cur) return true
if ('xpr' in cur) return this.is_comparator(cur)
}
Expand Down Expand Up @@ -1255,6 +1256,9 @@ const compareOperators = {
'IS NOT': 1,
'EXISTS': 1,
'BETWEEN': 1,
'CONTAINS': 1,
'MEMBER OF': 1,
'LIKE_REGEXPR': 1,
}

module.exports = HANAService
2 changes: 1 addition & 1 deletion hana/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const StandardFunctions = {
count: x => `count(${x || '*'})`,
countdistinct: x => `count(distinct ${x || '*'})`,
average: x => `avg(${x})`,
contains: (...args) => `(CASE WHEN coalesce(locate(${args}),0)>0 THEN TRUE ELSE FALSE END)`,
contains: (...args) => args.length > 2 ? `CONTAINS(${args})` : `(CASE WHEN coalesce(locate(${args}),0)>0 THEN TRUE ELSE FALSE END)`,
search: function (ref, arg) {
if (!('val' in arg)) throw `HANA only supports single value arguments for $search`
const refs = ref.list || [ref],
Expand Down
1 change: 1 addition & 0 deletions hana/test/fuzzy.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using {sap.capire.bookshop.Books as Books} from '../../test/bookshop/db/schema.cds';
19 changes: 19 additions & 0 deletions hana/test/fuzzy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const cds = require('../../test/cds')

describe('Fuzzy search', () => {
const { expect } = cds.test(__dirname, 'fuzzy.cds')

test('select', async () => {
const { Books } = cds.entities('sap.capire.bookshop')
const res = await SELECT.from(Books).where({
func: 'contains',
args: [
{ list: [{ ref: ['title'] }, { ref: ['descr'] }] },
{ val: 'poem' },
{ func: 'FUZZY', args: [{ val: 0.8 }, { val: 'similarCalculationMode=searchCompare' }] }
]
})

expect(res).to.have.property('length').to.be.eq(1)
})
})

0 comments on commit 80383f0

Please sign in to comment.