Nuxeo/Blogs

Product & Development / All about the Nuxeo Platform, from strategy to feature highlights to dev tricks

[Q&A Friday] SELECT clause in Nuxeo NXQL queries

with 2 comments

Hello Everyone! Today we have a question from Franck. He’s asking why he cannot do query with a specific select clause like this :

Administrator@127.0.0.1:/> query "select dc:title from myDocumentType"
Administrator@127.0.0.1:/>`

while this is working fine:

Administrator@127.0.0.1:/> query "select * from myDocumentType"
/default-domain/workspaces/Library/sections/North America
SELECT clause in NXQL queries

SELECT clause in NXQL queries

Frank is actually running those queries from Nuxeo Shell. The query command uses the Document.Query operation which itself uses CoreSession#query. This method will only return a DocumentModelList so you cannot use a specific select clause in your query. And this makes sense, because with Content Automation, you usually manipulate Documents.

So when can you use a specific select clause? You have to use CoreSession#queryAndFetch. This will return an IterableQueryResult. If you still want to do your query from the shell, a quick and easy way to do it is to use a Groovy script like this:

iterableQueryResult = Session.queryAndFetch("SELECT dc:title,ecm:uuid FROM Document WHERE dc:title = 'Workspace'", "NXQL");
out = "";
while(iterableQueryResult.hasNext()){
	out +=  iterableQueryResult.next().toString() + "n";
}
// don't forget to close it!
iterableQueryResult.close();
return out;

To call it you have to use the script command and give it the path of your Groovy script:

Administrator@localhost:/> script /home/ldoguin/test.groovy
{ecm:uuid=168458db-47fc-4c34-be50-bdc6f4dbcb56, dc:title=Workspace}

If you want to know more about Nuxeo’s NXQL, take a look at the documentation.

Cheers!

October 7th, 2012 at 6:46 pm

Posted in Product & Development

Tagged with , , ,

About Laurent Doguin

Laurent works as developer and community liaison at Nuxeo, a software company providing a full Enterprise Content Management Platform, open source, for any kind of content-driven application.
  • Franck

    Your reply makes perfect sense, thanks. I am not convinced that the improvement wouldn’t be valuable though, the Automation API seems to be the preferred remote interface technology and remote calls is precisely when you want to be able to precisely control the amount of data you retrieve based on the specific view or task at hand…
    Nothing that can’t be dealt with with a custom operation though, I will take that approach.

  • Williams Rivas

    Hi I am using this code but the DocumentModel does not have the Blob file:

    String sql = “SELECT * FROM File”;

    DocumentModelList dml = documentManager. query(sql, 1);