Full text search engine

From phpCMS

Jump to: navigation, search

Contents

[edit] Using the full text search engine

The comprehensive search module of phpCMS does not work on the content files themselves but on its own optimized index. This makes a search faster than a direct search in the content files would be.

[edit] Creating Search and Result Pages

  • This documentation is provided by a phpCMS newbie who just figured out search using the demo-en example. Hopefully someone with more experience will come behind me to correct and amplify

The following steps are required to create the search form, integrate it into your pages, and create the search results page:

  1. Create the search form sub-template
  2. Reference the search form sub-template in your other templates
  3. Create the search results page
  4. Create the search project file
  5. Add search code to your menu template file

That's the order in which I'll present the steps, and it's mostly influenced by the order in which I made incremental progress in figuring out the search functionality myself. A more natural order might start with the project file, then the menu template, the form template, and then the results page. It really doesn't matter as all of these steps must be completed before search will work.

[edit] Create the search form sub-template

To create a search form to put on your pages, I created a sub-template. You might call this sub-template searchform.tpl. This file will contain the actual HTML form. Here's the form followed by an explanation of the various elements:

                  1.   <form action="$home/searchresults.htm" method="get">
                  2.     <input type="hidden" name="phpcms_rc" value="10" />
                  3.     <input type="hidden" name="datadir" value="$home/search" />
                  4.     <input name="query" value="" type="text" size="20" maxlength="30" />
                  5.     <input type="submit" name="submit" value="Search" />
                  6.   </form>
  1. The action will be a phpCMS content page that represents the search results. This may seem a bit strange - the form is set to go to the results before the search has even been done! The method should get GET if you're using stealth mode, and POST otherwise.
  2. The value of the phpcms_rc hidden input will be the number of results displayed on the search results page.
  3. The value of the datadir hidden input is the location on the server of the search index.
  4. The query text input is where the user enters the search term. You can make the length and maxlength anything you want.
  5. The submit button's name should be submit, but you can use any label you want in the value field.

You drop this form in your searchform.tpl. Since it's a template, you can do anything you might otherwise normally do in a template, such as use custom tags.

[edit] Reference the search form sub-template in your other templates

You insert the search form into a page by including the sub template in other templates. The code would look something like this:

{TEMPLATE FILE="$home/template/searchform.tpl"}

That's all there is to that.

[edit] Create the search results page

You need to create a search results page - this is just like any other page in a phpCMS site (more or less). The key tags that need to be in this file are:

{PROJECT}   /template/search.ini
{TITLE}     Search Results
{MENU} 00.00
{CONTENT}
Search: Results

As you see, you need a special project file for search. We'll go over that file in a second. The rest of the file is pretty self-explanatory; you'll note that I put a menu class of 00.00 because the search results don't really correspond to any menu item.

When you save this file, you might call it searchresults.htm. This is the file that was the action of your search form.

[edit] Create the search project file

You need a search project file because most likely you're going to use a different template for the search results page. This is because the search results template needs a tag called {SEARCHRESULTS}. You can refer to your "normal" tag, menu, and menu template files here if you wish - they don't have to be unique to search. Here's an example search.ini file:

 HOME              := /
 TAGS              := $home/template/my.tag
 TEMPLATE          := $home/template/searchresults.tpl
 MENU              := $home/template/my.mnu
 MENUTEMPLATE      := $home/template/my.mtpl

[edit] Add search code to your menu template file

Here's where I personally got stuck. If you started a menu template file from scratch, you may not realize that you need to put some search template code in the menu template file. This is where you define what the results themselves look like. phpCMS handles the search results the way it handles menus. There is an example of this in the demo-en/home.en.mtpl file. So, at the end of your menu template file defined in your search.ini file, add something that looks like this (this has been simplified from the demo-en example for the sake of clarity):

; Search definition - English

{SEARCH.SEARCH.PRE}
Expression(s) searched for: <QUERY_TERM>
Search time: <SEARCH_TIME> sec. Number of matches: <WORD_COUNT> Number of pages found: <PAGE_COUNT> <TERM_EXCLUDED_PRE><SEARCH_TERM_EXCLUDED><TERM_EXCLUDED_PAST> {SEARCH.SEARCH.NORMAL} {NUMBER}. - <a href="{URL}">{TITLE}</a> Ranking: {RANKING} {TEXT} Matches: {FWORDS} {SEARCH.SEARCH.PAST}


Once you have completed all of these steps, you should have phpCMS search successfully integrated into your site.

Main Page: User Documentation MainPage
« Previous Page: How to create a site map | Top Page: How to work with phpCMS | Next Page: How to enable the online editor »

Personal tools