cancel
Showing results for 
Search instead for 
Did you mean: 

Reproducing a node browser query in Java code.

sfoster
Champ in-the-making
Champ in-the-making
Hi,

I'm trying to use the SearchService to retrieve all nodes of a certain type. I tested the query in the node browser, which returns a single noderef as expected. However, I can't reproduce this in Java code.

The lucene query is:  TYPE:"{http://www.adnovate.com/model/projectmanagement/1.0}project"

The Java code I'm using is:


    @Override
    public List<Project> getProjects() {
        RetryingTransactionCallback<List<NodeRef>> searchCallback = new RetryingTransactionCallback<List<NodeRef>>() {
            public List<NodeRef> execute() throws Throwable {
                List<NodeRef> nodeRefs = serviceRegistry
                        .getSearchService()
                        .query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE,
                                SearchService.LANGUAGE_LUCENE,
                                "TYPE:\"" + ProjectManagementModel.TYPE_PROJECT.toString() + "\"")
                        .getNodeRefs();
                return nodeRefs;
            }
        };

        List<Project> projects = new LinkedList<Project>();
        List<NodeRef> nodeRefs = serviceRegistry.getTransactionService()
                .getRetryingTransactionHelper().doInTransaction(searchCallback, true);

        for (NodeRef nodeRef : nodeRefs) {
            projects.add(getProject(nodeRef));
        }

        return projects;
    }

The code was mainly borrowed from the NodeBrowserScript class.

The alfresco version is 4.0.b.

Any help would be much appreciated.
3 REPLIES 3

mitpatoliya
Star Collaborator
Star Collaborator
//Set current user as admin and following code in sufficient.
AuthnticationUtils.runAs("admin");
List<NodeRef> nodeRefs = serviceRegistry
                        .getSearchService()
                        .query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE,
                                SearchService.LANGUAGE_LUCENE,
                                "TYPE:\"" + ProjectManagementModel.TYPE_PROJECT.toString() + "\"")
                        .getNodeRefs();

Use SearchService.LANGUAGE_FTS_ALFRESCO instead of SearchService.LANGUAGE_LUCENE.

sfoster
Champ in-the-making
Champ in-the-making
Yes, that fixed it. Thanks!