Sei sulla pagina 1di 6

4/5/2016

Query Builder API - docs.adobe.com

Adobe (www.adobe.comhttp://www.adobe.com/go/gnav_adobe_logo_en_us)
Products (www.adobe.comhttp://www.adobe.com/go/gnav_products_en_us) Solutions (www.adobe.comhttp://www.adobe.com/go/gnav_solutions_en_us) Learning
(www.adobe.comhttp://www.adobe.com/go/gnav_learning_en_us) Help (www.adobe.comhttp://www.adobe.com/go/gnav_help_en_us) Downloads
(www.adobe.comhttp://www.adobe.com/go/gnav_downloads_en_us) Company (www.adobe.comhttp://www.adobe.com/go/gnav_company_en_us)
Store (www.adobe.comhttp://www.adobe.com/go/gnav_store_en_us)
Adobe Store (www.adobe.comhttp://www.adobe.com/go/gnavtray_store_en_us) for home and home ofce
Education Store (www.adobe.comhttp://www.adobe.com/go/gnavtray_store_education_en_us) for students, educators, and staff
Business Store (www.adobe.comhttp://www.adobe.com/go/gnavtray_store_business_en_us) for small and medium businesses
Other ways to buy (www.adobe.comhttp://www.adobe.com/go/gnavtray_store_otherwaystobuy_en_us)

Overview (www.adobe.com/docs/en.html) /AEM 5.6.1 (www.adobe.com/docs/en/cq/5-6-1.html) /Working with Digital


Assets in DAM (www.adobe.com/docs/en/cq/5-6-1/dam.html) /Customizing and Extending CQ DAM
(www.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam.html) /

Query Builder API


You are reading the AEM 5.6.1 version of Query Builder API.
This version has reached End of core support. For further details see our technical support periods
(www.adobe.comhttps://www.adobe.com/support/products/enterprise/eol/eol_matrix.html#cq) .
This documentation is also available for the following versions: Adobe Experience Manager 6.1
(www.adobe.com/docs/en/aem/6-1/develop/search/querybuilder-api.html) AEM 6.0 (www.adobe.com/docs/en/aem/60/develop/search/querybuilder-api.html) AEM 5.6 (www.adobe.com/docs/en/cq/56/dam/customizing_and_extendingcq5dam/query_builder.html) CQ 5.5 (www.adobe.com/docs/en/cq/55/dam/customizing_and_extendingcq5dam/query_builder.html) CQ 5.4 (www.adobe.com/docs/en/cq/54/dam/customizing_and_extendingcq5dam/query_builder.html) CQ 5.3 (www.adobe.com/docs/en/cq/53/dam/customizing_and_extendingcq5dam/query_builder.html)
The functionality of the Asset Share Query Builder (www.adobe.com/docs/en/cq/5-61/dam/creating_and_conguringdamnderandasseteditorpages.html) is exposed through a Java API and a REST API.
This section describes these APIs.
The server-side query builder (QueryBuilder (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/QueryBuilder.html) ) will accept a query description, create and run an
XPath query, optionally lter the result set, and also extract facets, if desired.
The query description is simply a set of predicates (Predicate (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/Predicate.html) ). Examples include a full-text predicate, which corresponds
to the jcr:contains()function in XPath, and an image size predicate that looks for width and height properties in
the DAM asset subtree.
For each predicate type, there is an evaluator component (PredicateEvaluator
(www.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/search/eval/PredicateEvaluator.html) )
that knows how to handle that specic predicate for XPath, ltering, and facet extraction. It is very easy to create
custom evaluators, which are plugged-in through the OSGi component runtime.
The REST API provides access to exactly the same features through HTTP with responses being sent in JSON.
NOTE
The QueryBuilder API is built using the JCR API. You can also query the Adobe Experience
Manager JCR by using the JCR API from within an OSGi bundle. For information, see Querying
Adobe Experience Manager Data using the JCR API.

Sample Queries
These samples are given in Java properties style notation. To use them with the Java API, use a Java HashMapas in
the API sample that follows.
For the QueryBuilderJSON Servlet, each example includes a link to your local CQ installation (at the default
location, http://localhost:4502). Note that you have to log in to your CQ instance before using these links.
NOTE
To view the returned JSON data in your browser you may want to use a plugin such as
JSONView for Firefox.
By default, the query builder json servlet displays 10 hits. Adding the parameter of
p.limit=-1 allows the servlet to show the entire query result.

Find jar les and order them, newest rst


http://localhost:4502/bin/querybuilder.json?
type=nt:file&nodename=*.jar&orderby=@jcr:content/jcr:lastModified&orderby.sort=desc
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
type=nt:file&nodename=*.jar&orderby=@jcr:content/jcr:lastModified&orderby.sort=desc)
1
2
3
4

type=nt:file
nodename=*.jar
orderby=@jcr:content/jcr:lastModified
orderby.sort=desc
Code samples are intended for illustration purposes only.

Find all pages and order them by last modied


http://localhost:4502/bin/querybuilder.json?
type=cq:Page&orderby=@jcr:content/cq:lastModified
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?type=cq:Page&orderby=@jcr:content/cq:lastModied)
1
2

type=cq:Page
orderby=@jcr:content/cq:lastModified
Code samples are intended for illustration purposes only.

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

1/6

4/5/2016

Query Builder API - docs.adobe.com


Find all pages and order them by last modied, but descending
http://localhost:4502/bin/querybuilder.json?type=cq:Page&orderby=@jcr:content/cq:lastModied&orderby.sort=desc
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
type=cq:Page&orderby=@jcr:content/cq:lastModied&orderby.sort=desc)
1
2
3

type=cq:Page
orderby=@jcr:content/cq:lastModified
orderby.sort=desc
Code samples are intended for illustration purposes only.

Fulltext search, ordered by score


http://localhost:4502/bin/querybuilder.json?fulltext=Management&orderby=@jcr:score&orderby.sort=desc
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
fulltext=Management&orderby=@jcr:score&orderby.sort=desc)
1
2
3

fulltext=Management
orderby=@jcr:score
orderby.sort=desc
Code samples are intended for illustration purposes only.

Search for pages tagged with a certain tag


http://localhost:4502/bin/querybuilder.json?
type=cq:Page&tagid=marketing:interest/product&tagid.property=jcr:content/cq:tags
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
type=cq:Page&tagid=marketing:interest/product&tagid.property=jcr:content/cq:tags)
1
2
3

type=cq:Page
tagid=marketing:interest/product
tagid.property=jcr:content/cq:tags
Code samples are intended for illustration purposes only.

Use the tagidpredicate as in the example if you know the explicit tag ID.
Use the tagpredicate for the tag title path (without spaces).
Because, in the previous example, you are searching for pages (cq:Pagenodes), you need to use the relative path
from that node for the tagid.propertypredicate, which is jcr:content/cq:tags. By default, the
tagid.propertywould simply be cq:tags.

Search under multiple paths (using groups)


http://localhost:4502/bin/querybuilder.json?
fulltext=Management&group.1_path=/content/geometrixx/en/company/management&group.2_path=/content/geometrixx/en/company/bod&group.p.or=true
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
fulltext=Management&group.1_path=/content/geometrixx/en/company/management&group.2_path=/content/geometrixx/en/company/bod&group.p.or=true)
1
2
3
4

fulltext=Management
group.p.or=true
group.1_path=/content/geometrixx/en/company/management
group.2_path=/content/geometrixx/en/company/bod
Code samples are intended for illustration purposes only.

This query uses a group (named "group"), which acts to delimit subexpressions within a query, much as parentheses
do in more standard notations. For example, the previous query might be expressed in a more familiar style as:
"Management" and ("/content/geometrixx/en/company/management" or
"/content/geometrixx/en/company/bod")
Inside the group in the example, the pathpredicate is used multiple times. To dierentiate and order the two instances
of the predicate(ordering is required for some predicates), you must prex the predicates with N_ where N is the
ordering index. In the previous example, the resulting predicates are 1_pathand 2_path.
The pin p.oris a special delimiter indicating that what follows (in this case an or) is a parameter of the group, as
opposed to a subpredicate of the group, such as 1_path.
If no p.oris given then all predicates are ANDed together, that is, each result must satisfy all predicates.
NOTE
You cannot use the same numeric prex in one single query, even for dierent predicates.

Search for properties


Here you are searching for all pages of a given template, using the cq:templateproperty:
http://localhost:4502/bin/querybuilder.json?
property=cq%3atemplate&property.value=%2fapps%2fgeometrixx%2ftemplates%2fhomepage&type=cq%3aPageContent
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
property=cq%3atemplate&property.value=%2fapps%2fgeometrixx%2ftemplates%2fhomepage&type=cq%3aPageContent)
1
2
3

type=cq:PageContent
property=cq:template
property.value=/apps/geometrixx/templates/homepage
Code samples are intended for illustration purposes only.

This has the drawback that the jcr:contentnodes of the pages, not the pages themselves, are returned. To solve
this, you can search by relative path:
http://localhost:4502/bin/querybuilder.json?
property=jcr%3acontent%2fcq%3atemplate&property.value=%2fapps%2fgeometrixx%2ftemplates%2fhomepage&type=cq%3aPage
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
property=jcr%3acontent%2fcq%3atemplate&property.value=%2fapps%2fgeometrixx%2ftemplates%2fhomepage&type=cq%3aPage)
1
2

type=cq:Page
property=jcr:content/cq:template

property.value=/apps/geometrixx/templates/homepage

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

2/6

4/5/2016

Query Builder API - docs.adobe.com


3

property.value=/apps/geometrixx/templates/homepage
Code samples are intended for illustration purposes only.

Search for multiple properties


When using the property predicate multiple times, you have to add the number prexes again:

http://localhost:4502/bin/querybuilder.json?
1_property=jcr%3acontent%2fcq%3atemplate&1_property.value=%2fapps%2fgeometrixx%2ftemplates%2fhomepage&2_property=jcr%3acontent%2fjcr%3atitle&2_property.
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
1_property=jcr%3acontent%2fcq%3atemplate&1_property.value=%2fapps%2fgeometrixx%2ftemplates%2fhomepage&2_property=jcr%3acontent%2fjcr%3atitle&2_property.
1
2
3
4
5

type=cq:Page
1_property=jcr:content/cq:template
1_property.value=/apps/geometrixx/templates/homepage
2_property=jcr:content/jcr:title
2_property.value=English
Code samples are intended for illustration purposes only.

Search for multiple property values


To avoid big groups when you want to search for multiple values of a property ("A" or "B" or "C"), you can
provide multiple values to the propertypredicate:
http://localhost:4502/bin/querybuilder.json?
property=jcr%3atitle&property.1_value=Products&property.2_value=Square&property.3_value=Events
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
property=jcr%3atitle&property.1_value=Products&property.2_value=Square&property.3_value=Events)
1
2
3
4

property=jcr:title
property.1_value=Products
property.2_value=Square
property.3_value=Events
Code samples are intended for illustration purposes only.

For multi-value properties, you can also require that multiple values match ("A" and "B" and "C"):
http://localhost:4502/bin/querybuilder.json?
property=jcr%3atitle&property.and=true&property.1_value=test&property.2_value=foo&property.3_value=bar
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
property=jcr%3atitle&property.and=true&property.1_value=test&property.2_value=foo&property.3_value=bar)
1
2
3
4
5

property=jcr:title
property.and=true
property.1_value=test
property.2_value=foo
property.3_value=bar
Code samples are intended for illustration purposes only.

Rening What Is Returned


By default, the QueryBuilderJSON Servlet will return a default set of properties for each node in the search result
(e.g. path, name, title, etc.). In order to gain control over which properties are returned, you can do one of the following:
Specify p.hits=full, in which case all properties will be included for each node:
http://localhost:4502/bin/querybuilder.json?
p.hits=full&property=jcr%3atitle&property.value=Triangle

1
2
3

property=jcr:title
property.value=Triangle
p.hits=full
Code samples are intended for illustration purposes only.

Use p.hits=selectiveand specify the properties you want to get in p.properties, separated by a space:
http://localhost:4502/bin/querybuilder.json?
p.hits=selective&p.properties=sling%3aresourceType%20jcr%3aprimaryType&property=jcr%3atitle&property.value=Triangle
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
p.hits=selective&p.properties=sling%3aresourceType%20jcr%3aprimaryType&property=jcr%3atitle&property.value=Triangle)
1
2
3
4

