The descriptions in this topic refer to the range of functions for text and fuzzy searches that are provided in the context of SAP HANA.
The full text searching (or just text search) provides you with the capability to identify naturallanguage terms that satisfy a query and, optionally, to sort them by relevance (ranking) to the query. The most common type of search is to find texts that contain the term specified and return them in the order of their similarity to these terms.
Fuzzy search is a fast and fault-tolerant search feature of SAP HANA. The basic concept behind the fault-tolerant search is that a database query returns records even if the search term (user input) contains additional or missing characters, or even spelling errors. Fuzzy search can be used in various applications -- for example, to trigger a fault-tolerant search in a structured database content, like a search for a product called 'coffe krisp biscuit' and you find 'Toffee Crisp Biscuits'.
As an application developer however, you must ensure that your CDS views are suitable for text and fuzzy search enabling. For more information take a look at the corresponding topics in the SAP HANA Developer Guide.
As the name suggests, search annotations enable the search feature on the CDS view elements.
First of all, you need the following CDS annotation at the view level:
Annotation and Value |
Effect |
---|---|
@Search.searchable: true/false | Defines whether a CDS view is generally relevant for search scenarios. This annotation provides a general switch and a means to quickly detect whether a view is search-relevant or not. Set to value true in order to enable search support by means of @Search annotations. Here, at least one view field must be defined as @defaultSearchElement at element level. |
The annotations (required) at the element level are:
Annotation and Values |
Effect |
---|---|
@Search.defaultSearchElement: true/false | Specifies that the annotated element is to be considered in a full-text search Note
At least one element has to be defined for the default full-text search. Searching in views without default full-text search elements is not supported! All view elements that are annotated for the default
search define the search scope. (The search will be performed on
all elements that have this annotation.).
Caution
Such a search must not operate on all elements – for performance reasons and because not all elements qualify for this kind of access. |
@Search.fuzzinessThreshold: <value> | This annotation specifies the least level of fuzziness the element has to have in
order to be considered in a fuzzy search at all. The <value> defines the threshold for a fuzzy search (how fuzzy scores are calculated when comparing two strings or two terms). Possible values are: 0..1 The default value is 1. The fuzzy search algorithm calculates a fuzzy score for each string comparison. The higher the score, the more similar the strings are. A score of 1.0 means the strings are identical. A score of 0.0 means the strings have nothing in common. |
@Search.ranking: <value> | This annotation specifies how relevant the values of an element (view field) are for
ranking, should the freestyle search terms match the element’s
value. The ranking can have the following values:
|
The listing below implements a search model for searching products. The model definition results from a join between two data sources that already specify the persistence layer for searching: both database tables are part of the EPM data model, where SMWD_PD provides product data and the table SMWD_TEXTS serves as the text reference table.
The annotation @Search.searchable: true marks the view as searchable. In addition, the elements Name and Category are annotated with @Search.defaultSearchElement: true. This means that a freestyle search is enabled on the search UI where it is possible to search for the annotated elements. The annotation @Search.fuzzinessThreshold: 0.7 (0.8) defines that the text search should be applied to the element Category with a similarity value of 70% and to the element Name with a similarity value of 80%.
... @Search.searchable : true define view SearchForPuducts as select from snwd_pd as Product left outer join snwd_texts as ProductDescription on Product.desc_guid = ProductDescription.node_key { key Product.product_id as ID, @Search.defaultSearchElement : false ProductDescription.language as Language, @Search.defaultSearchElement : true @Search.fuzzinessThreshold : 0.8 @Search.ranking : #HIGH ProductDescription.text as Name, @Search.defaultSearchElement : true @Search.fuzzinessThreshold : 0.7 @Search.ranking : #LOW Product.category as Category }
Preview: