Building Content Queries with Expressions

This topic provides guidance for building advanced content queries with the WebLogic Portal Expression language. The advanced query-building window is available from content-related JSP tags and when building queries for placeholders, campaigns, and content selectors.

Jump to: Guidelines for Complex Queries | Sample Queries

About Queries

There are three parts to a query:

<property> <comparator> <value>

Here's the basic logic of a query: In the BEA Virtual Content Repository, compare a content property to a value you enter. If the comparison is true, retrieve all matching content items. For example:

employee_type == 'manager' In this query, any content item with a property called "employee_type" that contains the exact (==) String "manager" will be retrieved from the BEA Virtual Content Repository.

Queries are often made up of multiple clauses using "and" and "or" logic. For example:

(genre == 'rock' || genre == 'alternative') &&
platinum_records > 2
In this query, any content item with a "genre" property that has an exact value of "rock" OR (||) "alternative" AND (&&) with a "platinum_records" property value greater than "2" will be retrieved. Notice the parentheses separate one section of the query from the next, controlling the order of evaluation.
Query-Building Tips

Properties

There are two types of properties for content, and you can use both in queries: user-defined content properties and explicit (system) content properties.

User-defined content properties are stored in sets called "types," which are defined in the WebLogic Administration Portal. For example, you can create a type called "Book" and add properties like "title", "author", "pub_date", and "isbn".

Explicit content properties are automatically associated with all content items and are automatically assigned values for a content item when that content item is added to the content repository. These properties, which are also listed in the com.bea.content.expression.Search class, are:

Property API Description
cm_uid Node.id.uid The unique ID for a content item. You can view a content item's unique ID by selecting the content item in the WebLogic Administration Portal and viewing the description in the Edit Content window.
cm_createdDate Node.createDate The date on which a content item was created. You can view Creation Date information for content items by selecting their content folder in the WebLogic Administration Portal and selecting the Browse Children page.
cm_createdBy Node.createdBy The user who created the content item. You can view Created By information for content items by selecting their content folder in the WebLogic Administration Portal and selecting the Browse Children page.
cm_modifiedDate Node.modifiedDate The date the content item was last modified. You can view Modified Date information for content items by selecting their content folder in the WebLogic Administration Portal and selecting the Browse Children page.
cm_nodeName Node.name The name of the content item, as shown the WebLogic Administration Portal.
cm_path Node.path The Virtual Content Repository path to the content item. For example,
'/BEA Repository/juvenilebooks/TheCrazyAdventure'.
cm_isHierarchy Node.type == Node.HIERARCHY Identifies a content folder rather than a content item. Content folders can contain content items and child content folders. When using this property in queries, compare it using a Boolean value of true or false.
cm_isContent Node.type == Node.CONTENT Identifies a content item rather than a content folder. Content items contain properties from the type with which they are associated. When using this property in queries, compare it using a Boolean value of true or false.
cm_objectClass Node.objectClass.name The content type associated with a content item. You can view Type information for content items by selecting their content folder in the WebLogic Administration Portal and selecting the Browse Children page, or by selecting a content item and viewing its header row on the Edit Content page.
cm_contentType BinaryValue.contentType The mime type for content item binary properties. For example, 'image/jpeg'. You can view the mime type of a content item by selecting it in the WebLogic Administration Portal and looking at the binary property information.
cm_binarySize BinaryValue.size
(For Node Properties)

The size of the binary value of content items. You can view the size of a content item's binary value by selecting the content item in the WebLogic Administration Portal, clicking Download File on the binary property, and right-clicking the displayed binary file to view the file properties.

When using this property in a query, specify binary size in bytes.

cm_binaryName BinaryValue.name
(For Node Properties)
The file name of the binary value of a content item. You can view the name of a content item's binary value by selecting the content item in the WebLogic Administration Portal and viewing the File Name value.

In a query, you can specify property names by themselves without quotes.

For example: title <comparator> <value>.

However, if a property name contains spaces, double quotes, or dashes, you must enclose the property name within a toProperty() format.

For example: toProperty('Favorite Author') <comparator> <value>.

The query looks for the property name in all content types and returns any content item with that property value (if the content item meets the conditions of the query). However, you can also isolate a property within a specific type. For example, if you have a type "books" and a type "articles" that both contain a "title" property, you can retrieve content items with a specific "title" from within only the "book" type using the cm_objectClass property.