property=jcr:title
property.value=Triangle
p.hits=selective
p.properties=sling:resourceTypejcr:primaryType
Code samples are intended for illustration purposes only.

Another thing you can do is include child nodes in the QueryBuilderresponse. In order to do this you need to
specify p.nodedepth=n, where n is the number of levels you want the query to return. Note that, in order for a child
node to be returned, it must be specied by the properties selector (p.hits=full). Example:
http://localhost:4502/bin/querybuilder.json?
p.hits=full&p.nodedepth=5&property=jcr%3atitle&property.value=Triangle
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
p.hits=full&p.nodedepth=5&property=jcr%3atitle&property.value=Triangle)
1
2
3
4

property=jcr:title
property.value=Triangle
p.hits=full
p.nodedepth=5
Code samples are intended for illustration purposes only.

More Predicates
For more predicates, see the Javadoc for the*PredicateEvaluatorclasses (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/eval/PredicateEvaluator.html) . The Javadoc for these classes contains the list of

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

3/6

4/5/2016

Query Builder API - docs.adobe.com


properties that you can use.
The prex of the class name (for example, "similar" in SimilarityPredicateEvaluator
(www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/eval/SimilarityPredicateEvaluator.html) ) is theprincipalproperty of
the class. This property is also the name of the predicate to use in the query (in lower case).
For suchprincipal properties, you can shorten the query and use "similar=/content/en" instead of the fully
qualied variant "similar.similar=/content/en". The fully qualied form must be used for all non-principal
properties of a class.

