This sample adds functionality for searching for blog entries based on the caption value of its blog image. You can demo this sample by completing the following steps:
-
Add a blog entry with an image.
-
After saving the new blog entry, enter a value for the image's caption.
-
Search for the entry in the search bar by using the caption value. The matching blog entry is displayed in the search results.
This sample leverages the KeywordQueryContributor API.
This sample conveys the recommended approach to adding a field, which may contribute to the relevance of the search, in keyword queries.
To achieve this goal, you must create a component that implements the
com.liferay.portal.search.spi.model.query.contributor.KeywordQueryContributor
.
Specifically, you must implement the contribute
method, which is invoked when
a user enters something in the search bar:
@Override
public void contribute(
String keywords, BooleanQuery booleanQuery,
KeywordQueryContributorHelper keywordQueryContributorHelper) {
SearchContext searchContext =
keywordQueryContributorHelper.getSearchContext();
queryHelper.addSearchLocalizedTerm(
booleanQuery, searchContext, Field.CAPTION, true);
}
This method uses Declarative Services to get a reference for the QueryHelper
to invoke the addSearchLocalizedTerm
method.
Also, it is important to highlight the @Component
annotation that registers a
new service to OSGi:
@Component(
immediate = true,
property = "indexer.class.name=com.liferay.calendar.model.Calendar",
service = KeywordQueryContributor.class
)
You can implement similar functionality by using a ModelPreFilterContributor
.
To help you make an informed decision between implementing a
KeywordQueryContributor
or a ModelPreFilterContributor
, consider these below
items:
- Filters are cached and don't influence the score; therefore, they're faster than queries.
- A query is usually something unpredictable that the user types, while filters help users narrow down search results. For example, using facets.
For more information, read Elasticsearch's documentation.