Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion db-service/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,18 @@ const _getSearchableColumns = entity => {
if (annotatedColumnValue) return true

// calculated elements are only searchable if requested through `@cds.search`
if(column.value) return false
if (column.value) return false

// if at least one element is explicitly annotated as searchable, e.g.:
// `@cds.search { element1: true }` or `@cds.search { element1 }`
// and it is not the current column name, then it must be excluded from the search
if (atLeastOneColumnIsSearchable) return false

if (element?.['@Common.Text']?.['='] && element?.isAssociation) {
deepSearchCandidates.push({ ref: element['@Common.Text']['='].split('.') })
return false
}

Comment on lines +104 to +108

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move that out to a model pre-processor, that runs once on server start instead of polluting our db layers with such knowledge of OData / Fiori concepts , and doing that again and again per query at runtime

// the element is considered searchable if it is explicitly annotated as such or
// if it is not annotated and the column is typed as a string (excluding elements/elements expressions)
return (
Expand Down
2 changes: 1 addition & 1 deletion test/bookshop/db/data/sap.capire.bookshop-Books.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ID;title;descr;author_ID;stock;price;currency_code;genre_ID
201;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";101;12;11.11;GBP;11
201;Wuthering Heights;"Wuthering Heights was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre.";101;12;11.11;GBP;11

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change these texts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted the word "Emily" from the description, because I couldn't find any data for a test, where the result of the search would be different enough for search columns Books.title, Books.descr and for Books.title, Books.descr, Author.name(placeOfBirth/placeOfDeath).

207;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";107;11;12.34;GBP;11
251;The Raven;"""The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";150;333;13.13;USD;16
252;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";150;555;14;USD;15
Expand Down
1 change: 1 addition & 0 deletions test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ entity Books : managed {
key ID : Integer;
title : localized String(111);
descr : localized String(1111);
@Common.Text : author.name
author : Association to Authors;
genre : Association to Genres default 10;
stock : Integer;
Expand Down
7 changes: 7 additions & 0 deletions test/scenarios/bookshop/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe.skip('Bookshop - Search', () => {
}
})

test('Search books and author.name', async () => {
const res = await GET('/admin/Books?$search="Emily"', admin)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
expect(res.data.value[0].title).to.be.eq('Wuthering Heights')
})

test('Search authors via books', async () => {
const { Books } = cds.entities
// ad-hoc search expression
Expand Down