Example Query Builder API Usage


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

StringfulltextSearchTerm="Geometrixx"

Map<String,String>map=newHashMap<String,String>()

map.put("path","/content")
map.put("type","cq:Page")
map.put("group.p.or","true")
map.put("group.1_fulltext",fulltextSearchTerm)
map.put("group.1_fulltext.relPath","jcr:content")
map.put("group.2_fulltext",fulltextSearchTerm)
map.put("group.2_fulltext.relPath","jcr:content/@cq:tags")

map.put("p.offset","0")
map.put("p.limit","20")

Queryquery=builder.createQuery(PredicateGroup.create(map),session)
query.setStart(0)
query.setHitsPerPage(20)

SearchResultresult=query.getResult()

inthitsPerPage=result.getHits().size()
longtotalMatches=result.getTotalMatches()
longoffset=result.getStartIndex()
longnumberOfPages=totalMatches/20

DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance()
DocumentBuilderbuilder=factory.newDocumentBuilder()
Documentdoc=builder.newDocument()

Elementroot=doc.createElement("results")
doc.appendChild(root)

for(Hithit:result.getHits()){
Stringpath=hit.getPath()

Elementresultel=doc.createElement("result")
root.appendChild(resultel)

Elementpathel=doc.createElement("path")
pathel.appendChild(doc.createTextNode(path))
resultel.appendChild(pathel)
}
Code samples are intended for illustration purposes only.
NOTE
To learn how to build an OSGi bundle that uses the QueryBuilder API and use that OSGi bundle
within an Adobe Experience Manager application, see Creating Adobe CQ OSGi bundles that
use the Query Builder API.

