Skip to Content
0
Dec 15, 2022 at 10:24 PM

Issues with cds.search

393 Views

Hi,

I am having some issues with CAP Node.js "cds.search" handling. Hoping somebody can help clarify.

First, if I have a view with derived fields, like this, the search will issue a "contains" on the derived field which is not supported.

entity Something { key ID : UUID; prop : String; };
entity MyEntity {key ID : UUID; val : String; toSomething: Association to Something; };
@cds.search: {
   computed: false // Without this I get a query with "CONTAINS((val, computed), 'searchterm')"
}
entity MyProjection as projection on MyEntity {
   *,
   'somecomputedfield' as computed,
};

The database will not run this query, since it resolves to "CONTAINS((..., 'somecomputedfield'), 'searchstring');We can work around this by always adding those cds.search annotations, but hoping it can be solved generically somehow?

Second, if we send a query with aliased fields, like this, it will include the field name at the end of the association into the search, which fails.

cds.run({
  SELECT: {
    from: { ref: ['MyEntity'] },
    columns: [ { ref: ['toSomething', 'prop'] }, as: 'someProp' ],
    search: [ { val: 'searchterm' } ],
  }
}) // This tries to search in the field MyEntity.prop which does not exist

The field gets added to the search in @sap/cds/libx/_runtime/cds-services/services/utils/columns.js, but I cannot see how to avoid it. For now we are patching like this.

diff --git a/node_modules/@sap/cds/libx/_runtime/cds-services/services/utils/columns.js b/node_modules/@sap/cds/libx/_runtime/cds-services/services/utils/columns.js
index da20617..337160d 100644
--- a/node_modules/@sap/cds/libx/_runtime/cds-services/services/utils/columns.js
+++ b/node_modules/@sap/cds/libx/_runtime/cds-services/services/utils/columns.js
@@ -154,8 +154,9 @@ const computeColumnsToBeSearched = (cqn, entity = { __searchableColumns: [] }, a
         const columnName = columnRef[columnRef.length - 1]
         const csnColumn = entity.elements[columnName]
         if (csnColumn) return
-        const col = { ref: [columnName] }
+        var col = { ref: [columnName] }
         if (alias) col.ref.unshift(alias)
+        else col = column
         toBeSearched.push(col)
       }
     })

Thanks in advance!

//Carl