The request objects have various properties for handling query constraints. For example, the QueryRequest object has a constraint property named documentSetConstraints. These query constraints are encoded as XML in the search request. The XML can be quite complex , often with many levels of nested tags.

The constraint properties point to components of class atg.search.routing.command.search.DocumentSetConstraint. This class has an xml property that you can use to specify the constraint XML directly.

For example, suppose you want to specify the following constraint through the QueryRequest.documentSetConstraints property:

<and>
  <strprop op="equal" name="childSKU.color">
    rose
  </strprop>
  <numprop op="lesseq" name="childSKU.price">
    30.00
  </numprop>
</and>

This constraint limits search results to products whose childSKU.color property has a value of “rose” and whose childSKU.price property has a value less than or equal to 30.00. (These properties may have multiple values, since products can have multiple SKUs, but as long as one of the values of childSKU.color is “rose” and one of the values of childSKU.price is less than or equal to 30.00, the constraint is satisfied.)

To specify this constraint, create a component of class DocumentSetConstraint and set its xml property like this:

xml=<and>\
      <strprop op="equal" name="childSKU.color">\
         rose\
      </strprop>\
      <numprop op="lesseq" name="childSKU.price">\
         30.00\
      </numprop>\
    </and>

Set QueryRequest.documentSetConstraints to this component; e.g.:

documentSetConstraints=/MyStuff/MyConstraints
Assembling the Constraint using Components

Specifying the XML directly is useful for initializing constraints, but it is not very flexible, because it doesn’t allow you to modify the XML dynamically in JSPs. Therefore the ATG platform also includes a number of subclasses of DocumentSetConstraint that allow you to specify elements of the XML as individual properties. When a query is issued, these constraint classes take care of constructing the XML and including it in the input sent to ATG Search.

To construct the constraint shown above, you’d create components of the DocumentSetConstraint subclasses StringConstraint, NumericConstraint, and ConstraintsGroup, from the atg.search.routing.command.search package.

To specify the first part of the constraint, create a StringConstraint component named /MyStuff/MyStringConstraint and set these properties:

property=childSKU.color
operation=equal
value=rose

To specify the second part, create a NumericConstraint component named /MyStuff/MyNumericConstraint and set these properties:

property=childSKU.price
operation=lesseq
value1=30

Now combine these by creating a ConstraintsGroup component named MyConstraintsGroup and setting these properties:

operation=and
constraints=/MyStuff/MyStringConstraint,/MyStuff/MyNumericConstraint

Set the QueryRequest.documentSetConstraints property to MyConstraintsGroup:

documentSetConstraints=/MyStuff/MyConstraintsGroup

This approach makes it possible to modify the constraints dynamically in JSPs. For example:

<dsp:select bean="/MyStuff/MyStringConstraint.value">
  <dsp:option value="rose">rose</dsp:option>
  <dsp:option value="mauve">mauve</dsp:option>
  <dsp:option value="gray">gray</dsp:option>
  <dsp:option value="purple">purple</dsp:option>
  <dsp:option value="cobalt">cobalt</dsp:option>
</dsp:select>
 
loading table of contents...