Delivery Tier Fluent Search API

Dear Community,

Currently Im follow up this documentation

so I create the below classes

  1. ref:
    Define Configuration Parameters for Delivery Tier Components or REST Services - Bloomreach Experience Manager (PaaS/Self-Hosted) - The Fast and Flexible Headless CMS

public interface SearchInfo {
@Parameter(name = “pageSize”,
defaultValue = “10”,
displayName = “Page Size”)
int getPageSize();

@Parameter(name = “documentType”,
defaultValue = “bloomreach:basedocument”,
displayName = “Document Type”)
String getDocumentType();
}

  1. Delivery Tier Fluent Search API - Bloomreach Experience Manager (PaaS/Self-Hosted) - The Fast and Flexible Headless CMS
@ParametersInfo(type = SearchInfo.class)

public class MySearchComponent extends BaseHstComponent {

@Override
public void doBeforeRender(final HstRequest request, final HstResponse response)
throws HstComponentException {
HstRequestContext requestContext = request.getRequestContext();
SearchInfo info = getComponentParametersInfo(request);
// the scope to search below, for example /content/documents/myproject
HippoBean scope = requestContext.getSiteContentBaseBean();

try {
// parse a free text query to remove invalid chars. The argument
// ‘false’ means no wildcards allowed
String query = getPublicRequestParameter(request, “q”);
String parsedQuery = SearchInputParsingUtils.parse(query, false);

   int pageSize = NumberUtils.toInt(getPublicRequestParameter(request, "ps"), 10);
   int pageNum = NumberUtils.toInt(getPublicRequestParameter(request, "pn"), 1);

   // create the query to search below 'scope', return beans that are
   // of type BaseDocument bean or a subclass/sub-jcr-types, the
   // third argument, 'true', indicates whether to include subtypes
   HstQuery hstQuery = HstQueryBuilder.create(scope)
            .ofTypes(BaseDocument.class)
            .where(constraint(".").contains(parsedQuery))
            .limit(pageSize)
            .offset(pageSize * (pageNum - 1))
            .orderByDescending("bloomreach:date")
            .build();

    // execute the query
    HstQueryResult result = hstQuery.execute();

    // set the result, info and parsedQuery on the HstRequest : It is
    // then available in the JSP
    request.setAttribute("result", result);
    request.setAttribute("info", info);
    request.setAttribute("query", parsedQuery);

} catch (QueryException e) {
    throw new HstComponentException(
     "Exception occured during creation or execution of HstQuery.", e);
}

}

  1. ftl
    Render a Search Query Result - Bloomreach Experience Manager (PaaS/Self-Hosted) - The Fast and Flexible Headless CMS

<#include “/WEB-INF/freemarker/include/imports.ftl”>

${info.title?html} for '${query?html}': ${result.totalSize} results

    <#-- Iterate through the hippoBeans on the result --> <#if result?? && result.hippoBeans?has_content> <#list result.hippoBeans as item> <@hst.link var="link" hippobean=item />
  • <@hst.cmseditlink hippobean=item/> ${item.title?html}
    <#if item.date?? && item.date.time??>

    <@fmt.formatDate value=item.date.time type="Date" pattern="MMMM d, yyyy h:mm a"/>

    ${item.summary?html}

So when I run
localhost:9080/site/search

Its that expected result???

Thanks in advance

@andorian
The search component you’re creating expects a q parameter with the search term. For example, http://localhost:8080/site/search?q=search-term

The two lines below that you have in the component take the query parameter and parse it:

String query = getPublicRequestParameter(request, “q”);
String parsedQuery = SearchInputParsingUtils.parse(query, false);

Then the hstQuery uses the parsedQuery in the where contstraint

Hi Adam,

Thank you for your answer

Should I add a form to display the response?

When i send the request
http://localhost:8080/site/search?q=search-term
I see the same message " Please fill in a search term"

Is there an example.how to test this??

@andorian
Hello,

Below is a link to the Essentials Search Component freemarker templates. There are 2 templates:

  1. A search box to submit a search term to the search page
  2. The search results page to render the search results

Thank you!