Документация InstantCMS

для администраторов и разработчиков

Инструменты пользователя

Инструменты сайта


en:dev:integration:search

Full-Text Search Integration

There is the «Search» component in InstantCMS that allows to search relevant pages on the site. The component’s architecture allows to link any engine’s component to the search.

Hook Linking

The linking of your component comes down to the creation of a hook that works according to the parallel scheme called fulltext_search. Please note that the MySQL full-text search is performed. Your table should have the corresponding index such as FULLTEXT (the index should be composite, if the search is performed on several cells). The table’s engine should be MyISAM (InnoDB is allowed if your MySQL version supports full-text InnoDB indexes).

Parameters are not passed into the hook. The hook should be returned by an array with such cells (all are required):

return array(
    'name'          => $this->name,
    'sources'       => $sources,
    'table_names'   => $table_names,
    'match_fields'  => $match_fields,
    'select_fields' => $select_fields,
    'filters'       => $filters,
    'item_callback' => function($item, $model, $sources_name, $match_fields, $select_fields){
 
        return array_merge($item, array(
            'url'      => href_to($sources_name, $item['slug'] . '.html'),
            'title'    => $item['title'],
            'fields'   => array(),
            'date_pub' => $item['date_pub'],
            'image'    => ''
        ));
 
    }
);

Decoding of response cells

Title Description
name Controller’s title, for example, guestbook.
sources An array of processed goals, for example, array('guestbook' > 'Guestbook'). The goal title may coinside with the controller title. There can be several goals.
table_names An array of tables to select the data from. The array’s key should be the goal’s title. A separate table is specified for each goal, for example, array('guestbook' > 'guestbooks') (cms_guestbooks table).
match_fields An array whose key is the goal's title and whose value is an array of fields participating in the full-text selection in the MATCH operator. For example, array('guestbook' ⇒ array('title', 'description')). You can specify a custom set of fields for each goal.
select_fields An array whose key is the goal's title and whose value is an array of fields to be selected from the table. For example, array('guestbook' ⇒ array('id', 'slug', 'title', 'description')). You can specify a custom set of fields for each goal.
filters An array whose key is the goal's title and whose value is an array of conditions and fields to filter. For example, array('guestbook' ⇒ array(array('condition' ⇒ '=','value' ⇒ 1,'field' ⇒ 'is_pub'))). In this case, the filtration will be performed by the is_pub field that must be equal to 1.
item_callbackck An Anonymous Function that processes the response from the database. The following variables are available in it: $item - an array of an entry with the fields that we requested in the select_fields cell, $model - an object of the modelSearch class, $sources_name - a goal's title, for example guestbook (this will help to form the urls of entries), $match_fields and $select_fields that contain an array of the corresponding fields mentioned above. This function must return several required cells: url - a generated link to the target entry page, title - a target entry title, date_pub - an entry date (not formatted), image - an image HTML code, if there is one. Other cells are optional, that is why it will be more practical to merge the resultant array with an array of the required cells as shown in the PHP code above.

You can view the examples of ready hooks in the files: /system/controllers/content/hooks/fulltext_search.php (a full-text search implementation in the «Content» component) and /system/controllers/photos/hooks/fulltext_search.php (a full-text search on photos).

Custom Output Template of Goal Results

By default, the engine uses the /templates/default/controllers/search/index.tpl.php template to output the search results. However, for a needed goal, you can create a custom template titled index_GOAL_TITLE.tpl.php. For example, the /templates/default/controllers/search/index_photos.tpl.php template processes the results of a search on photos. The /templates/default/controllers/search/index_guestbook.tpl.php template will maintain the results of a search on the “Guestbook” component and so on.

Search Data Processing Hooks

In addition to the above described, the «Search» component processes the following hooks after implementing the main hook:

Title Description
search_CONTROLLER_NAME_data CONTROLLER_NAME - a controller’s title, for example, guestbook (the hook will be called search_guestbook_data). Processes each response of the main fulltext_search hook’s controllers. The data returned by the specified controller is passed to it. It will be helpful to additionally filter/supplement the selection with fields or for other actions.
content_TARGET_NAME_search_list TARGET_NAME - a goal’s title, for example, guestbook (the hook will be called content_guestbook_search_list). Processes the resultant array of the response data from the search.
en/dev/integration/search.txt · Последнее изменение: 05.04.2017 17:10 — murlysja