SAP HANA Search and Predictive Functions from ABAP
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Imagine a clerk who types the name "Jon Smyth" into a search box and expects to find the employee "John Smith". A normal database search finds nothing, because the spelling is different. The SAP HANA database, which SAP S/4HANA and the ABAP environment run on, has built-in functions for exactly this: a fault-tolerant search that scores how well a row matches, a text analysis that splits documents into words and names, and a library of statistical algorithms for tasks such as grouping similar customers. This page explains what these functions are, which of them an ABAP program can reach and how (through annotations on a CDS view, or through a CDS table function implemented in SQLScript), and which of them belong to the past. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608), and against the SAP HANA Cloud documentation of QRC 3/2026 and the SAP HANA Platform 2.0 documentation where a function exists only there. Every listing on this page needs an SAP HANA database, so each one is labelled "requires an SAP system; not run here"; the local ABAP playground has no HANA engines.
Before you read
-
ABAP for SAP HANA and Code Pushdown: why work is moved into the database, and the order ABAP SQL, CDS, AMDP that this page follows.
-
ABAP Language Versions and Released APIs: what ABAP Cloud allows and what only classic Standard ABAP allows.
-
ADT Eclipse Setup: CDS views, table functions and AMDP classes are edited only in the ABAP Development Tools.
-
Data Types and Data Objects: structured types, elementary types and the inline declaration used when reading the result.
-
ABAP SQL, ABAP CDS and SAP HANA SQLScript have pages of their own in this section or elsewhere; this page names only the parts it needs.
Key terms
-
Fuzzy search: a search that also returns rows whose text is close to the search term, for example with a typing error.
-
Fuzzy score: a number from 0 to 1 that says how similar a found value is to the search term; 1 means identical.
-
SQLScript: the programming language of the SAP HANA database, an extension of SQL with variables and control flow.
-
AMDP: ABAP Managed Database Procedures, a method of an ABAP class whose body is written in SQLScript and runs inside the database.
-
CDS table function: a CDS entity whose rows are produced by an AMDP method, so that ABAP SQL and other CDS views can read it like a view.
-
Predictive Analysis Library (PAL): a set of ready-made statistical and machine-learning algorithms that run inside SAP HANA and are called from SQLScript.
-
Application function library (AFL): the mechanism through which SAP HANA offers compiled functions, such as PAL, to SQLScript.
-
Text analysis: a database feature that finds words, languages and named entities such as people and places in text.
Where these functions live
All the functions of this page are features of the SAP HANA database, not of the ABAP language. ABAP has no statement
that searches; the search is done by the database, and the ABAP programmer reaches it by one of two routes. On the first
route the programmer does not write any database code: annotations on a CDS view tell the SAP Fiori search box to use the
HANA fuzzy search. On the second route the programmer writes a few lines of SQLScript in an AMDP method, publishes the
result as a CDS table function, and then reads that table function with an ordinary ABAP SQL SELECT. The second route is
also the only way for ABAP to call the text analysis and the predictive library.
The diagram reads from left to right. The program asks; ABAP SQL or the Fiori search hands the question to the database; inside the database the HANA engine does the matching, analysing or calculating, and only the finished rows travel back to the application server. That is the same code pushdown idea as in the page on ABAP for SAP HANA.
What applies where
The routes differ between ABAP Cloud and classic Standard ABAP. Read this table before you copy an example.
| Topic | ABAP Cloud (9.20) | Standard ABAP (8.16), on premise |
|---|---|---|
AMDP database |
SAP HANA only; the class carries the marker interface |
The same. |
Read or write |
The option |
|
Objects outside the ABAP schema (for example |
The addition |
|
|
Available; the annotation is released for key user apps and for SAP BTP. |
Available. |
Tools |
AMDP classes and CDS entities are edited only in ADT. |
The same. |
Source: ABAP CDS - Feature Tables, ABAP Keyword Documentation: AMDP OPTIONS, METHOD, BY DATABASE PROCEDURE, FUNCTION and Search.defaultSearchElement
A direct consequence is that fuzzy search through the @Search annotations is the route for ABAP Cloud. The ABAP CDS
feature table lists CDS table functions with a release for Standard ABAP only (7.50 on premise) and no release for the
SAP BTP ABAP environment or SAP S/4HANA Cloud Public Edition, so check the edition you build for before you plan around a
table function there. Calling PAL or the text analysis, whose procedures live in a database schema outside the ABAP schema,
is a classic Standard ABAP topic and a question for the system owner on a BTP ABAP environment.
Fuzzy search with CONTAINS and SCORE
In SQL a normal comparison asks "is this value equal to that one". A fuzzy search asks "is this value similar to that one, and how similar". The SAP HANA Search Developer Guide describes two parts that work together:
-
The
CONTAINSpredicate in theWHEREclause of aSELECT. Its optionFUZZYtakes a threshold between 0 and 1: a search withFUZZY(0.7)returns every value whose fuzzy score is at least 0.7. The guide adds thatFUZZY(0.0)returns all values with a score greater than 0, never all values. -
The function
SCORE(), which returns the relevance of each found row. The more exact the hit, the higher the score, and a column with a higher weight raises the score of a hit in it.
A search over several columns at once, a freestyle search, puts the column names in brackets, as in
CONTAINS( ( col1, col2 ), 'term', FUZZY( 0.7 ) ). The guide limits the objects CONTAINS can search: a column table
works, and so do in some cases SQL views and joins; row tables, calculation views, analytic views, temporary tables and
SQLScript table variables are not supported.
There are two flavours of fuzzy search, and the second one matters for text such as descriptions. A search on a short value, for example a name in one column, compares the whole value with the search term. A fuzzy text search treats the column as running text, compares word by word and is case-insensitive. In SAP HANA Cloud it needs a fuzzy search index created with the mode text; the guide shows the statement and notes that without the index a substring search finds too much, because it does not respect word boundaries. The following SQL is the guide’s own example of that difference. It requires an SAP system; not run here.
CREATE COLUMN TABLE tab1 ( id INTEGER PRIMARY KEY, txt NVARCHAR(100) );
INSERT INTO tab1 VALUES ( 1, 'SAP HANA database' );
INSERT INTO tab1 VALUES ( 2, 'SAPPHIRE NOW 2021' );
INSERT INTO tab1 VALUES ( 3, 'SAP in-memory database' );
CREATE FUZZY SEARCH INDEX textindex1 ON tab1( txt ) SEARCH MODE TEXT;
SELECT TO_NVARCHAR( SCORE(), '0.00' ) AS score, id, txt
FROM tab1
WHERE CONTAINS( txt, 'sap databse', FUZZY( 0.8, 'searchMode=text,excessTokenWeight=0.02' ) )
ORDER BY SCORE() DESC;
The guide lists rows 1 and 3 as the result, row 1 first, and does not list row 2, "SAPPHIRE NOW 2021", because the word "sap" is not a word of that text. The misspelled "databse" is still matched.
A table that the ABAP Dictionary manages is created and changed through the Dictionary, not with a CREATE statement
typed into the database. How a fuzzy text index is attached to such a table is a question for the system documentation;
the example in the next sections therefore uses the plain fuzzy search on a name column, which needs no extra index.
The easy route: search annotations on a CDS view
When the search box of a Fiori app is all you need, you write no SQLScript at all. The ABAP RESTful Application
Programming Model describes three annotations. With @Search.searchable: true on the view you declare it searchable and
get a standard search field on the user interface. With @Search.defaultSearchElement: true on an element you put it
into the search scope; at least one element must have it, and a view without one is not supported. With
@Search.fuzzinessThreshold you set the least fuzziness for an element, as a number from 0 to 1 whose default is 1, and
with @Search.ranking (#HIGH, #MEDIUM or #LOW) you say how much a hit in that element counts for the ranking. The
documentation recommends starting at 0.7 and tuning it from user feedback. In the Keyword Documentation these annotations
are described as interpreted by Enterprise Search and the service adaptation (SADL), which is why they work for services
and not as a call inside ABAP code.
The following CDS view entity makes the names of employees searchable with a tolerance for typing errors. The table
ZEXP_EMPLOYEE with the columns EMPLOYEE_ID and NAME (the same table as in the Dictionary pages) is an assumption of this page. It requires an SAP system;
not run here.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Employee search'
@Search.searchable: true
define view entity ZEXP_I_EmployeeSearch
as select from zexp_employee
{
key employee_id,
@Search.defaultSearchElement: true
@Search.fuzzinessThreshold: 0.8
@Search.ranking: #HIGH
name as employee_name
}
The programmer route: a CDS table function over an AMDP method
Sometimes the search must run inside ABAP code, for example a duplicate check before a new employee is created. Then you use a CDS table function. It has two halves that you create in this order, and the order matters because the table function must be activated first.
The first half is the definition in CDS. It declares the input parameters, the columns of the result, and names the ABAP
method that fills them. The annotations @ClientHandling.type: #CLIENT_DEPENDENT and @ClientHandling.algorithm:
#SESSION_VARIABLE make the function client-safe: the database restricts the rows to the client of the session by itself,
the function has no client parameter, and its result has a client column as in the Keyword Documentation example. It
requires an SAP system; not run here.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Fuzzy employee search'
@ClientHandling.type: #CLIENT_DEPENDENT
@ClientHandling.algorithm: #SESSION_VARIABLE
define table function ZEXP_TF_EMPLOYEE_SEARCH
with parameters
p_name : abap.char(60)
returns
{
client : abap.clnt;
employee_id : abap.char(10);
employee_name : abap.char(60);
match_score : abap.fltp;
}
implemented by method zcl_exp_employee_search=>search;
Source: ABAP Keyword Documentation: ABAP CDS - Table Functions and ABAP CDS - Client Safety of CDS Table Functions
The second half is the ABAP class. It is an AMDP class, which means it implements the marker interface
IF_AMDP_MARKER_HDB. The method is declared with CLASS-METHODS ... FOR TABLE FUNCTION, with no parameter list of its
own, because the Keyword Documentation generates the interface from the table function: the input parameters keep their
names and the result is a table called result. The statement is allowed only in the public section of the class. The
implementation uses BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT, and the option OPTIONS READ-ONLY is required in
ABAP Cloud. The database tables the SQLScript reads must be listed after USING, and must all be used in the body. The
body is the fuzzy search of the previous section. It requires an SAP system; not run here.
CLASS zcl_exp_employee_search DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
CLASS-METHODS search FOR TABLE FUNCTION zexp_tf_employee_search.
ENDCLASS.
CLASS zcl_exp_employee_search IMPLEMENTATION.
METHOD search BY DATABASE FUNCTION
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING zexp_employee.
RETURN SELECT mandt AS client,
employee_id,
name AS employee_name,
SCORE() AS match_score
FROM zexp_employee
WHERE CONTAINS( name, :p_name, FUZZY( 0.7 ) )
ORDER BY match_score DESC;
ENDMETHOD.
ENDCLASS.
Source: ABAP Keyword Documentation: CLASS-METHODS, FOR TABLE FUNCTION, AMDP - Function Implementations and METHOD, BY DATABASE PROCEDURE, FUNCTION; the shape of the class follows the Keyword Documentation example "Client-Dependent CDS Table Function (Client-safe)"
An ABAP program now reads the table function like a view, passing the search text as a parameter. Before relying on it,
a program may ask the runtime whether the database supports AMDP table functions, as the Keyword Documentation example
does with cl_abap_dbfeatures=>use_features; the pragma ##db_feature_mode acknowledges that the statement needs
such a database feature. It requires an SAP system; not run here.
DATA search_text TYPE c LENGTH 60 VALUE 'Jon Smyth'.
SELECT employee_id, employee_name, match_score
FROM zexp_tf_employee_search( p_name = @search_text )
INTO TABLE @DATA(hits)
##db_feature_mode[amdp_table_function].
LOOP AT hits INTO DATA(hit).
WRITE / |{ hit-employee_id } { hit-employee_name } { hit-match_score }|.
ENDLOOP.
The Keyword Documentation lists restrictions that decide when a table function is the right tool. It works only on a
database that supports AMDP. A transport of a table function moves the CDS entity first and the class later, so between
the two phases the function is not usable. ABAP SQL cannot serve a table function from its in-memory engine, bypasses
the table buffer, and can send at most one internal table per statement to the database when a statement joins a table
function with an internal table through FROM @itab.
Text analysis
Text analysis reads a piece of text and returns what a person would see: which language it is in, its words with their
word class, and the people, places and organisations it mentions. SAP HANA 2.0 offers it through a procedure called
TA_ANALYZE, which accepts one document and a configuration name and returns the annotations. The following call is the
SAP HANA Text Analysis Developer Guide’s own example for the sentence "I love that dirty water, Boston, you’re my home!".
It is SQL for SAP HANA 2.0 and requires an SAP system; not run here.
CALL TA_ANALYZE (
DOCUMENT_TEXT => 'I love that dirty water, Boston, you''re my home!',
LANGUAGE_CODE => ?,
MIME_TYPE => ?,
LANGUAGE_DETECTION => 'DE, EN, KR',
CONFIGURATION => 'LINGANALYSIS_FULL',
RETURN_PLAINTEXT => 0,
TA_ANNOTATIONS => ?,
PLAINTEXT => ? );
This is where the age of the documentation matters. The SAP HANA Cloud Migration Guide states that fulltext indexes, their
system views and built-in functions, and several built-in procedures, among them TA_ANALYZE, are not available in
SAP HANA Cloud. Applications that use them must be redesigned, and the fuzzy search index with the mode text takes the
place of the fulltext index for searching. So on a system whose database is SAP HANA Cloud, which includes the ABAP
environment on SAP BTP, do not plan on TA_ANALYZE. On an on-premise SAP S/4HANA with SAP HANA 2.0 the procedure exists, and an AMDP in Standard ABAP is the
way to reach it, subject to the USING rules described above.
The Predictive Analysis Library
The Predictive Analysis Library (PAL) is a library of algorithms that run inside the database so that large tables do
not have to travel to the application server. Its reference describes ten categories: clustering, classification,
regression, association, time series, preprocessing, statistics, social network analysis, recommender system and
miscellaneous. The reference’s own example for the cost of doing it elsewhere is a cluster analysis of a customer table
with a trillion records. PAL is an application function library, which is a collection of functions that SQLScript calls
as procedures. It is present in SAP HANA Cloud, where the script server must be enabled and users need the database role
AFL__SYS_AFL_AFLPAL_EXECUTE; there the clause WITH OVERVIEW of CALL is not supported.
A PAL call always has the same shape. The procedures live in the database schema _SYS_AFL and their names start with
PAL_. You pass an input table with the data, a parameter table with the settings, and receive the results in output
tables. The parameter table has the columns PARAM_NAME, INT_VALUE, DOUBLE_VALUE and STRING_VALUE, and the
reference recommends a local temporary column table for it so that two sessions do not collide. The next listing is
shortened from the reference’s k-means example: it groups the rows of a table into four clusters. It is SQLScript and
requires an SAP system; not run here.
CREATE LOCAL TEMPORARY COLUMN TABLE #PAL_PARAMETER_TBL (
"PARAM_NAME" NVARCHAR(256),
"INT_VALUE" INTEGER,
"DOUBLE_VALUE" DOUBLE,
"STRING_VALUE" NVARCHAR(1000) );
INSERT INTO #PAL_PARAMETER_TBL VALUES ( 'GROUP_NUMBER', 4, NULL, NULL );
INSERT INTO #PAL_PARAMETER_TBL VALUES ( 'MAX_ITERATION', 100, NULL, NULL );
CALL _SYS_AFL.PAL_KMEANS( PAL_4_COLUMN_DATA_TBL, "#PAL_PARAMETER_TBL", ?, ?, ?, ?, ? );
Source: SAP HANA Predictive Analysis Library (PAL): Calling PAL Procedures and SAP HANA Cloud Predictive Analysis Library (PAL)
To make an ABAP program use such a call, the SQLScript goes into an AMDP method, usually a procedure
(BY DATABASE PROCEDURE) that takes the data as an input table and returns the clusters in an output table. One rule of
the Keyword Documentation shapes the method: after USING you may list only objects that ABAP manages, such as
Dictionary tables, CDS entities and other AMDP methods. The procedures of _SYS_AFL are not such objects, and in
Standard ABAP the addition USING SCHEMA is the documented way to name objects of a mapped database schema; in ABAP
Cloud that addition is not available. The practical advice for a beginner is to treat PAL from ABAP as a
classic Standard ABAP, on-premise topic, and to ask the database administrator that AFL is installed and your database
user may execute it before you write any code.
Decision tables: legacy only
A decision table in SAP HANA is a table of rules in which each row says "if these conditions hold, then do this". It was
modelled in SAP HANA Studio as a design-time object with the file ending .hdbruldec. SAP’s modelling guide offers a
migration that turns a decision table into a procedure, a table function and related objects, and the XS advanced
migration guide lists decision tables, together with AFL models, among the deprecated artifact types that you convert by
hand before moving an application to XS advanced or to SAP HANA Cloud: to a graphical calculation view or to an equivalent
SQL artifact. For new ABAP work do not start with a decision table. Keep the rules in an ordinary database table and read
it with ABAP SQL or CDS, or write them as ABAP code, and treat a decision table as something to replace when you meet it
in an old system.
What older books show, and what has changed
The book ABAP Development for SAP HANA (first edition, 2014) from SAP PRESS was written for the SAP HANA 2.0 of its time, when fulltext indexes, TA_ANALYZE and decision tables were the normal tools. Three things have moved on. Fulltext indexes and TA_ANALYZE do not exist in SAP HANA Cloud, and the
fuzzy search index with the mode text replaces them. Decision tables are deprecated. And ABAP Cloud limits AMDP to
read-only methods with no USING SCHEMA, so some of what the book does with database procedures is possible only in
classic Standard ABAP. The book’s publisher page is the source for its edition: ABAP Development for SAP HANA at SAP PRESS (the page lists the successor edition).
Summary
-
The search, text analysis and predictive functions of this page are features of the SAP HANA database that an ABAP program reaches through annotations or through AMDP; ABAP has no search statement of its own.
-
Fuzzy search uses
CONTAINS( column, term, FUZZY( threshold ) )andSCORE(); the score is a number from 0 to 1 and a higher threshold means a stricter match. -
For a Fiori search box, annotate a CDS view entity with
@Search.searchable,@Search.defaultSearchElement,@Search.fuzzinessThresholdand@Search.ranking; for a search inside ABAP code, wrap the SQLScript in an AMDP method and expose it as a CDS table function that aSELECTreads (on Standard ABAP; the feature table lists no release of table functions for the BTP ABAP environment and the Public Edition). -
ABAP Cloud allows read-only AMDP and no
USING SCHEMA; calls of the predictive library, whose procedures live in_SYS_AFL, are a classic Standard ABAP topic. -
TA_ANALYZEand fulltext indexes are not available in SAP HANA Cloud; decision tables are deprecated.
References
-
ABAP Keyword Documentation: ABAP Managed Database Procedures (AMDP)
-
ABAP Keyword Documentation: [CLASS-METHODS, AMDP OPTIONS]
-
ABAP Keyword Documentation: CLASS-METHODS, FOR TABLE FUNCTION
-
ABAP Keyword Documentation: METHOD, BY DATABASE PROCEDURE, FUNCTION
-
ABAP Keyword Documentation: ABAP CDS - Client Safety of CDS Table Functions
-
ABAP RESTful Application Programming Model: Enabling Text and Fuzzy Searches in SAP Fiori Apps
-
SAP HANA Cloud Search Developer Guide: The CONTAINS() Predicate
-
SAP HANA Cloud Search Developer Guide: Migrating From Fulltext Indexes
-
SAP HANA Cloud Getting Started Guide: Using the Machine Learning Libraries
-
SAP HANA Predictive Analysis Library (PAL): Calling PAL Procedures
-
SAP HANA Text Analysis Developer Guide: Performing Linguistic Analysis Using TA_ANALYZE
-
SAP HANA Cloud Migration Guide: Migrate an XS Classic Application