For example: title likeignorecase '*Adventure' && cm_objectClass = 'books'

Comparators

Comparators provide the logic that compares a query's property to the value you enter. If a content item meets the conditions of the query, the content item is returned. Following are the comparators you can use in your queries.

Comparator Description (property formats the comparator can act on)
= or ==

Checks to see if a single-value property (including a single value containing a list) is exactly equal to the case-sensitive value(s) you enter. (Boolean, Date/Time, numeric, and String values) See an example.

!=

Checks to see if a single-value property (including a single value containing a list) is not equal to the case-sensitive value(s) you enter. (Boolean, Date/Time, numeric, and String values) See an example.

> Checks to see if a single-value property is greater than the value you specify. (Date/Time and numeric values)
See an example
.
< Checks to see if a single-value property is less than the value you specify. (Date/Time and numeric values)
See an example
.
>= Checks to see if a single-value property is greater than or equal to the value you specify. (Date/Time and numeric values) See an example.
<= Checks to see if a single-value property is less than or equal to the value you specify. (Date/Time and numeric values) See an example.
like

Checks to see if a single-value property is like the case-sensitive value you enter. You can use wildcard characters * (one or more characters) or ? (single character). (String values) See an example.

likeignorecase

Checks to see if a single-value property is like the value you enter. You can use the wildcard characters * and ?. Character case is ignored. (String values) See an example.

contains

Checks to see if a multi-value property contains exactly the single value you specify. (Date/Time, numeric, and String values) See an example.

In some implementations, this comparator may also work against single-value properties.

containsall

Checks to see if a multi-value property contains all of the exact values you specify. (Date/Time, numeric, and String values) See an example.

In some implementations, this comparator may also work against single-value properties.

containsany

Checks to see if a multi-value property contains any of the exact values you specify. (Date/Time, numeric, and String values) See an example.

In some implementations, this comparator may also work against single-value properties.

in

Checks to see if a single-value property contains any of the values you enter. If the value you enter is not a list of possible values (is a single value), "in" is the same as "=" or "==". (Date/Time, numeric, and String values) See an example.

Values

Values represent the content you want the query to return. By supplying values to a query, you're telling the query which content to retrieve (or ignore) based on the values stored on the content items.

For example, if you have a content type called "book" with a property called "title", all content items you associate with the "book" type have a "title" field. Each "title" value is unique, so in a query, you can enter the unique value you want, resulting in the query retrieving a specific content item (or ignoring the content item, depending on the comparator you use).

You can enter values in two ways: hard code them in the queries or get them from user profiles and other types of property sets (session and request).

With hard-coded values, you get predictability with your queries, because you can pinpoint the specific content you want to retrieve.

When you populate values from user profile, session, or request property sets, the value in each specified user profile/session/request property is inserted programmatically into the query, letting you create personalized queries based on the current user's preferences or the current session or request. For example, in a query where a content property is "author", you can get the value of the user's "FavoriteAuthor" profile property to supply the value for the query, letting the query retrieve content associated with the user's favorite author.

Following are guidelines for building values in queries:

Guidelines for Complex Queries

You can combine multiple independent query clauses, tying them together with and (&&) and or (||) logic and controlling the order of evaluation with parentheses the way you would with algebraic expressions. This lets you create more complex queries. You can also include "not" logic (!) in complex queries by using the exclamation point in front of a parenthetical grouping. The sample queries in this topic provide examples of complex queries. There is also an example that uses "not" logic.

Sample Queries

The following figure shows the properties set on a graphic stored in BEA's Virtual Content Repository.

The example queries following the figure are written using properties in this sample content item, and the description for each sample query tells you whether or not the query will retrieve the content item.

Query Will retrieve sample? Description

genre == 'fantasy'

Yes Retrieves any content item that has case-sensitive fantasy as the value for the genre property.
will_visit_schools == true Yes

Retrieves any content item with the will_visit_schools property set to true.
Note: There are no single quotes around true, and true is lowercase.

genre != 'mystery'
Yes Retrieves any content item that does not have case-sensitive mystery as the value for the genre property.
pub_date > toDate('MM-dd-yyyy', '01-01-2000') Yes Retrieves any content item with a pub_date property set to a value later than January 1, 2000.
books in series < 3 No Because the property name contains spaces, you must use toProperty(), as shown in the next example.

