-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: config options for fuzzy search (#898)
Co-authored-by: Bob den Os <[email protected]>
- Loading branch information
1 parent
45bcdcf
commit f6593e6
Showing
8 changed files
with
203 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
using {sap.capire.bookshop.Books as Books} from '../../test/bookshop/db/schema.cds'; | ||
using {sap.capire.bookshop.BooksAnnotated as BooksAnnotated} from '../../test/bookshop/db/schema.cds'; | ||
|
||
annotate BooksAnnotated with @cds.search: {title, descr, currency.code}; | ||
annotate BooksAnnotated:title with @(Search.ranking: HIGH, Search.fuzzinessThreshold: 0.9); | ||
annotate BooksAnnotated:descr with @(Search.ranking: LOW, Search.fuzzinessThreshold: 0.9); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,78 @@ | ||
const cds = require('../../test/cds') | ||
|
||
describe('Fuzzy search', () => { | ||
describe('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' }] } | ||
] | ||
beforeEach (() => { | ||
delete cds.env.hana.fuzzy | ||
}) | ||
|
||
describe('fuzzy', () => { | ||
test('default', async () => { | ||
const { Books } = cds.entities('sap.capire.bookshop') | ||
const cqn = SELECT.from(Books).search('"autobio"').columns('1') | ||
const {sql} = cqn.toSQL() | ||
expect(sql).to.include('FUZZY MINIMAL TOKEN SCORE 0.7') | ||
const res = await cqn | ||
expect(res.length).to.be(2) // Eleonora and Jane Eyre | ||
}) | ||
|
||
//HCE returns different result than HXE | ||
test.skip('multiple search terms', async () => { | ||
const { Books } = cds.entities('sap.capire.bookshop') | ||
const cqn = SELECT.from(Books).search('"autobio" "jane"').columns('1') | ||
const {sql, values} = cqn.toSQL() | ||
expect(sql).to.include('FUZZY MINIMAL TOKEN SCORE 0.7') | ||
expect(values[0]).to.eq('"autobio" "jane"') // taken as is | ||
const res = await cqn | ||
expect(res.length).to.be(2) // Eleonora and Jane Eyre | ||
}) | ||
|
||
test('global config', async () => { | ||
cds.env.hana.fuzzy = 1 | ||
const { Books } = cds.entities('sap.capire.bookshop') | ||
const cqn = SELECT.from(Books).search('"autobio"').columns('1') | ||
const {sql} = cqn.toSQL() | ||
expect(sql).to.include('FUZZY MINIMAL TOKEN SCORE 1') | ||
const res = await cqn | ||
expect(res.length).to.be(2) // Eleonora and Jane Eyre | ||
}) | ||
|
||
expect(res).to.have.property('length').to.be.eq(1) | ||
test('annotations', async () => { | ||
const { BooksAnnotated } = cds.entities('sap.capire.bookshop') | ||
const cqn = SELECT.from(BooksAnnotated).search('"first-person"').columns('1') | ||
const {sql} = cqn.toSQL() | ||
expect(sql).to.include('title FUZZY WEIGHT 0.8 MINIMAL TOKEN SCORE 0.9') | ||
expect(sql).to.include('code FUZZY WEIGHT 0.5 MINIMAL TOKEN SCORE 0.7') | ||
expect(sql).to.include('descr FUZZY WEIGHT 0.3 MINIMAL TOKEN SCORE 0.9') | ||
|
||
const res = await cqn | ||
expect(res.length).to.be(1) // jane eyre | ||
}) | ||
}) | ||
|
||
describe('like', () => { | ||
beforeEach (() => cds.env.hana.fuzzy = false) | ||
test('fallback - 1 search term', async () => { | ||
const { Books } = cds.entities('sap.capire.bookshop') | ||
const cqn = SELECT.from(Books).search('"autobio"').columns('1') | ||
const {sql} = cqn.toSQL() | ||
// 5 columns to be searched createdBy, modifiedBy, title, descr, currency_code | ||
expect(sql.match(/(like)/g).length).to.be(5) | ||
const res = await cqn | ||
expect(res.length).to.be(2) // Eleonora and Jane Eyre | ||
}) | ||
|
||
test('fallback - 2 search terms', async () => { | ||
const { Books } = cds.entities('sap.capire.bookshop') | ||
const cqn = SELECT.from(Books).search('"autobio"', '"Jane"').columns('1') | ||
const {sql, values} = cqn.toSQL() | ||
// 5 columns to be searched createdBy, modifiedBy, title, descr, currency_code | ||
expect(sql.match(/(like)/g).length).to.be(10) | ||
expect(values).to.include('%autobio%') | ||
expect(values).to.include('%jane%') | ||
const res = await cqn | ||
expect(res.length).to.be(1) // Jane Eyre | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters