Search in sources :

Example 1 with SearchResult

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

the class ListImplTest method testSearchListType.

@Test
public void testSearchListType() throws Exception {
    SearchResult searchResult = mock(SearchResult.class);
    Hit hit = mock(Hit.class);
    when(mockSimpleSearch.getResult()).thenReturn(searchResult);
    when(searchResult.getHits()).thenReturn(Collections.singletonList(hit));
    Resource contentResource = context.currentResource("/content/list/pages/page_1/jcr:content");
    when(hit.getResource()).thenReturn(contentResource);
    Resource resource = context.currentResource("/content/list/listTypes/searchListType");
    slingBindings.put(WCMBindings.PROPERTIES, resource.adaptTo(ValueMap.class));
    slingBindings.put(WCMBindings.CURRENT_STYLE, new MockStyle(resource));
    List list = context.request().adaptTo(List.class);
    assertEquals(1, list.getItems().size());
}
Also used : Hit(com.day.cq.search.result.Hit) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle) SearchResult(com.day.cq.search.result.SearchResult) List(com.adobe.cq.wcm.core.components.models.List) Test(org.junit.Test)

Example 2 with SearchResult

use of com.day.cq.search.result.SearchResult in project acs-aem-commons by Adobe-Consulting-Services.

the class OnDeployScriptBase method searchAndUpdateResourceType.

/**
 * Searches for the current sling:resourceType under /content and replaces any nodes it finds
 * with the newResourceType.
 *
 * @param oldResourceType The current sling:resourceType.
 * @param newResourceType The new sling:resourceType to be used.
 */
protected final void searchAndUpdateResourceType(String oldResourceType, String newResourceType) throws RepositoryException {
    Map<String, String> map = new HashMap<>();
    map.put("p.limit", "-1");
    map.put("path", "/content");
    map.put("1_property", SLING_RESOURCE_TYPE);
    map.put("1_property.value", oldResourceType);
    logger.info("Finding all nodes under /content with resource type: {}", oldResourceType);
    Query query = resourceResolver.adaptTo(QueryBuilder.class).createQuery(PredicateGroup.create(map), session);
    SearchResult result = query.getResult();
    Iterator<Node> nodeItr = result.getNodes();
    if (nodeItr.hasNext()) {
        while (nodeItr.hasNext()) {
            Node node = nodeItr.next();
            updateResourceType(node, newResourceType);
        }
    } else {
        logger.info("No nodes found with resource type: {}", oldResourceType);
    }
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Node(javax.jcr.Node) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder)

Example 3 with SearchResult

use of com.day.cq.search.result.SearchResult in project acs-aem-commons by Adobe-Consulting-Services.

the class TwitterFeedUpdaterImpl method findTwitterResources.

private List<Resource> findTwitterResources(ResourceResolver resourceResolver) {
    List<Resource> twitterResources = new ArrayList<>();
    Map<String, String> predicateMap = new HashMap<>();
    predicateMap.put("path", "/content");
    predicateMap.put("property", "sling:resourceType");
    int counter = 1;
    for (String path : twitterComponentPaths) {
        predicateMap.put("property." + (counter++) + "_value", path);
    }
    predicateMap.put("p.limit", "-1");
    QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
    Session session = resourceResolver.adaptTo(Session.class);
    Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
    SearchResult result = query.getResult();
    Iterator<Resource> resources = result.getResources();
    while (resources.hasNext()) {
        twitterResources.add(resources.next());
    }
    return twitterResources;
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) Session(javax.jcr.Session)

Example 4 with SearchResult

use of com.day.cq.search.result.SearchResult in project acs-aem-commons by Adobe-Consulting-Services.

the class QueryBuilderViewQuery method execute.

@Override
public Collection<com.day.cq.wcm.core.contentfinder.Hit> execute() {
    final List<com.day.cq.wcm.core.contentfinder.Hit> hits = new ArrayList<com.day.cq.wcm.core.contentfinder.Hit>();
    if (this.query == null) {
        return hits;
    }
    final SearchResult result = this.query.getResult();
    // iterating over the results
    for (Hit hit : result.getHits()) {
        try {
            hits.add(createHit(hit));
        } catch (RepositoryException e) {
            log.error("Could not return required information for Content Finder result: {}", hit.toString());
        }
    }
    return hits;
}
Also used : Hit(com.day.cq.search.result.Hit) ArrayList(java.util.ArrayList) SearchResult(com.day.cq.search.result.SearchResult) RepositoryException(javax.jcr.RepositoryException)

Example 5 with SearchResult

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

the class ContentFragmentListImplTest method testListWithOneFragment.

@SuppressWarnings("unchecked")
private void testListWithOneFragment(String listName) {
    Resource resource = context.resourceResolver().getResource("/content/dam/contentfragments-for-list/text-only");
    if (resource != null) {
        Resource DAMFragment = Mockito.spy(resource);
        Query query = Mockito.mock(Query.class);
        SearchResult searchResult = Mockito.mock(SearchResult.class);
        Iterator<Resource> iterator = Mockito.mock(Iterator.class);
        ResourceResolver spyResolver = Mockito.spy(DAMFragment.getResourceResolver());
        when(query.getResult()).thenReturn(searchResult);
        when(searchResult.getResources()).thenReturn(iterator);
        when(iterator.hasNext()).thenReturn(true, false);
        when(iterator.next()).thenReturn(DAMFragment);
        when(DAMFragment.getResourceResolver()).thenReturn(spyResolver);
        Mockito.doNothing().when(spyResolver).close();
        when(queryBuilderMock.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
        ContentFragmentList contentFragmentList = getModelInstanceUnderTest(listName);
        assertEquals(ContentFragmentListImpl.RESOURCE_TYPE_V1, contentFragmentList.getExportedType());
        assertEquals(contentFragmentList.getListItems().size(), 1);
        Utils.testJSONExport(contentFragmentList, Utils.getTestExporterJSONPath(TEST_BASE, listName));
        Mockito.doCallRealMethod().when(spyResolver).close();
    }
}
Also used : Query(com.day.cq.search.Query) ContentFragmentList(com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragmentList) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SearchResult(com.day.cq.search.result.SearchResult) PredicateGroup(com.day.cq.search.PredicateGroup) Session(javax.jcr.Session)

Aggregations

SearchResult (com.day.cq.search.result.SearchResult)16 Query (com.day.cq.search.Query)12 Resource (org.apache.sling.api.resource.Resource)11 Session (javax.jcr.Session)10 PredicateGroup (com.day.cq.search.PredicateGroup)8 QueryBuilder (com.day.cq.search.QueryBuilder)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (com.adobe.cq.wcm.core.components.models.List)3 Hit (com.day.cq.search.result.Hit)3 Iterator (java.util.Iterator)3 Objects (java.util.Objects)3 StreamSupport (java.util.stream.StreamSupport)3 RepositoryException (javax.jcr.RepositoryException)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)2 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)2 SimpleSearch (com.day.cq.search.SimpleSearch)2