Search
void CSearch::Search( array arParams, array aSort = array(), array aParamsEx = array(), bool bTagsCloud = false );
The method initializes the data retrieving. Non-static method.
Prior to completing search queries, the OnSearch event is called.
Method parameters
Parameter | Description | Available from version |
---|---|---|
arParams | Array, containing the search conditions as the "field name"=>"value" parameter set.
Field name can have the following value:
| |
aSort | Array, containing the sorting attribute as the "field name"=>"direction" parameter sets.
Field name can have the value:
In case when bTagsCloud parameter equals true, the available fields are the following:
| |
aParamsEx | Array of arrays, containing additional search conditions as the "field name"=>"value" parameter sets. These conditions will be united by a condition OR. See description for arParams parameter.
Also, can contain the following element:
array(In this case, search will be performed in forums with IDs 1 and 2, as well as in the information block with the ID 3. | |
bTagsCloud | Tag cloud building attribute. If specified and equal true, then the cloud tag will be built. |
Expanding filters
Starting from the module version 7.1.2, arParams and arParamsEx parameters can use key modifiers and the complex filter logic.Keys that support modifiers:
- MODULE_ID
- ITEM_ID
- PARAM1
- PARAM2
- SITE_ID
- DATE_CHANGE
- DATE_FROM
- DATE_TO
- SITE_URL
Modifiers:
Modifier | Example | Description |
= | "=MODULE_ID" => "iblock" | Retrieving exact match |
% | "%ITEM_ID" => "my" | Register-independent search by substring |
? | "?PARAM1" => "forum|iblock" | Using complex logic in the filter |
>, <, >=, <= | ">=PARAM1" => "44" | Retrieving with the use of operator |
>< | "><PARAM2" => array(10, 20) | Search range values selection |
Modifier can be preceded by the "!" symbol, then the condition will be negative. If the "LOGIC" key is available and has the "OR" value, then the values are united by the logical OR. Also, key arrays can have nesting.
Examples of a complex filter:
//Search only among information block elements, except sections and information blocks with ID 3, 5 and 7 as well as forum posts not older than 24 hours.
array( "LOGIC" => "OR", array( "=MODULE_ID" => "iblock", "!ITEM_ID" => "S%", "!=PARAM2" => array(3,5,7), ), array( "=MODULE_ID" => "forum", ">=DATE_CHANGE" => ConvertTimeStamp(time()-24*60*60, "FULL"), ) )
Returned value
None.
Array(
[ID] => Index code
[DATE_CHANGE] => Data when the indexed element is modified
[MODULE_ID] => Module, which has the indexed element
[ITEM_ID] => Indexed element code
[LID] => Language-specific mirror (language) of site
[URL] => Address relative to site root, via which this element is available (with replacement of special symbols <, >, " for <, >, ")
[TITLE] => Indexed element title (with replacement of special symbols)
[BODY] => Text of indexed element (with replacement of special symbols)
[SEARCHABLE_CONTENT] => Element index, by which the search is performed (with replacement of special symbols)
[PARAM1] => First element parameter
[PARAM2] => Second element parameter
[~URL] => Address relative to site root, by which this element is accessible
[~TITLE] => Indexed element title
[~BODY] => Indexed element text
[~SEARCHABLE_CONTENT] => Element index, by which the search is performed
[TITLE_FORMATED] => Indexed element title with selection of search words
[BODY_FORMATED] => Portion of the indexed element text with selection of search words
[URL_WO_PARAMS] => Element address without parameters, added in the OnSearch event handlers.
)
If the tag cloud build is defined, then:
Array(
[NAME] => tag
[CNT] => frequency
[DATE_CHANGE] => Upper date limit for modifying the element, containing this tag
[FULL_DATE_CHANGE] => Date when the element was modified, including the time.
)
Examples of use
Examples No.1: search of specified string within the current site forums:
<?
$q = "frined fish or herring";
$module_id = "forum";
$obSearch = new CSearch;
$obSearch->Search(array(
"QUERY" => $q,
"SITE_ID" => LANG,
"MODULE_ID" => $module_id,
));
if ($obSearch->errorno!=0):
?>
<font class="text">Error found in search phrase:</font>
<?echo ShowError($obSearch->error);?>
<font class="text">Correct search phrase and repeat the search.</font>
<?
else:
while($arResult = $obSearch->GetNext())
{?>
<a href="<?echo $arResult["URL"]?>"><?echo $arResult["TITLE_FORMATED"]?></a>
<?echo $arResult["BODY_FORMATED"]?>
<hr size="1" color="#DFDFDF">
<?}
endif;
?>
Example No.2: for a search phrase to be indexed in the Search statistics when accessing the search module, NavStart() must be called:
$obSearch = new CSearch; $obSearch->Search(array( 'QUERY' => $_REQUEST['q'], 'SITE_ID' => LANG, 'MODULE_ID' => 'iblock', )); $obSearch->NavStart(); while ($arSearch = $obSearch->Fetch()) { echo($arSearch); }
Note: such NavStart() call will produce an additional PAGEN_. If not needed, then NavStart can be replaced with the following structure:
$obSearch->Statistic = new CSearchStatistic($obSearch->strQueryText, $obSearch->strTagsText); $obSearch->Statistic->PhraseStat($obSearch->NavRecordCount, $obSearch->NavPageNomer);
© «Bitrix24», 2001-2024