The same query executed over HTTP using the Query Builder (JSON) Servlet:

http://localhost:4502/bin/querybuilder.json?
path=/content&type=cq:Page&group.p.or=true&group.1_fulltext=Geometrixx&group.1_fulltext.relPath=jcr:content&group.2_fulltext=Geometrixx&group.2_fulltext
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?
path=/content&type=cq:Page&group.p.or=true&group.1_fulltext=Geometrixx&group.1_fulltext.relPath=jcr:content&group.2_fulltext=Geometrixx&group.2_fulltext.relPath=jcr:content/@cq:tags&p.oset=0&p.li

Storing and loading queries


Queries can be stored to the repository so that you can use them later. The QueryBuilderprovides the storeQuery
method with the following signature:
1

voidstoreQuery(Queryquery,Stringpath,booleancreateFile,Sessionsession)throws
Code samples are intended for illustration purposes only.

When using the QueryBuilder#storeQuery(www.adobe.comhttp://dev.day.com/docs/en/cq/5-61/javadoc/com/day/cq/search/QueryBuilder.html#storeQuery(com.day.cq.search.Query,%20java.lang.String,%20boolean,%20javax.jcr.Session))


method, the given Queryis stored into the repository as a le or as a property according to the createFileargument
value. The following example shows how to save a Queryto the path /mypath/getfilesas a le:
1

builder.storeQuery(query,"/mypath/getfiles",true,session)
Code samples are intended for illustration purposes only.

