Search in sources :

Example 1 with Search

use of com.adobe.cq.wcm.core.components.models.Search in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SearchImplTest method testSearchProperties.

@Test
void testSearchProperties() {
    Search search = getSearchUnderTest(SEARCH_PAGE + "/jcr:content/search");
    assertEquals(10, search.getResultsSize());
    assertEquals(3, search.getSearchTermMinimumLength());
    assertEquals("/jcr:content/search", search.getRelativePath());
    assertEquals("core/wcm/components/search/v1/search", search.getExportedType());
    assertEquals("/content/en/search", search.getSearchRootPagePath());
    assertSame(search.getSearchRootPagePath(), search.getSearchRootPagePath());
    Utils.testJSONExport(search, Utils.getTestExporterJSONPath(TEST_BASE, "search"));
}
Also used : Search(com.adobe.cq.wcm.core.components.models.Search) Test(org.junit.jupiter.api.Test)

Example 2 with Search

use of com.adobe.cq.wcm.core.components.models.Search in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SearchImplTest method testSearchProperties_noPath.

@Test
void testSearchProperties_noPath() {
    Search search = getSearchUnderTest(SEARCH_PAGE_DE + "/jcr:content/search");
    assertEquals(10, search.getResultsSize());
    assertEquals(3, search.getSearchTermMinimumLength());
    assertEquals("/jcr:content/search", search.getRelativePath());
    assertEquals("core/wcm/components/search/v1/search", search.getExportedType());
    assertEquals(SEARCH_PAGE_DE, search.getSearchRootPagePath());
    assertSame(search.getSearchRootPagePath(), search.getSearchRootPagePath());
    Utils.testJSONExport(search, Utils.getTestExporterJSONPath(TEST_BASE, "search2"));
}
Also used : Search(com.adobe.cq.wcm.core.components.models.Search) Test(org.junit.jupiter.api.Test)

Example 3 with Search

use of com.adobe.cq.wcm.core.components.models.Search in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SearchResultServlet method getResults.

/**
 * Gets the search results.
 *
 * @param request The search request.
 * @param searchComponent The search component.
 * @param pageManager A PageManager.
 * @return List of search results.
 */
@NotNull
private List<ListItem> getResults(@NotNull final SlingHttpServletRequest request, @NotNull final Search searchComponent, @NotNull final PageManager pageManager) {
    List<ListItem> results = new ArrayList<>();
    String fulltext = request.getParameter(PARAM_FULLTEXT);
    if (fulltext == null || fulltext.length() < searchComponent.getSearchTermMinimumLength()) {
        return results;
    }
    long resultsOffset = Optional.ofNullable(request.getParameter(PARAM_RESULTS_OFFSET)).map(Long::parseLong).orElse(0L);
    Map<String, String> predicatesMap = new HashMap<>();
    predicatesMap.put(FulltextPredicateEvaluator.FULLTEXT, fulltext);
    predicatesMap.put(PathPredicateEvaluator.PATH, searchComponent.getSearchRootPagePath());
    predicatesMap.put(TypePredicateEvaluator.TYPE, NameConstants.NT_PAGE);
    PredicateGroup predicates = PredicateConverter.createPredicates(predicatesMap);
    ResourceResolver resourceResolver = request.getResource().getResourceResolver();
    Query query = queryBuilder.createQuery(predicates, resourceResolver.adaptTo(Session.class));
    if (searchComponent.getResultsSize() != 0) {
        query.setHitsPerPage(searchComponent.getResultsSize());
    }
    if (resultsOffset != 0) {
        query.setStart(resultsOffset);
    }
    SearchResult searchResult = query.getResult();
    LinkHandler linkHandler = request.adaptTo(LinkHandler.class);
    // Query builder has a leaking resource resolver, so the following work around is required.
    ResourceResolver leakingResourceResolver = null;
    try {
        Iterator<Resource> resourceIterator = searchResult.getResources();
        while (resourceIterator.hasNext()) {
            Resource resource = resourceIterator.next();
            // Get a reference to QB's leaking resource resolver
            if (leakingResourceResolver == null) {
                leakingResourceResolver = resource.getResourceResolver();
            }
            Optional.of(resource).map(res -> resourceResolver.getResource(res.getPath())).map(pageManager::getContainingPage).map(page -> new PageListItemImpl(linkHandler, page, searchComponent.getId(), null)).ifPresent(results::add);
        }
    } finally {
        if (leakingResourceResolver != null) {
            leakingResourceResolver.close();
        }
    }
    return results;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) TypePredicateEvaluator(com.day.cq.search.eval.TypePredicateEvaluator) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SearchResult(com.day.cq.search.result.SearchResult) PredicateConverter(com.day.cq.search.PredicateConverter) Map(java.util.Map) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) Search(com.adobe.cq.wcm.core.components.models.Search) PN_FRAGMENT_VARIATION_PATH(com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH) PathPredicateEvaluator(com.day.cq.search.eval.PathPredicateEvaluator) Session(javax.jcr.Session) Servlet(javax.servlet.Servlet) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Query(com.day.cq.search.Query) Optional(java.util.Optional) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) NotNull(org.jetbrains.annotations.NotNull) FulltextPredicateEvaluator(com.day.cq.search.eval.FulltextPredicateEvaluator) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) HashMap(java.util.HashMap) LanguageManager(com.day.cq.wcm.api.LanguageManager) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) StreamSupport(java.util.stream.StreamSupport) Activate(org.osgi.service.component.annotations.Activate) SearchImpl(com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl) QueryBuilder(com.day.cq.search.QueryBuilder) Iterator(java.util.Iterator) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) LocalizationUtils(com.adobe.cq.wcm.core.components.internal.LocalizationUtils) PredicateGroup(com.day.cq.search.PredicateGroup) Template(com.day.cq.wcm.api.Template) NameConstants(com.day.cq.wcm.api.NameConstants) Reference(org.osgi.service.component.annotations.Reference) Query(com.day.cq.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) SearchResult(com.day.cq.search.result.SearchResult) PredicateGroup(com.day.cq.search.PredicateGroup) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) Session(javax.jcr.Session) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Search

