Documentation

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:
  • QUERY - Query string. Required element. Must be compiled according to query language rules.
  • TAGS - List of tags. Resulting search will return all elements with these tags.
  • SITE_ID - Site, which data is searched. Optional parameter, equals the current value by default.
  • MODULE_ID - Code of the module, which data the search is limited to. If this parameter equals false, the search is performed for all modules. Optional parameter, equals false by default.
  • ITEM_ID - Code of the indexed element. Used to limit search area by the element code. If the parameter equals false, then the limitation is not specified. Optional parameter, equals false by default.
  • PARAM1 - First element parameter or an array of first elements. Used to limit the search range by the arbitrary parameter. If the parameter equals false, the limitation is not specified. Optional parameter, equals false by default.
  • PARAM2 - Second element parameter or an array of second parameters. Used to limit the search range by the arbitrary parameter. If the parameter equals false, the limitation is not specified. Optional parameter, equals false by default.
  • URL - address mask relative to site root, via which this element or an array of mask addresses will be available;
  • DATE_CHANGE - time when the element was modified in the site format (enables time);
  • CHECK_DATES - If specified and equal Y, then only active elements will be found;
aSort Array, containing the sorting attribute as the "field name"=>"direction" parameter sets.

Field name can have the value:
  • ID - search index ID;
  • MODULE_ID - module ID;
  • ITEM_ID - search index element ID (for example for the forum it will be a forum post ID);
  • TITLE - header;
  • PARAM1 - First element parameter or an array of first parameters. Used to limit the search range by the arbitrary parameter. If the parameter equals false, the limitation is not specified. Optional parameter, equals false by default.
  • PARAM2 - Second element parameter or an array of second parameters. Used to limit the search range by the arbitrary parameter. If the parameter equals false, the limitation is not specified. Optional parameter, equals false by default.
  • DATE_FROM - date when the element activity started;
  • DATE_TO - date when the element activity finished;
  • RANK - computed relevancy value;
  • TITLE_RANK - number of quey substring occurences in the title. Allows increasing title relevancy in search results;
  • CUSTOM_RANK - specified relevancy value;
  • DATE_CHANGE - time when the element is modified;
Sorting direction can have the following value:
  • ASC - by ascension;
  • DESC - by descension.
Example (specifically this value is considered as by-default value):
array("CUSTOM_RANK"=>"DESC", "RANK"=>"DESC", "DATE_CHANGE"=>"DESC")
In case when bTagsCloud parameter equals true, the available fields are the following:
  • DATE_CHANGE - time when the element is modified;
  • NAME - tag value;
  • CNT - tag frequency;
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:
  • STEMMING - If equals true, then morphology will be considered in the search. If false, then it will not be considered. If not specified, the module setting will be used;
Examples of complex condition:
		array(
array(
"MODULE_ID" => "forum",
"PARAM1" => array(1, 2),
),
array(
"MODULE_ID" => "iblock",
"PARAM2" => 3,
),
)
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:

ModifierExampleDescription
="=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 &lt;, &gt;, &quot;)
[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
Up