This article covers a Bloomreach Experience Manager version 11. There's an updated version available that covers our most recent release.

Search for a Phrase

When searching for a phrase (a number of words in a specific sequence) you might be tempted to write something like

final String query = "Hippo is the best CMS";
final HstQuery hstQuery = HstQueryBuilder.create(scope)
        .ofTypes(BaseDocument.class)
        .where(constraint(".").contains(query))
        .build();

However, HST's querying logic extracts all words from your query string, translating it to a logical query of 'Hippo AND is AND the AND best AND CMS', which could also be coded as:

final HstQuery hstQuery = HstQueryBuilder.create(scope)
        .ofTypes(BaseDocument.class)
        .where(
                and(
                        constraint(".").contains("Hippo")
                        constraint(".").contains("is")
                        constraint(".").contains("the")
                        constraint(".").contains("best")
                        constraint(".").contains("CMS")
                )
        )
        .build();

In the above query, the ordering of the words of the phrase is gone. The query will produce results with all of these words present in arbitrary order. When you worry about the word order and really look for a specific phrase, put your phrase between double quotes:

final String phrase = "\"Hippo is the best CMS\"";
final HstQuery hstQuery = HstQueryBuilder.create(scope)
        .ofTypes(BaseDocument.class)
        .where(constraint(".").contains(phrase))
        .build();

 

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?