Search in sources :

Example 96 with SearchParameters

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

the class SiteServiceImpl method findSites.

/*
     * (non-Javadoc)
     * @see org.alfresco.service.cmr.site.SiteService#findSites(java.lang.String, int)
     */
@Override
public List<SiteInfo> findSites(String filter, int size) {
    List<SiteInfo> result;
    NodeRef siteRoot = getSiteRoot();
    if (siteRoot == null) {
        result = Collections.emptyList();
    } else {
        // get the sites that match the specified names
        StringBuilder query = new StringBuilder(128);
        query.append("+TYPE:\"").append(SiteModel.TYPE_SITE).append('"');
        final boolean filterIsPresent = filter != null && filter.length() > 0;
        if (filterIsPresent) {
            query.append(" AND (");
            String escNameFilter = SearchLanguageConversion.escapeLuceneQuery(filter.replace('"', ' '));
            String[] tokenizedFilter = SearchLanguageConversion.tokenizeString(escNameFilter);
            // cm:name
            query.append(" cm:name:\" ");
            for (int i = 0; i < tokenizedFilter.length; i++) {
                if (// Not first element
                i != 0) {
                    query.append("?");
                }
                query.append(tokenizedFilter[i].toLowerCase());
            }
            query.append("*\"");
            // cm:title
            query.append(" OR ").append(" cm:title: (");
            for (int i = 0; i < tokenizedFilter.length; i++) {
                if (// Not first element
                i != 0) {
                    query.append(" AND ");
                }
                query.append("\"" + tokenizedFilter[i] + "*\" ");
            }
            query.append(")");
            query.append(" OR cm:description:\"" + escNameFilter + "\"");
            query.append(")");
        }
        SearchParameters sp = new SearchParameters();
        sp.addStore(siteRoot.getStoreRef());
        sp.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO);
        sp.setQuery(query.toString());
        if (size > 0) {
            sp.setLimit(size);
            sp.setLimitBy(LimitBy.FINAL_SIZE);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Search parameters are: " + sp);
        }
        ResultSet results = null;
        try {
            results = this.searchService.query(sp);
            result = new ArrayList<SiteInfo>(results.length());
            for (NodeRef site : results.getNodeRefs()) {
                result.add(createSiteInfo(site));
            }
        } catch (LuceneQueryParserException lqpe) {
            // Log the error but suppress is from the user
            logger.error("LuceneQueryParserException with findSites()", lqpe);
            result = Collections.emptyList();
        } finally {
            if (results != null)
                results.close();
        }
    }
    return result;
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) ResultSet(org.alfresco.service.cmr.search.ResultSet) FilterTypeString(org.alfresco.repo.node.getchildren.FilterPropString.FilterTypeString) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString)

Example 97 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters 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 98 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters 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 99 with SearchParameters

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

the class PersonServiceImpl method getPeopleFtsList.

private List<NodeRef> getPeopleFtsList(String pattern, PagingRequest pagingRequest) throws Throwable {
    // Think this code is based on org.alfresco.repo.jscript.People.getPeopleImplSearch(String, StringTokenizer, int, int)
    List<NodeRef> people = null;
    SearchParameters params = new SearchParameters();
    params.addQueryTemplate("_PERSON", "|%firstName OR |%lastName OR |%userName");
    params.setDefaultFieldName("_PERSON");
    StringBuilder query = new StringBuilder(256);
    query.append("TYPE:\"").append(ContentModel.TYPE_PERSON).append("\" AND (");
    StringTokenizer t = new StringTokenizer(pattern, " ");
    if (t.countTokens() == 1) {
        // fts-alfresco property search i.e. location:"maidenhead"
        query.append('"').append(pattern).append("*\"");
    } else {
        // multiple terms supplied - look for first and second name etc.
        // assume first term is first name, any more are second i.e.
        // "Fraun van de Wiels"
        // also allow fts-alfresco property search to reduce results
        params.setDefaultOperator(SearchParameters.Operator.AND);
        StringBuilder multiPartNames = new StringBuilder(pattern.length());
        int numOfTokens = t.countTokens();
        int counter = 1;
        String term = null;
        // MNT-8539, in order to support firstname and lastname search
        while (t.hasMoreTokens()) {
            term = t.nextToken();
            // firstName and lastName
            if (term.endsWith("*")) {
                term = term.substring(0, term.lastIndexOf("*"));
            }
            multiPartNames.append("\"");
            multiPartNames.append(term);
            multiPartNames.append("*\"");
            if (numOfTokens > counter) {
                multiPartNames.append(' ');
            }
            counter++;
        }
        // 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 (pagingRequest.getMaxItems() > 0) {
        params.setLimitBy(LimitBy.FINAL_SIZE);
        params.setLimit(pagingRequest.getMaxItems());
    }
    ResultSet results = null;
    try {
        results = searchService.query(params);
        people = results.getNodeRefs();
    } 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 people;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) StringTokenizer(java.util.StringTokenizer) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Example 100 with SearchParameters

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

the class VirtualQueryImpl method perform.

@Override
public PagingResults<Reference> perform(ActualEnvironment environment, VirtualQueryConstraint constraint, PagingRequest pagingRequest, Reference parentReference) throws VirtualizationException {
    VirtualQueryConstraint theConstraint = constraint;
    if (pagingRequest != null) {
        theConstraint = new PagingRequestConstraint(theConstraint, pagingRequest);
    }
    SearchParameters searchParameters = theConstraint.apply(environment, this);
    ResultSet result = environment.query(searchParameters);
    if (logger.isDebugEnabled()) {
        logger.debug("Constrained query " + searchParameters + " resulted in " + result.length() + " rows.");
    }
    return asPagingResults(environment, pagingRequest, result, parentReference);
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ResultSet(org.alfresco.service.cmr.search.ResultSet) EmptyResultSet(org.alfresco.repo.search.EmptyResultSet)

Aggregations

SearchParameters (org.alfresco.service.cmr.search.SearchParameters)120 ResultSet (org.alfresco.service.cmr.search.ResultSet)51 Test (org.junit.Test)41 NodeRef (org.alfresco.service.cmr.repository.NodeRef)33 ArrayList (java.util.ArrayList)25 StoreRef (org.alfresco.service.cmr.repository.StoreRef)16 IOException (java.io.IOException)15 HashMap (java.util.HashMap)14 QName (org.alfresco.service.namespace.QName)13 List (java.util.List)12 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)12 JSONObject (org.json.JSONObject)12 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)11 FieldHighlightParameters (org.alfresco.service.cmr.search.FieldHighlightParameters)11 Set (java.util.Set)10 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)10 RangeParameters (org.alfresco.service.cmr.search.RangeParameters)10 JSONArray (org.json.JSONArray)10 JSONTokener (org.json.JSONTokener)10 Locale (java.util.Locale)9