use of com.adobe.cq.wcm.core.components.models.Search in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SearchResultServlet method doGet.

@Override
protected void doGet(@NotNull final SlingHttpServletRequest request, @NotNull final SlingHttpServletResponse response) throws IOException {
    Page currentPage = Optional.ofNullable(request.getResourceResolver().adaptTo(PageManager.class)).map(pm -> pm.getContainingPage(request.getResource())).orElse(null);
    if (currentPage != null) {
        SlingBindings bindings = new SlingBindings();
        bindings.setSling(new ScriptHelper(bundleContext, null, request, response));
        request.setAttribute(SlingBindings.class.getName(), bindings);
        Search searchComponent = getSearchComponent(request, currentPage);
        try {
            List<ListItem> results = getResults(request, searchComponent, currentPage.getPageManager());
            response.setContentType("application/json");
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            new ObjectMapper().writeValue(response.getWriter(), results);
        } catch (NumberFormatException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) TypePredicateEvaluator(com.day.cq.search.eval.TypePredicateEvaluator) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SearchResult(com.day.cq.search.result.SearchResult) PredicateConverter(com.day.cq.search.PredicateConverter) Map(java.util.Map) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) Search(com.adobe.cq.wcm.core.components.models.Search) PN_FRAGMENT_VARIATION_PATH(com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH) PathPredicateEvaluator(com.day.cq.search.eval.PathPredicateEvaluator) Session(javax.jcr.Session) Servlet(javax.servlet.Servlet) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Query(com.day.cq.search.Query) Optional(java.util.Optional) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) NotNull(org.jetbrains.annotations.NotNull) FulltextPredicateEvaluator(com.day.cq.search.eval.FulltextPredicateEvaluator) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) HashMap(java.util.HashMap) LanguageManager(com.day.cq.wcm.api.LanguageManager) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) StreamSupport(java.util.stream.StreamSupport) Activate(org.osgi.service.component.annotations.Activate) SearchImpl(com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl) QueryBuilder(com.day.cq.search.QueryBuilder) Iterator(java.util.Iterator) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) LocalizationUtils(com.adobe.cq.wcm.core.components.internal.LocalizationUtils) PredicateGroup(com.day.cq.search.PredicateGroup) Template(com.day.cq.wcm.api.Template) NameConstants(com.day.cq.wcm.api.NameConstants) Reference(org.osgi.service.component.annotations.Reference) PageManager(com.day.cq.wcm.api.PageManager) SlingBindings(org.apache.sling.api.scripting.SlingBindings) Search(com.adobe.cq.wcm.core.components.models.Search) Page(com.day.cq.wcm.api.Page) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Search (com.adobe.cq.wcm.core.components.models.Search)4 LocalizationUtils (com.adobe.cq.wcm.core.components.internal.LocalizationUtils)2 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)2 PageListItemImpl (com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl)2 SearchImpl (com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl)2 PN_FRAGMENT_VARIATION_PATH (com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH)2 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)2 PredicateConverter (com.day.cq.search.PredicateConverter)2 PredicateGroup (com.day.cq.search.PredicateGroup)2 Query (com.day.cq.search.Query)2 QueryBuilder (com.day.cq.search.QueryBuilder)2 FulltextPredicateEvaluator (com.day.cq.search.eval.FulltextPredicateEvaluator)2 PathPredicateEvaluator (com.day.cq.search.eval.PathPredicateEvaluator)2 TypePredicateEvaluator (com.day.cq.search.eval.TypePredicateEvaluator)2 SearchResult (com.day.cq.search.result.SearchResult)2 LanguageManager (com.day.cq.wcm.api.LanguageManager)2 NameConstants (com.day.cq.wcm.api.NameConstants)2 Page (com.day.cq.wcm.api.Page)2 PageManager (com.day.cq.wcm.api.PageManager)2 Template (com.day.cq.wcm.api.Template)2