Search in sources :

Example 76 with ResultSet

use of org.alfresco.service.cmr.search.ResultSet in project alfresco-repository by Alfresco.

the class CMISResultSet method getScores.

private Map<String, Float> getScores(int i) {
    HashMap<String, Float> scores = new HashMap<String, Float>();
    for (String selector : wrapped.keySet()) {
        ResultSet rs = wrapped.get(selector);
        scores.put(selector, Float.valueOf(rs.getScore(i)));
    }
    return scores;
}
Also used : HashMap(java.util.HashMap) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Example 77 with ResultSet

use of org.alfresco.service.cmr.search.ResultSet in project alfresco-repository by Alfresco.

the class BlogServiceImpl method findBlogPosts.

@Override
public PagingResults<BlogPostInfo> findBlogPosts(final NodeRef blogContainerNode, final RangedDateProperty dateRange, final String tag, final PagingRequest pagingReq) {
    StringBuilder luceneQuery = new StringBuilder();
    luceneQuery.append("+TYPE:\"").append(ContentModel.TYPE_CONTENT).append("\" ").append("+PARENT:\"").append(blogContainerNode.toString()).append("\" ");
    if (tag != null && !tag.trim().isEmpty()) {
        luceneQuery.append("+PATH:\"/cm:taggable/cm:").append(ISO9075.encode(tag)).append("/member\"");
    }
    if (dateRange != null) {
        luceneQuery.append(createDateRangeQuery(dateRange.getFromDate(), dateRange.getToDate(), dateRange.getDateProperty()));
    }
    SearchParameters sp = new SearchParameters();
    sp.addStore(blogContainerNode.getStoreRef());
    sp.setLanguage(SearchService.LANGUAGE_LUCENE);
    sp.setQuery(luceneQuery.toString());
    sp.addSort(ContentModel.PROP_PUBLISHED.toString(), false);
    sp.setMaxItems(pagingReq.getMaxItems() * MIN_NUMBER_OF_PAGES_FOR_THE_USER_TO_LOOP_THROUGH);
    sp.setSkipCount(pagingReq.getSkipCount());
    ResultSet luceneResults = null;
    PagingResults<BlogPostInfo> results = null;
    try {
        luceneResults = searchService.query(sp);
        final ResultSet finalLuceneResults = luceneResults;
        final List<NodeRef> nodeRefs = finalLuceneResults.getNodeRefs().subList(0, min(pagingReq.getMaxItems(), finalLuceneResults.length()));
        results = new PagingResults<BlogPostInfo>() {

            @Override
            public List<BlogPostInfo> getPage() {
                List<BlogPostInfo> blogPostInfos = new ArrayList<BlogPostInfo>(nodeRefs.size());
                for (NodeRef nodeRef : nodeRefs) {
                    String postName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
                    blogPostInfos.add(new BlogPostInfoImpl(nodeRef, blogContainerNode, postName));
                }
                return blogPostInfos;
            }

            @Override
            public String getQueryExecutionId() {
                return null;
            }

            @Override
            public Pair<Integer, Integer> getTotalResultCount() {
                long totalResultCount = finalLuceneResults.getNumberFound();
                /*if (finalLuceneResults.hasMore()){
                        totalResultCount++;
                    }*/
                return new Pair<Integer, Integer>((int) totalResultCount, (int) totalResultCount);
            }

            @Override
            public boolean hasMoreItems() {
                return finalLuceneResults.length() > pagingReq.getMaxItems();
            }
        };
    } finally {
        if (luceneResults != null)
            luceneResults.close();
    }
    return results;
}
Also used : BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ResultSet(org.alfresco.service.cmr.search.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) Pair(org.alfresco.util.Pair)

Example 78 with ResultSet

use of org.alfresco.service.cmr.search.ResultSet in project alfresco-repository by Alfresco.

the class People method getPeopleImplSearch.

