Search in sources :

Example 1 with HighlightEntry

use of org.alfresco.rest.api.search.model.HighlightEntry in project alfresco-remote-api by Alfresco.

the class ResultMapperTests method testToCollectionWithPagingInfo.

@Test
public void testToCollectionWithPagingInfo() throws Exception {
    ResultSet results = mockResultset(Arrays.asList(514l), Arrays.asList(566l, VERSIONED_ID));
    SearchRequestContext searchRequest = SearchRequestContext.from(SearchQuery.EMPTY);
    CollectionWithPagingInfo<Node> collectionWithPage = mapper.toCollectionWithPagingInfo(EMPTY_PARAMS, searchRequest, SearchQuery.EMPTY, results);
    assertNotNull(collectionWithPage);
    Long found = results.getNumberFound();
    assertEquals(found.intValue(), collectionWithPage.getTotalItems().intValue());
    Node firstNode = collectionWithPage.getCollection().stream().findFirst().get();
    assertNotNull(firstNode.getSearch().getScore());
    assertEquals(StoreMapper.LIVE_NODES, firstNode.getLocation());
    collectionWithPage.getCollection().stream().forEach(aNode -> {
        List<HighlightEntry> high = aNode.getSearch().getHighlight();
        if (high != null) {
            assertEquals(2, high.size());
            HighlightEntry first = high.get(0);
            assertNotNull(first.getField());
            assertNotNull(first.getSnippets());
        }
    });
    // 1 deleted node in the test data
    assertEquals(1l, collectionWithPage.getCollection().stream().filter(node -> StoreMapper.DELETED.equals(node.getLocation())).count());
    // 1 version nodes in the test data (and 1 is not shown because it is in the archive store)
    assertEquals(1l, collectionWithPage.getCollection().stream().filter(node -> StoreMapper.VERSIONS.equals(node.getLocation())).count());
}
Also used : Node(org.alfresco.rest.api.model.Node) ResultSet(org.alfresco.service.cmr.search.ResultSet) EmptyResultSet(org.alfresco.repo.search.EmptyResultSet) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) HighlightEntry(org.alfresco.rest.api.search.model.HighlightEntry) Test(org.junit.Test)

Example 2 with HighlightEntry

use of org.alfresco.rest.api.search.model.HighlightEntry in project alfresco-remote-api by Alfresco.

the class ResultMapper method toCollectionWithPagingInfo.

/**
 * Turns the results into a CollectionWithPagingInfo
 * @param params
 * @param searchQuery
 *@param results  @return CollectionWithPagingInfo<Node>
 */
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, SearchRequestContext searchRequestContext, SearchQuery searchQuery, ResultSet results) {
    SearchContext context = null;
    Integer total = null;
    List<Node> noderesults = new ArrayList<Node>();
    Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
    Map<NodeRef, List<Pair<String, List<String>>>> hightLighting = results.getHighlighting();
    int notFound = 0;
    boolean isHistory = searchRequestContext.getStores().contains(StoreMapper.HISTORY);
    for (ResultSetRow row : results) {
        Node aNode = getNode(row, params, mapUserInfo, isHistory);
        if (aNode != null) {
            float f = row.getScore();
            List<HighlightEntry> highlightEntries = null;
            List<Pair<String, List<String>>> high = hightLighting.get(row.getNodeRef());
            if (high != null && !high.isEmpty()) {
                highlightEntries = new ArrayList<HighlightEntry>(high.size());
                for (Pair<String, List<String>> highlight : high) {
                    highlightEntries.add(new HighlightEntry(highlight.getFirst(), highlight.getSecond()));
                }
            }
            aNode.setSearch(new SearchEntry(f, highlightEntries));
            noderesults.add(aNode);
        } else {
            logger.debug("Unknown noderef returned from search results " + row.getNodeRef());
            notFound++;
        }
    }
    SolrJSONResultSet solrResultSet = findSolrResultSet(results);
    if (solrResultSet != null) {
        // We used Solr for this query
        context = toSearchContext(solrResultSet, searchRequestContext, searchQuery, notFound);
        total = setTotal(solrResultSet);
    } else {
        // This probably wasn't solr
        if (!results.hasMore()) {
            // If there are no more results then we are confident that the number found is correct
            // otherwise we are not confident enough that its accurate
            total = setTotal(results);
        }
    }
    return CollectionWithPagingInfo.asPaged(params.getPaging(), noderesults, results.hasMore(), total, null, context);
}
Also used : HashMap(java.util.HashMap) Node(org.alfresco.rest.api.model.Node) ArrayList(java.util.ArrayList) SearchContext(org.alfresco.rest.api.search.context.SearchContext) UserInfo(org.alfresco.rest.api.model.UserInfo) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) NodeRef(org.alfresco.service.cmr.repository.NodeRef) List(java.util.List) TupleList(org.alfresco.rest.api.search.model.TupleList) ArrayList(java.util.ArrayList) SearchEntry(org.alfresco.rest.api.search.model.SearchEntry) HighlightEntry(org.alfresco.rest.api.search.model.HighlightEntry) Pair(org.alfresco.util.Pair)

Aggregations

SolrJSONResultSet (org.alfresco.repo.search.impl.lucene.SolrJSONResultSet)2 Node (org.alfresco.rest.api.model.Node)2 HighlightEntry (org.alfresco.rest.api.search.model.HighlightEntry)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 EmptyResultSet (org.alfresco.repo.search.EmptyResultSet)1 UserInfo (org.alfresco.rest.api.model.UserInfo)1 SearchContext (org.alfresco.rest.api.search.context.SearchContext)1 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)1 SearchEntry (org.alfresco.rest.api.search.model.SearchEntry)1 TupleList (org.alfresco.rest.api.search.model.TupleList)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1 ResultSetRow (org.alfresco.service.cmr.search.ResultSetRow)1 Pair (org.alfresco.util.Pair)1 Test (org.junit.Test)1