toProperty('books in series') < 3

Yes Retrieves any content item with a value less than 3 for the books in series property.
toProperty('books in series') >= 3 No Retrieves any content item with value greater than or equal to 3 for the books in series property. The property value of the sample content item is 1.
toProperty('books in series') <= 3 Yes Retrieves any content item with value less than or equal to 3 for the books in series property.

author like 'P?nm*'

Yes Retrieves any content that contains case-sensitive Penman, Panmen, or any other variation with a different character between the "P" and the "n" and any characters after the "m" for the author property.
Note: With no asterisk (*) at the end, the content item would not be retrieved, because more text follows the name Penman in the property value.

author likeignorecase 'pen*'

Yes Retrieves any content with an author value that begins with pen in any case combination, such as penman, Penman, Penfield, and so on.

genre contains 'fantasy'

Yes Retrieves any content containing an genre value of exactly fantasy.
genre contains 'child*' No The contains comparator does not allow wildcard characters.
genre containsall ('fantasy', 'children') Yes Retrieves any content that contains genre property values of fantasy and children.

genre containsall ('fantasy', 'children', 'scifi')

No Retrieves any content that contains genre property values of fantasy, children, and scifi. The sample content item contains fantasy and children, but not scifi.
genre containsany ('fantasy', 'children', 'scifi') Yes Retrieves any content that contains genre property values of fantasy, children, or scifi.
isbn in ('pending', 'not_available') Yes Retrieves any content that contains the isbn property value of either pending or not_available.
Complex queries
toProperty('books in series') >= 3 &&
pub_date > toDate('MM-yyyy','1-2000')
No Retrieves books that are part of a trilogy that were also published after January 2000.
(genre contains 'children' || keywords like '*children*') && will_visit_schools == true Yes Retrieves books with a genre value set to children or has *children* in the keyword value; and whose author is available to visit schools.
((genre contains 'children' || keywords like '*children*') && will_visit_schools == true) && isbn != 'pending' No

Retrieves books with a genre value set to children or has *children* in the keyword value; and whose author is available to visit schools; and with an isbn value that does not equal pending (meaning the book is not yet published).

Notice the parenthetical nesting that controls the order of evaluation.

(title likeignorecase '*adventure' || genre contains 'fantasy') && (pub_date >= toDate('MM-yyyy', '01-2004') || isbn == 'pending') Yes Retrieves books whose title contains *adventure or whose genre contains fantasy; and whose pub_date is after January 2004 or whose isbn is still pending (not yet published).
(genre containsany userProperty('userpreferences', 'BookGenre') && (keywords likeignorecase '*pixies')) ||
author likeignorecase userProperty('userpreferences', 'FavoriteAuthor')
Depends

This query reads the user's profile properties and uses specific property values to supply the values in the query. This query provides personalized content retrieval, because retrieved content is based on user preferences.

For example, if the current user has her BookGenre property set to mystery and her FavoriteAuthor set to Penman, Piper, this query will return the sample content; because even though in the first clause the BookGenre value doesn't match, the FavoriteAuthor in the OR'd (||) second clause does match.

Note: If you are using the Property Control or the setProperty JSP Tag to set user property sets and properties programmatically (rather than creating property sets in WebLogic Workshop), you can still use userProperty() in your queries.

Other useful queries (not related to the sample content)
language == userProperty('userpreferences', 'userPreferredLang') This approach lets you serve language-appropriate content to each user based on the user language preference (userPreferredLang) stored in a property set. You could also use sessionProperty() to get the language preference from the session; or you could use requestProperty('DefaultRequestPropertySet', 'Locale'), which returns the user's locale string, such as en-US.
((UserAge <= 35 && colors contains 'red' ||
UserAge > 35 && !(colors contains 'black')) &&
mimeType == 'text/html') &&
toProperty('Launch Date') < now && !(expireDate > toDate('MM-yyyy', '12-2004'))
This query uses "not" logic, as shown in the !(colors contains 'black') clause.

Related Topics

com.bea.content.expression.ExpressionHelper class

com.bea.content.expression.Search class

WebLogic Portal Expression Guide

Managing BEA Repositories