// search query
protected List<NodeRef> getPeopleImplSearch(String term, String[] tokens, ScriptPagingDetails pagingRequest, String sortBy, Boolean sortAsc) throws Throwable {
    List<NodeRef> personRefs = null;
    Long start = (logger.isDebugEnabled() ? System.currentTimeMillis() : null);
    int propIndex = term.indexOf(':');
    int maxResults = pagingRequest.getMaxItems();
    int skipCount = pagingRequest.getSkipCount();
    SearchParameters params = new SearchParameters();
    params.addQueryTemplate("_PERSON", "|%firstName OR |%lastName OR |%userName");
    params.setDefaultFieldName("_PERSON");
    params.setExcludeTenantFilter(getExcludeTenantFilter());
    params.setPermissionEvaluation(getPermissionEvaluationMode());
    StringBuilder query = new StringBuilder(256);
    query.append("TYPE:\"").append(ContentModel.TYPE_PERSON).append("\" AND (");
    if (tokens.length == 1) {
        // single word with no field will go against _PERSON and expand
        // fts-alfresco property search i.e. location:"maidenhead"
        query.append(term.substring(0, propIndex + 1)).append('"');
        if (propIndex < 0) {
            query.append('*');
        }
        query.append(term.substring(propIndex + 1));
        if (propIndex > 0) {
            query.append('"');
        } else {
            query.append("*\"");
        }
    } else {
        // scan for non-fts-alfresco property search tokens
        int nonFtsTokens = 0;
        for (String token : tokens) {
            if (token.indexOf(':') == -1)
                nonFtsTokens++;
        }
        tokens = term.split("(?<!\\\\) ");
        // multiple terms supplied - look for first and second name etc.
        // also allow fts-alfresco property search to reduce results
        params.setDefaultOperator(SearchParameters.Operator.AND);
        boolean propertySearch = false;
        StringBuilder multiPartNames = new StringBuilder(tokens.length);
        boolean firstToken = true;
        for (String token : tokens) {
            if (!propertySearch && token.indexOf(':') == -1) {
                if (nonFtsTokens == 1) {
                    // simple search: first name, last name and username
                    // starting with term
                    query.append("_PERSON:\"*");
                    query.append(token);
                    query.append("*\" ");
                } else {
                    // firstName and lastName
                    if (token.endsWith("*")) {
                        token = token.substring(0, token.lastIndexOf("*"));
                    }
                    multiPartNames.append("\"*");
                    multiPartNames.append(token);
                    multiPartNames.append("*\"");
                    if (firstToken) {
                        multiPartNames.append(' ');
                    }
                    firstToken = false;
                }
            } else {
                // fts-alfresco property search i.e. "location:maidenhead"
                propIndex = token.lastIndexOf(':');
                query.append(token.substring(0, propIndex + 1)).append('"').append(token.substring(propIndex + 1)).append('"').append(' ');
                propertySearch = true;
            }
        }
        // name and "lewis martinez" is the last name.
        if (multiPartNames.length() > 0) {
            query.append("firstName:");
            query.append(multiPartNames);
            query.append(" OR lastName:");
            query.append(multiPartNames);
        }
    }
    query.append(")");
    // define the search parameters
    params.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO);
    params.addStore(this.storeRef);
    params.setQuery(query.toString());
    if (logger.isDebugEnabled()) {
        if ((sortBy != null) && (!sortBy.isEmpty())) {
            logger.debug("getPeopleImplSearch: ignoring sortBy (" + sortBy + ")- not yet supported by model for search");
        }
    }
    if (maxResults > 0) {
        params.setLimitBy(LimitBy.FINAL_SIZE);
        params.setLimit(maxResults);
    }
    if (skipCount > 0) {
        params.setSkipCount(skipCount);
    }
    ResultSet results = null;
    try {
        results = services.getSearchService().query(params);
        personRefs = getSortedPeopleObjects(results.getNodeRefs(), sortBy, sortAsc);
        if (start != null) {
            logger.debug("getPeople: search - " + personRefs.size() + " items (in " + (System.currentTimeMillis() - start) + " msecs)");
        }
    } catch (Throwable err) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to execute people search: " + query.toString(), err);
        }
        throw err;
    } finally {
        if (results != null) {
            results.close();
        }
    }
    return personRefs;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Example 79 with ResultSet

use of org.alfresco.service.cmr.search.ResultSet in project alfresco-repository by Alfresco.

the class LuceneCategoryServiceImpl method getChildren.

