Version 4 > Content Model > Search

Search

Search indexes work on similar principles to v3, but with some enhancements. Indexed URLs are stored in the searchurl table, and relevant terms in the searchterm table, as before. To get a list of links, sorted by relevance, do this:

my $search = new ExSite::Search($section_id);
$out = $search->do_search($search_string);

Results are automatically filtered for the access level of the user.

Searchurls can be tagged with a "type", which is a simple way to classify URLs. By default the type is set to the content type of the content indexed at that URL (for example, "event" or "product"), or the plug-in Module if the URL originates from a plug-in. For example, to search products only, you can append the type to the parameter list:

$out = $search->do_search($search_string, "product");

Searchurls can be tagged with a content ID, indicating the object that best represents this content from a search results point of view. (For example, a content object like a body is generally not viewed directly, so searchterms found in the body should be associated with the container page.) This allows the CMS admin tools to use the search index to find content objects:

my @content = $search->find_content($search_string);

It returns an array of content objects, most relevant first, which each have a "search_score" attribute to indicate relevance. (As above, an optional $type can also be appended to the parameter list if you want to limit your searches that way.) This allows admin control panels to make use of the system search index for their own purposes. For instance, a Catalog plugin could implement an admin search tool to find products, or a Membership plugin could search for member profiles.

Not every URL in the search index is necessarily tagged with a content ID, so public searches and admin searches will not necessarily return the same results.

Content classes can support two methods to customize their search indexes:

$c->can_index();

returns true/false depending on whether the content should add terms to the index or not. False values can distinguish between 0 (meaning permission denied) and undef (meaning the content should not be indexed). True values can distinguish between -1 (meaning the content can be indexed, but the index appears to be up to date) and 1 (meaning the content should be indexed now).

$c->search_index($search);  # $search is an ExSite::Search object

this method actually creates the search index entries.