content.queryΒΆ

This command queries content. See Query Language for details.

Arguments:

start (integer)
Start index (used for paging). Default is 0.
count (integer)
Number of contents to fetch. Default is 10.
query (string)
Query expression.
sort (string)
Sorting expression.
aggregations (object)
Aggregations expression.
contentTypes (string[])
Content types to filter on.
branch (string)
Set by portal, depending on context, to either “draft” or “master”. May be overridden, but this is not recommended. Default is the current branch set in portal.

Example:

var result = execute('content.query', {
    start: 0,
    count: 100,
    query: "type = 'article' AND fulltext('myField', 'searching for cheese', 'AND')",
    sort: "modifiedTime DESC, geodistance('p1', 'p2')",
    contentTypes: [
        "mymodule:article",
        "mymodule:comment"
    ],
    aggregations: {
        genders: {
            terms: {
                field: "gender",
                order: "_count asc",
                size: 2
            }
        },
        by_month: {
            date_histogram: {
                field: "init_date",
                interval: "1m",
                minDocCount: 0
            }
        }
    }
});

log.info('Found ' + result.total + ' number of contents');

for (var i = 0; i < result.contents.length; i++) {
    var content = result.contents[i];
    log.info('Content ' + content._name + ' loaded');
}

Result:

{
  "aggregations": {
    "by_month": {
      "buckets": [
        {
          "doc_count": 8,
          "key": "2014-01"
        },
        {
          "doc_count": 10,
          "key": "2014-02"
        },
        {
          "doc_count": 12,
          "key": "2014-03"
        }
      ]
    },
    "genders": {
      "buckets": [
        {
          "doc_count": 10,
          "key": "male"
        },
        {
          "doc_count": 12,
          "key": "female"
        }
      ]
    }
  },
  "contents": [
    {
      "_createdTime": "1970-01-01T00:00:00Z",
      "_creator": "user:system:admin",
      "_id": "111111",
      "_modifiedTime": "1970-01-01T00:00:00Z",
      "_modifier": "user:system:admin",
      "_name": "mycontent",
      "_path": "/a/b/mycontent",
      "data": {},
      "displayName": "My Content",
      "draft": false,
      "hasChildren": false,
      "x": {},
      "page": {},
      "type": "system:unstructured"
    },
    {
      "_createdTime": "1970-01-01T00:00:00Z",
      "_creator": "user:system:admin",
      "_id": "222222",
      "_modifiedTime": "1970-01-01T00:00:00Z",
      "_modifier": "user:system:admin",
      "_name": "othercontent",
      "_path": "/a/b/othercontent",
      "data": {},
      "displayName": "Other Content",
      "draft": false,
      "hasChildren": false,
      "x": {},
      "page": {},
      "type": "system:unstructured"
    }
  ],
  "total": 2
}