private Collection<ChildAssociationRef> getChildren(NodeRef categoryRef, Mode mode, Depth depth, boolean sortByName, String filter, int fetchSize) {
    if (categoryRef == null) {
        return Collections.<ChildAssociationRef>emptyList();
    }
    // for solr
    categoryRef = tenantService.getBaseName(categoryRef);
    ResultSet resultSet = null;
    try {
        StringBuilder luceneQuery = new StringBuilder(64);
        switch(mode) {
            case ALL:
                luceneQuery.append("PATH:\"");
                luceneQuery.append(buildXPath(nodeService.getPath(categoryRef))).append("/");
                if (depth.equals(Depth.ANY)) {
                    luceneQuery.append("/");
                }
                luceneQuery.append("*").append("\" ");
                break;
            case MEMBERS:
                luceneQuery.append("PATH:\"");
                luceneQuery.append(buildXPath(nodeService.getPath(categoryRef))).append("/");
                if (depth.equals(Depth.ANY)) {
                    luceneQuery.append("/");
                }
                luceneQuery.append("member").append("\" ");
                break;
            case SUB_CATEGORIES:
                luceneQuery.append("+PATH:\"");
                luceneQuery.append(buildXPath(nodeService.getPath(categoryRef))).append("/");
                if (depth.equals(Depth.ANY)) {
                    luceneQuery.append("/");
                }
                luceneQuery.append("*").append("\" ");
                luceneQuery.append("+TYPE:\"" + ContentModel.TYPE_CATEGORY.toString() + "\"");
                break;
        }
        if (filter != null) {
            luceneQuery.append(" " + "+@cm\\:name:\"*" + filter + "*\"");
        }
        // Get a searcher that will include Categories added in this transaction
        SearchService searcher = indexerAndSearcher.getSearcher(categoryRef.getStoreRef(), true);
        // Perform the search
        SearchParameters searchParameters = new SearchParameters();
        resultSet = searcher.query(categoryRef.getStoreRef(), "lucene", luceneQuery.toString(), null);
        searchParameters.setLanguage("lucene");
        if (sortByName) {
            searchParameters.addSort("@" + ContentModel.PROP_NAME, true);
        }
        searchParameters.setQuery(luceneQuery.toString());
        searchParameters.setLimit(-1);
        searchParameters.setMaxItems(fetchSize);
        searchParameters.setLimitBy(LimitBy.FINAL_SIZE);
        searchParameters.addStore(categoryRef.getStoreRef());
        resultSet = searcher.query(searchParameters);
        // Convert from search results to the required Child Assocs
        return resultSetToChildAssocCollection(resultSet);
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
    }
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) SearchService(org.alfresco.service.cmr.search.SearchService) ResultSet(org.alfresco.service.cmr.search.ResultSet) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 80 with ResultSet

use of org.alfresco.service.cmr.search.ResultSet in project alfresco-repository by Alfresco.

the class TaggingServiceImpl method findTaggedNodes.

/**
 * @see org.alfresco.service.cmr.tagging.TaggingService#findTaggedNodes(StoreRef, java.lang.String, org.alfresco.service.cmr.repository.NodeRef)
 */
public List<NodeRef> findTaggedNodes(StoreRef storeRef, String tag, NodeRef nodeRef) {
    // Lower the case of the tag
    tag = tag.toLowerCase();
    // Get path
    Path nodePath = this.nodeService.getPath(nodeRef);
    String pathString = nodePath.toPrefixString(this.namespaceService);
    ResultSet resultSet = null;
    try {
        // Do query
        resultSet = this.searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "+PATH:\"" + pathString + "//*\" +PATH:\"/cm:taggable/cm:" + ISO9075.encode(tag) + "/member\"");
        List<NodeRef> nodeRefs = resultSet.getNodeRefs();
        return nodeRefs;
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
    }
}
Also used : Path(org.alfresco.service.cmr.repository.Path) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Aggregations

ResultSet (org.alfresco.service.cmr.search.ResultSet)112 NodeRef (org.alfresco.service.cmr.repository.NodeRef)57 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)49 ArrayList (java.util.ArrayList)25 HashMap (java.util.HashMap)22 SolrJSONResultSet (org.alfresco.repo.search.impl.lucene.SolrJSONResultSet)20 Test (org.junit.Test)20 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)17 QName (org.alfresco.service.namespace.QName)16 StoreRef (org.alfresco.service.cmr.repository.StoreRef)14 ResultSetRow (org.alfresco.service.cmr.search.ResultSetRow)14 HashSet (java.util.HashSet)13 EmptyResultSet (org.alfresco.repo.search.EmptyResultSet)13 SearchService (org.alfresco.service.cmr.search.SearchService)13 NodeService (org.alfresco.service.cmr.repository.NodeService)12 IOException (java.io.IOException)11 List (java.util.List)11 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)11 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)10 PagingLuceneResultSet (org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet)10