Artifactory Query Language (AQL) is a useful tool for querying the information stored in Artifactory and obtain more specific bits of information by constructing more complex queries to answer more specific use cases.
In this short article, we will use a few examples in order to get a better understanding of how to construct basic AQL queries and how to later use the queries with Artifactory.
Queries can be constructed to work on certain domains and fields in Artifactory. Domains will include items, builds, properties, and more. Each of the Domains includes fields that can be queried, for example, for the ‘item’ domain we are able to query fields such as ‘repo’, ‘name’, ‘created’, ‘modified’, and more.
For instance, this query will show the top 10 items in the specified repository, sorting them by descending order of their modification time:
items.find({"repo": "<repo_name>"}).sort({“$desc” : [”modified”]}).limit(10)
Builds that were created after July 1st 2021, that have a property with a key called ‘license’ which value is a variant of “LGPL”:
builds.find({"created" : {"$gt" : "2021-07-01"}, {“@artifactory.licenses” : {“match” : “*LGPL*”}})
All the files inder the workshop-docker-local repository that were created in the last 5 days and disk size is greater than 500:
items.find({"repo": "workshop-docker-local", "created" : {"$last" : "5d"}, "size" : {"$gt" : "500"}, "type" : "file"})
To use an AQL query in an API call, we may use the following command structure:
curl -u<user>:<password> -X POST -k -H 'Content-Type:text/plain' -i https://<artifactory_host>/artifactory/api/search/aql --data 'items.find({"repo": "<repo_name>"}).sort({"$desc" : ["modified"]}).limit(5)'
The output of the above cURL query will look similar to this:
{ "results" : [ { "repo" : "workshop-docker-local", "path" : ".", "name" : "repository.catalog", "type" : "file", "size" : 1034, "created" : "2021-06-16T15:47:10.806Z", "created_by" : "admin", "modified" : "2021-08-25T08:21:12.423Z", "modified_by" : "admin", "updated" : "2021-08-25T08:21:12.453Z" },{ "repo" : "workshop-docker-local", "path" : "asp/multi-arch-test/1.1.1-rc.1", "name" : "list.manifest.json", "type" : "file", "size" : 741, "created" : "2021-08-25T08:21:12.390Z", "created_by" : "admin", "modified" : "2021-08-25T08:21:12.354Z", "modified_by" : "admin", "updated" : "2021-08-25T08:21:12.391Z" },{ "repo" : "workshop-docker-local", "path" : "asp/multi-arch-test/1.1.1-rc.1-linux-arm64", "name" : "manifest.json", "type" : "file", "size" : 735, "created" : "2021-08-25T08:21:12.321Z", "created_by" : "admin", "modified" : "2021-08-25T08:21:12.296Z", "modified_by" : "admin", "updated" : "2021-08-25T08:21:12.323Z" },{ "repo" : "workshop-docker-local", "path" : "asp/multi-arch-test/1.1.1-rc.1-linux-amd64", "name" : "manifest.json", "type" : "file", "size" : 735, "created" : "2021-08-25T08:21:12.119Z", "created_by" : "admin", "modified" : "2021-08-25T08:21:11.437Z", "modified_by" : "admin", "updated" : "2021-08-25T08:21:12.120Z" },{ "repo" : "workshop-docker-local", "path" : "asp/multi-arch-test/sha256:c20b21f62a18b44dd62a18bc3144bb0bd369a4eb6c5a5de1e18188f291649279", "name" : "manifest.json", "type" : "file", "size" : 735, "created" : "2021-08-25T08:21:10.345Z", "created_by" : "admin", "modified" : "2021-08-25T08:21:10.316Z", "modified_by" : "admin", "updated" : "2021-08-25T08:21:10.347Z" } ], "range" : { "start_pos" : 0, "end_pos" : 5, "total" : 5, "limit" : 5 } }