Tutorials

Query and Search

Updated: February 22, 2017 Page Information Edit on GitHub

Using the JS client, the Repository's query method can be used to launch NXQL queries or call page providers.

Launching NXQL Queries

Specify the NXQL query to be launched in the query property:

nuxeo.Repository().query({ 'query': myQuery })...

where myQuery is a variable containing the query to launch.

Sample NXQL Query for a Fulltext Search

SELECT * FROM Document
    WHERE ecm:mixinType != 'HiddenInNavigation'
    AND ecm:isProxy = 0
    AND ecm:isCheckedInVersion = 0
    AND ecm:currentLifeCycleState != 'deleted'
    AND ecm:fulltext LIKE 'MyKeyword'

See the NXQL documentation for other possibilities.

Calling Page Providers

A page provider is the underlying query when creating a content view in Nuxeo Studio.

To call page providers:

  • Specify the content view ID, as defined in Nuxeo Studio in the pageProvider property.
  • Pass an array containing the parameters in the queryParams property in the same order as they are defined in the content view.
nuxeo.Repository().query({ 'pageProvider': 'pageProviderId', queryParams['parameter1', 'parameter2'...] })...

Practice - Querying and Searching

Calling Page Providers

  1. Download callPageProvider.js or open in another tab to copy and paste.
  2. Replace NUXEO_SERVER with your Nuxeo Server URL.
  3. Define query options, calling the DefaultContentListingInNavigation content view defined in Nuxeo Studio and passing it the UID of an existing parent document as a parameter. The query result should return the document's children as objects.
  4. Pass the result to the assertResult method.
  5. When your code is ready, run it with the following command:
    $ node callPageProvider.js
    

  • Calling Page Providers - Solution

    
                const Nuxeo = require('nuxeo');
                
                var nuxeo = new Nuxeo({
                  // Replace with your Nuxeo Server URL
                  baseURL: 'http://NUXEO_SERVER/nuxeo',
                  auth: {
                    method: 'basic',
                    username: 'Administrator',
                    password: 'Administrator'
                  },
                });
                
                nuxeo.schemas("*");
                
                // Define query options
                var queryOpts = {
                  "pageProvider": "DefaultContentListingInNavigation",
                  // Replace with UID of existing parent document
                  "queryParams": ["11857bce-4a0f-4bc2-b679-4216b4bf216b"]
                };
                
                nuxeo
                  .repository()
                  .query(queryOpts)
                  .then(function(result) {
                    console.log("QUERY RESULTS:")
                    console.log(result);
                    // Pass the call result to the method below
                    assertResult(result);
                  })
                  .catch(function(error) {
                    throw error;
                  });
                
                
                /************
                DO NOT EDIT BELOW THIS LINE
                ************/
                
                function assertResult(result) {
                  if (Array.isArray(result)) {
                    console.log("Please pass the query result to complete this exercise.");
                    return false;
                  }
                
                  if (Array.isArray(result)) {
                      if(result.length > 0) {
                        if(result[0]['entity-type'] !== 'document') {
                        console.log("You didn't retrieve any documents. Check the page provider and queryParams.");
                        return false;
                      }
                    }
                  }
                
                  console.log('Congratulations, you have successfully completed this exercise.');
                }
                

Launching NXQL Queries

  1. Download nxqlQuery.js or open in another tab to copy and paste.
  2. Replace NUXEO_SERVER with your Nuxeo Server URL.
  3. Define an NXQL query that will return all documents created TODAY. Exclude proxies, archived versions and deleted (trashed) documents.
  4. Add query to queryOpts and launch the query.
  5. Pass the result to the assertResult method.
  6. When your code is ready, run it with the following command:
    $ node nxqlQuery.js
    

  • Launching NXQL Queries - Solution

    
                const Nuxeo = require('nuxeo');
                
                var nuxeo = new Nuxeo({
                  // Replace with your Nuxeo Server URL
                  baseURL: 'http://NUXEO_SERVER/nuxeo',
                  auth: {
                    method: 'basic',
                    username: 'Administrator',
                    password: 'Administrator'
                  },
                });
                
                nuxeo.schemas("*");
                
                // Define an NXQL query that will return all documents created TODAY
                var queryToLaunch = "SELECT * FROM Document    "
                // Change to today's date
                    + " WHERE dc:created >= DATE '2017-02-09' "
                  + " AND ecm:isVersion = 0 "
                  + " AND ecm:isProxy = 0 "
                  + " AND ecm:currentLifeCycleState != 'deleted' ";
                
                // Add query to query options
                var queryOpts = {
                  "query": queryToLaunch
                };
                
                // Launch query
                nuxeo
                  .repository()
                  .query(queryOpts)
                  .then(function(result) {
                    console.log(result);
                    // Pass the call result to the method below
                    assertResult(result);
                  })
                  .catch(function(error) {
                    throw error;
                  });
                
                
                /************
                DO NOT EDIT BELOW THIS LINE
                ************/
                
                function assertResult(result) {
                  if (typeof queryToLaunch === 'undefined') {
                    console.log("\"queryToLaunch\" is undefined.");
                    return false;
                  }
                
                  if (!queryToLaunch.includes('ecm:isVersion') &&
                      !queryToLaunch.includes('ecm:isCheckedInVersion')
                    ) {
                    console.log("Exclude archived versions from query.");
                    return false;
                  }
                
                  if (!queryToLaunch.includes('ecm:isProxy')) {
                    console.log("Exclude proxies from query.");
                    return false;
                  }
                
                  if (!queryToLaunch.includes('ecm:currentLifeCycleState') ||
                      !queryToLaunch.includes('deleted')
                    ) {
                    console.log("Exclude deleted documents from query.");
                    return false;
                  }
                
                  if (!Array.isArray(result.entries)) {
                  console.log();
                  console.log(result);
                    console.log("Please pass the query result to complete this exercise.");
                    return false;
                  }
                
                  if (Array.isArray(result.entries)) {
                    if (result.length > 0) {
                      if (result[0]['entity-type'] !== 'document') {
                        console.log("You didn't retrieve any documents. Check your NXQL query and syntax.");
                        return false;
                      }
                    }
                  }
                
                  console.log('Congratulations, you have successfully completed this exercise.');
                }
                

Notes about Elasticsearch and the Passthrough API

Where is the query launched?

By default, NXQL queries use the database, but you can change this to use the ElasticSearch index when creating a page provider.

How Can I Take Advantage of all the Elasticsearch Features?

Nuxeo provides a passthrough API for Elasticsearch that lets you benefit from the Elasticsearch API while handling security. When using it, queries are written using the Elasticsearch API, but they are actually sent to Nuxeo Platform that reworks them. We won't cover this aspect during this tutorial but you can head to the Elasticsearch documentation for more information.

2 months ago Solen Guitter Add description for SEO
2 months ago Solen Guitter Formatting fix for consistency
2 months ago Solen Guitter Consistency fixes (formatting, headings, links, etc.)
2 months ago Solen Guitter Use MD format for links to files
2 months ago Karin Touchie NXDOC-822: Separate pages
History: Created by Karin Touchie