SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Add full-text search queries with Hibernate

Leverage the full-text search capabilities of SAP HANA through Hibernate.

Overview

🎓 intermediate 20 min. SAP Hana, Express EditionIntermediateJavaSAP HANAExpress Edition

You will learn

  • โœ”How to search for text using SAP HANA’s full-text search capabilities
  • โœ”How to use term mappings to improve the search results
Unknown U Unknown November 1, 2022
Created by August 14, 2018
Contributors

Prerequisites

Steps

Motivation

The application’s address search functionality based on SQL LIKE queries has several drawbacks

Intro

  • Only exact matches are returned. A typo in the address causes the result list to be empty.
  • There is no ordering of the results, for example, by best matching address.
  • Street abbreviations like ‘rd’, ‘ct’, etc. need to be used. Searches for ‘road’ or ‘court’ don’t return any results.

These drawbacks can be addressed by using full-text search.


Step 1 Use full-text search for the address search
โ€”

Open the file AddressRepository.java from the directory src/main/java/com/sap/hana/hibernate/sample/repositories.

The current implementation uses a standard LIKE query to retrieve matching addresses

Java
public List<Address> findByAddressContaining(String address) {
  Query query = this.em
      .createQuery( "select a from Address a where upper(a.address) like upper(:address)", Address.class );

  query.setParameter( "address", "%" + address + "%" );

  query.setMaxResults( 20 );

  List<Address> addresses = query.getResultList();

  return addresses;
}

As there is no Hibernate support for SAP HANA’s full-text search capabilities you’ll need to change the query to a native SQL query. Then you can use the CONTAINS predicate to perform a full-text search on the address table.

Java
public List<Address> findByAddressContaining(String address) {
  Query query = this.em
      .createNativeQuery(
            "select a.base_id, a.address, a.address_number, a.address_number_suffix, a.cnn, a.street_name, a.street_type, a.x, a.y, a.zip_code, a.location "
          + "from address a "
          + "where CONTAINS(a.address, :address, FUZZY(0.7, 'similarCalculationMode=searchCompare, textSearch=compare')) "
          + "order by SCORE() desc "
          + "limit 20",
          Address.class );

  query.setParameter( "address", address );

  List<Address> addresses = query.getResultList();

  return addresses;
}

The CONTAINS predicate used in the query matches the input from the :address parameter against the address column of the address table using full-text search. It then calculates a similarity score for each row. If the similarity score is at least 0.7 as specified in the FUZZY option, the value is included in the result set.

The similarity score is also used to sort the result, so the best matches are returned at the top of the list.

You can find more information about the CONTAINS predicate and fuzzy search in the SAP HANA Search Developer Guide.

Note that the address parameter doesn’t need the surrounding % characters any more.

Since the query is a native query now, query.setMaxResults() can’t be used any more. Instead, the limit is included in the query via a limit clause.

Save the AddressRepository.java file.

Step 2 Deploy the application
+
Step 3 Learn about term mappings
+
Step 4 Add term mappings for synonymous search terms
+
Step 5 Configure the address search to use the term mappings
+
Step 6 Deploy the application
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 6
1. Use full-text search for the address search 2. Deploy the application 3. Learn about term mappings 4. Add term mappings for synonymous search terms 5. Configure the address search to use the term mappings 6. Deploy the application

Learn more →