Any previously stored queries can be loaded from the repository by using the QueryBuilder#loadQuery
(www.adobe.comhttp://dev.day.com/docs/en/cq/5-61/javadoc/com/day/cq/search/QueryBuilder.html#loadQuery(java.lang.String,%20javax.jcr.Session))
method:

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

4/6

4/5/2016

Query Builder API - docs.adobe.com


1

QueryloadQuery(Stringpath,Sessionsession)throwsRepositoryException,IOException
Code samples are intended for illustration purposes only.

For example, a Querystored to the path /mypath/getfilescan be loaded by the following snippet:
1

QueryloadedQuery=builder.loadQuery("/mypath/getfiles",session)
Code samples are intended for illustration purposes only.

Testing and Debugging


For playing around and debugging querybuilder queries, you can use the QueryBuilder debugger console at
http://localhost:4502/libs/cq/search/content/querydebug.html
(www.adobe.comhttp://localhost:4502/libs/cq/search/content/querydebug.html)
or alternatively the querybuilder json servlet at
http://localhost:4502/bin/querybuilder.json?path=/tmp
(www.adobe.comhttp://localhost:4502/bin/querybuilder.json?path=/tmp)
(path=/tmpis only an example).

Debugging Queries with Logging


NOTE
The conguration of the loggers is described in the sectionCreating Your Own Loggers and
Writers (www.adobe.com/docs/en/cq/5-61/deploying/congure_logging.html#Creating%20Your%20Own%20Loggers%20and%20Writers)
.

The log output (INFO level) of the query builder implementation when executing the query described in Testing and
Debugging:
1
2
3
4
5
6
7
8
9
10
11
12

com.day.cq.search.impl.builder.QueryImplexecutingquery(predicatetree):
null=group:limit=20,offset=0[
{group=group:or=true[
{1_fulltext=fulltext:fulltext=Geometrixx,relPath=jcr:content}
{2_fulltext=fulltext:fulltext=Geometrixx,relPath=jcr:content/@cq:tags}
]}
{path=path:path=/content}
{type=type:type=cq:Page}
]
com.day.cq.search.impl.builder.QueryImplXPathquery:/jcr:root/content//element(*,cq:Page)[(jcr:contains(jcr:content,"Geometrixx")orjcr:conta
com.day.cq.search.impl.builder.QueryImplnofilteringpredicates
com.day.cq.search.impl.builder.QueryImplqueryexecutiontook69ms
Code samples are intended for illustration purposes only.

If you have a query using predicate evaluators that lter or that use a custom order by comparator, this will also be
noted in the query:
1
2
3
4
5
6
7
8
9
10

com.day.cq.search.impl.builder.QueryImplexecutingquery(predicatetree):
null=group:[
{nodename=nodename:nodename=*.jar}
{orderby=orderby:orderby=@jcr:content/jcr:lastModified}
{type=type:type=nt:file}
]
com.day.cq.search.impl.builder.QueryImplcustomorderbycomparator:jcr:content/jcr:lastModified
com.day.cq.search.impl.builder.QueryImplXPathquery://element(*,nt:file)
com.day.cq.search.impl.builder.QueryImplfilteringpredicates:{nodename=nodename:nodename=*.jar}
com.day.cq.search.impl.builder.QueryImplqueryexecutiontook272ms
Code samples are intended for illustration purposes only.

Javadoc Links
Javadoc

Description

com.day.cq.search (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/package-summary.html)

Basic QueryBuilder and Query


API

com.day.cq.search.result (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/result/package-summary.html)

Result API

com.day.cq.search.facets (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/facets/package-summary.html)

Facets

com.day.cq.search.facets.buckets (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/facets/buckets/package-summary.html)

Buckets (contained within facets)

com.day.cq.search.eval (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/eval/package-summary.html)

Predicate Evaluators

com.day.cq.search.facets.extractors (www.adobe.com/docs/en/cq/56-1/javadoc/com/day/cq/search/facets/extractors/packagesummary.html)

Facet Extractors (for evaluators)

com.day.cq.search.writer (www.adobe.com/docs/en/cq/5-61/javadoc/com/day/cq/search/writer/package-summary.html)

JSON Result Hit Writer for


Querybuilder servlet
(/bin/querybuilder.json)

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

5/6

4/5/2016

Query Builder API - docs.adobe.com

(www.adobe.comhttp://www.adobe.com/go/gfooter_truste_logo)
Copyright 2009-2016 Adobe Systems Incorporated. All rights reserved.
Use of this website signies your agreement to the Terms of Use (www.adobe.comhttp://www.adobe.com/go/gfooter_terms_of_use) and Online Privacy Policy
(www.adobe.comhttp://www.adobe.com/go/gfooter_privacy_policy) .

https://docs.adobe.com/docs/en/cq/5-6-1/dam/customizing_and_extendingcq5dam/query_builder.html

6/6

Potrebbero piacerti anche