Search for a Phrase
To search for an exact phrase—a sequence of words in a specific order—do not pass the phrase directly as a plain string to the query. For example, consider the following code:
final String query = "Hippo is the best CMS"; final HstQuery hstQuery = HstQueryBuilder.create(scope) .ofTypes(BaseDocument.class) .where(constraint(".").contains(query)) .build();
By default, HST's query logic splits the input string into individual words and constructs a logical query equivalent to 'Hippo AND is AND the AND best AND CMS'. The resulting query is functionally the same 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();
This approach ignores the order of the words. The query matches documents containing all the specified words, regardless of their sequence.
To search for the exact phrase with the words in the specified order, enclose the phrase in double quotes:
final String phrase = "\"Hippo is the best CMS\""; final HstQuery hstQuery = HstQueryBuilder.create(scope) .ofTypes(BaseDocument.class) .where(constraint(".").contains(phrase)) .build();
By wrapping the phrase in double quotes, the query matches only documents containing the exact phrase "Hippo is the best CMS" in the specified order.