Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions db-service/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ const _getSearchableColumns = entity => {
// always ignore virtual elements from search
if(column?.virtual) continue
if (column?.isAssociation || columnName.includes('.')) {
if(!annotationValue)
continue

const ref = columnName.split('.')
if(ref.length > 1) skipDefaultSearchableElements = true
deepSearchCandidates.push({ ref })
Expand Down
10 changes: 10 additions & 0 deletions db-service/test/bookshop/db/search.cds
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ entity CalculatedAddressesWithoutAnno : Addresses {
calculatedAddress : String = street || ' ' || zip || '' || city
}

@cds.search: {calculatedAddress: false}
entity CalculatedAddressesExclude : Addresses {}

@cds.search: {
genre,
books.doesNotExist
Expand All @@ -137,3 +140,10 @@ entity MultipleKeys {
key ID3 : Integer;
text: String;
}

// the following should just lead to the defaults being searched
@cds.search: {author: false}
entity BooksDontSearchAuthor: Books {}

@cds.search: {author.name: false}
entity BooksDontSearchAuthorName: Books {}
38 changes: 38 additions & 0 deletions db-service/test/cqn4sql/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,42 @@ describe('include / exclude logic', () => {
} where search(Books.title, 'x')`
expect(JSON.parse(JSON.stringify(transformed))).to.deep.equal(expected)
})

it('excluding an association should not lead to the association being searched', () => {
let query = cds.ql`SELECT from search.CalculatedAddressesExclude as Address { Address.ID }`
query.SELECT.search = [{ val: 'x' }]

let res = cqn4sql(query, model)
const expected = cds.ql`
SELECT from search.CalculatedAddressesExclude as Address
{
Address.ID
} where search(Address.city, 'x')`

expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
})

it('excluding an association should not lead to the association being searched', () => {
const excludeAuthor = cds.ql`SELECT from search.BooksDontSearchAuthor as Books { ID }`
const excludeAuthorName = cds.ql`SELECT from search.BooksDontSearchAuthorName as Books { ID }`
const defaultSearchableElements = cds.ql`SELECT from search.Books as Books { ID }`
excludeAuthor.SELECT.search = [{ val: 'x' }]
excludeAuthorName.SELECT.search = [{ val: 'x' }]
defaultSearchableElements.SELECT.search = [{ val: 'x' }]

// excluding assocs (or paths) should lead to same result as default searchable elements
const noAuthor = cqn4sql(excludeAuthor, model)
const noAuthorName = cqn4sql(excludeAuthorName, model)
const allDefaults = cqn4sql(defaultSearchableElements, model)
expect(noAuthor.SELECT.where)
.to.deep.equal(noAuthorName.SELECT.where)
.to.deep.equal(allDefaults.SELECT.where)

const expected = cds.ql`
SELECT from search.BooksDontSearchAuthor as Books
{
Books.ID
} where search(Books.title, 'x')`
expect(JSON.parse(JSON.stringify(noAuthor))).to.deep.equal(expected)
})
})