Setting Up Advanced Search

To create a custom search capability, you need to:

  1. Create the custom search form
  2. Create the results page

We'll address these in order, but to help make sense out of the first part, it is important to realize that the search results page will use an interator with the attribute of condition="auto". This will pick up the conditions that our form provides as HTML Post data.

Creating the Search Form

The search form can display whatever fields you want to test. You can use text input elements, or you can create HTML selects by iterating through the terms in a taxonomy, or through categories. You can also use a JavaScript date-picker or auto-complete widget to make the form easier to use.

Here's an example of allows you to search a Company item type by either name or state:

<form id="searchForm" action="/company_search" method="post">

  <p>

    <label>Company Name:</label>

    <input name="condition[name]" type="text" />

  </p>

  <p>

  <label>Pick a State:</label>

  <select name="condition[company.state]">

    <option value=""></option>

    <w:taxonomy:each name="states" sort="name">

      <option value="<w:name />"><w:name /></option>

    </w:taxonomy:each>  

  </select>

  </p>

  <input type="select" >Search</input>

</form>

To enable the condition="auto" attribute in your results page to automatically pick up the condition, you must name the fields as above, using the convention of "condition[field name]", where the condition and square brackets are literals and between the brackets you put the fieldname you want to test.

See the article Using Compound Conditions for details on the field types.

Typically, you'll use the name of the field directly for built-in types:

<input name="condition[name]" type="text" />

or, for a select element that lists all the subcategories of the category "pets":

<select name="condition[categories]">

  <option value="">-- Select --</option>

  <w:kb:category category="pets">  

    <w:each relative='children'>

       <option value="<w:name/>"><w:name/></option> 

    </w:each>

  </w:kb:category>

</select>

For taxonomies, you have to provide a hint on where the taxonomy applies (you can't just search for any use of the taxonomy at the moment). Specify the custom item type and fieldname of the taxonomy usage (here, the state field in the school custom item type):

<label>Pick a School's state:</label>

<select name="condition[school.state]">

    <option value=""></option>

    <w:taxonomy:each name="states" sort="name">

       <option value="<w:name />"><w:name /></option>

    </w:taxonomy:each>  

</select>

Searching on related items requires that you specify the relationship between the custom item type, the related item type, and the field you are searching on. In this example, we are picking from a list of contributors to a school by name:

<label>Contributor:</label>

<select name="condition[school.contributor.name]">

    <option value="">-- Select --</option>

    <w:item:each type="contributor"  sort="name asc">  

        <option value="<w:name/>"><w:name/></option>  

    </w:item:each>  

</select>

In this example, "school" is the custom item type name, "contributor" is the name of the related item field, and "name" is the field within the contributor item that we want to test.

Comparison Operators in Forms

If no condition is supplied, the assumed operation is "=", which requires an exact match.

If you supply a form element using the name of "condition_op[field name]" and a value of the desired operator (such as >, >=, ~, etc.; see the article Using Compound Conditions for the complete list and their meanings), that operator will be used. For example, if you have a name field and want to do a "contains" comparison, you might have HTML like this:

<input name="condition[name]"  type="text"  size="30" />

<input name="condition_op[name]" value="~" type='hidden' />


Related Articles