Search in sources :

Example 1 with SimpleSearch

use of com.day.cq.search.SimpleSearch in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ListImpl method populateSearchListItems.

private void populateSearchListItems() {
    listItems = new ArrayList<>();
    if (!StringUtils.isBlank(query)) {
        SimpleSearch search = resource.adaptTo(SimpleSearch.class);
        if (search != null) {
            search.setQuery(query);
            search.setSearchIn(startIn);
            search.addPredicate(new Predicate("type", "type").set("type", NameConstants.NT_PAGE));
            search.setHitsPerPage(limit);
            try {
                collectSearchResults(search.getResult());
            } catch (RepositoryException e) {
                LOGGER.error("Unable to retrieve search results for query.", e);
            }
        }
    }
}
Also used : SimpleSearch(com.day.cq.search.SimpleSearch) RepositoryException(javax.jcr.RepositoryException) Predicate(com.day.cq.search.Predicate)

Example 2 with SimpleSearch

use of com.day.cq.search.SimpleSearch in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ListImplTest method testSearchListType.

@Test
protected void testSearchListType() throws Exception {
    Session mockSession = mock(Session.class);
    SimpleSearch mockSimpleSearch = mock(SimpleSearch.class);
    context.registerAdapter(ResourceResolver.class, Session.class, mockSession);
    context.registerAdapter(Resource.class, SimpleSearch.class, mockSimpleSearch);
    SearchResult searchResult = mock(SearchResult.class);
    when(mockSimpleSearch.getResult()).thenReturn(searchResult);
    when(searchResult.getResources()).thenReturn(Collections.singletonList(Objects.requireNonNull(context.resourceResolver().getResource("/content/list/pages/page_1/jcr:content"))).iterator());
    List list = getListUnderTest(LIST_6);
    checkListConsistencyByPaths(list, new String[] { "/content/list/pages/page_1" });
    Utils.testJSONExport(list, Utils.getTestExporterJSONPath(testBase, LIST_6));
}
Also used : SimpleSearch(com.day.cq.search.SimpleSearch) SearchResult(com.day.cq.search.result.SearchResult) List(com.adobe.cq.wcm.core.components.models.List) Session(javax.jcr.Session) Test(org.junit.jupiter.api.Test)

Example 3 with SimpleSearch

use of com.day.cq.search.SimpleSearch in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ListImpl method getSearchListItems.

/**
 * Get the list items if using the search source type.
 *
 * @return The search list items.
 */
private Stream<Page> getSearchListItems() {
    Optional<Page> searchRoot = getRootPage(PN_SEARCH_IN);
    String query = properties.get(PN_SEARCH_QUERY, String.class);
    if (!StringUtils.isBlank(query) && searchRoot.isPresent()) {
        SimpleSearch search = resource.adaptTo(SimpleSearch.class);
        if (search != null) {
            search.setQuery(query);
            search.setSearchIn(searchRoot.get().getPath());
            search.addPredicate(new Predicate("type", "type").set("type", NameConstants.NT_PAGE));
            int limit = properties.get(PN_SEARCH_LIMIT, SEARCH_LIMIT_DEFAULT);
            search.setHitsPerPage(limit);
            return safeGetSearchResult(search).map(SearchResult::getResources).map(it -> (Iterable<Resource>) () -> it).map(it -> StreamSupport.stream(it.spliterator(), false)).orElseGet(Stream::empty).filter(Objects::nonNull).map(currentPage.getPageManager()::getContainingPage).filter(Objects::nonNull);
        }
    }
    return Stream.empty();
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) TagManager(com.day.cq.tagging.TagManager) Style(com.day.cq.wcm.api.designer.Style) SearchResult(com.day.cq.search.result.SearchResult) ScriptVariable(org.apache.sling.models.annotations.injectorspecific.ScriptVariable) RepositoryException(javax.jcr.RepositoryException) Locale(java.util.Locale) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Model(org.apache.sling.models.annotations.Model) StreamSupport(java.util.stream.StreamSupport) Predicate(com.day.cq.search.Predicate) ComponentExporter(com.adobe.cq.export.json.ComponentExporter) Collator(java.text.Collator) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) SimpleSearch(com.day.cq.search.SimpleSearch) List(com.adobe.cq.wcm.core.components.models.List) Collection(java.util.Collection) Resource(org.apache.sling.api.resource.Resource) Collectors(java.util.stream.Collectors) Exporter(org.apache.sling.models.annotations.Exporter) Serializable(java.io.Serializable) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) Stream(java.util.stream.Stream) ExporterConstants(com.adobe.cq.export.json.ExporterConstants) NameConstants(com.day.cq.wcm.api.NameConstants) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Comparator(java.util.Comparator) NotNull(org.jetbrains.annotations.NotNull) AbstractComponentImpl(com.adobe.cq.wcm.core.components.util.AbstractComponentImpl) SimpleSearch(com.day.cq.search.SimpleSearch) Objects(java.util.Objects) Page(com.day.cq.wcm.api.Page) SearchResult(com.day.cq.search.result.SearchResult) Predicate(com.day.cq.search.Predicate)

Aggregations

SimpleSearch (com.day.cq.search.SimpleSearch)3 List (com.adobe.cq.wcm.core.components.models.List)2 Predicate (com.day.cq.search.Predicate)2 SearchResult (com.day.cq.search.result.SearchResult)2 RepositoryException (javax.jcr.RepositoryException)2 ComponentExporter (com.adobe.cq.export.json.ComponentExporter)1 ExporterConstants (com.adobe.cq.export.json.ExporterConstants)1 AbstractComponentImpl (com.adobe.cq.wcm.core.components.util.AbstractComponentImpl)1 TagManager (com.day.cq.tagging.TagManager)1 NameConstants (com.day.cq.wcm.api.NameConstants)1 Page (com.day.cq.wcm.api.Page)1 Style (com.day.cq.wcm.api.designer.Style)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 Serializable (java.io.Serializable)1 Collator (java.text.Collator)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1