• Модуль: search
  • Путь к файлу: ~/bitrix/modules/search/classes/general/search.php
  • Класс: CAllSearch
  • Вызов: CAllSearch::GetFreqStatistics
function GetFreqStatistics($lang_id, $arStem, $site_id = "")
{
	$DB = CDatabase::GetModuleConnection('search');
	$sql_site_id = $DB->ForSQL($site_id);
	$sql_lang_id = $DB->ForSQL($lang_id);
	$sql_stem = array();
	foreach ($arStem as $stem)
		$sql_stem[] = $DB->ForSQL($stem);

	$limit = COption::GetOptionInt("search", "max_result_size");
	if ($limit < 1)
		$limit = 500;

	$arResult = array();
	foreach ($arStem as $stem)
		$arResult[$stem] = array(
			"STEM" => false,
			"FREQ" => 0,
			"TF" => 0,
			"STEM_COUNT" => 0,
			"TF_SUM" => 0,
		);

	if (BX_SEARCH_VERSION > 1)
		$strSql = "
			SELECT s.ID, s.STEM, FREQ, TF
			FROM b_search_content_freq f
			inner join b_search_stem s on s.ID = f.STEM
			WHERE LANGUAGE_ID = '".$sql_lang_id."'
			AND s.STEM in ('".implode("','", $sql_stem)."')
			AND ".($site_id <> ''? "SITE_ID = '".$sql_site_id."'": "SITE_ID IS NULL")."
			ORDER BY STEM
		";
	else
		$strSql = "
			SELECT STEM ID,STEM, FREQ, TF
			FROM b_search_content_freq
			WHERE LANGUAGE_ID = '".$sql_lang_id."'
			AND STEM in ('".implode("','", $sql_stem)."')
			AND ".($site_id <> ''? "SITE_ID = '".$sql_site_id."'": "SITE_ID IS NULL")."
			ORDER BY STEM
		";

	$rs = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__); while ($ar = $rs->Fetch()) { if ($ar["TF"] <> '') $arResult[$ar["STEM"]] = $ar; } $arMissed = array(); foreach ($arResult as $stem => $ar) if (!$ar["STEM"]) $arMissed[] = $DB->ForSQL($stem); if (count($arMissed) > 0) { if (BX_SEARCH_VERSION > 1) $strSql = " SELECT s.ID, s.STEM, floor(st.TF/100) BUCKET, sum(st.TF/10000) TF_SUM, count(*) STEM_COUNT FROM b_search_content_stem st inner join b_search_stem s on s.ID = st.STEM ".($site_id <> ''? "INNER JOIN b_search_content_site scsite ON scsite.SEARCH_CONTENT_ID = st.SEARCH_CONTENT_ID AND scsite.SITE_ID = '".$sql_site_id."'": "")." WHERE st.LANGUAGE_ID = '".$sql_lang_id."' AND s.STEM in ('".implode("','", $arMissed)."') GROUP BY s.ID, s.STEM, floor(st.TF/100) ORDER BY s.ID, s.STEM, floor(st.TF/100) DESC "; else $strSql = " SELECT st.STEM ID, st.STEM, floor(st.TF*100) BUCKET, sum(st.TF) TF_SUM, count(*) STEM_COUNT FROM b_search_content_stem st ".($site_id <> ''? "INNER JOIN b_search_content_site scsite ON scsite.SEARCH_CONTENT_ID = st.SEARCH_CONTENT_ID AND scsite.SITE_ID = '".$sql_site_id."'": "")." WHERE st.LANGUAGE_ID = '".$sql_lang_id."' AND st.STEM in ('".implode("','", $arMissed)."') GROUP BY st.STEM, floor(st.TF*100) ORDER BY st.STEM, floor(st.TF*100) DESC "; $rs = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__); while ($ar = $rs->Fetch()) { $stem = $ar["STEM"]; if ($arResult[$stem]["STEM_COUNT"] < $limit) $arResult[$stem]["TF"] = $ar["BUCKET"] / 100.0; $arResult[$stem]["STEM_COUNT"] += $ar["STEM_COUNT"]; $arResult[$stem]["TF_SUM"] += $ar["TF_SUM"]; $arResult[$stem]["DO_INSERT"] = true; $arResult[$stem]["ID"] = $ar["ID"]; } } foreach ($arResult as $stem => $ar) { if ($ar["DO_INSERT"]) { $FREQ = intval(defined("search_range_by_sum_tf")? $ar["TF_SUM"]: $ar["STEM_COUNT"]); $strSql = " UPDATE b_search_content_freq SET FREQ=".$FREQ.", TF=".number_format($ar["TF"], 2, ".", "")." WHERE LANGUAGE_ID='".$sql_lang_id."' AND ".($site_id <> ''? "SITE_ID = '".$sql_site_id."'": "SITE_ID IS NULL")." AND STEM='".$DB->ForSQL($ar["ID"])."' "; $rsUpdate = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__); if ($rsUpdate->AffectedRowsCount() <= 0) { $strSql = " INSERT INTO b_search_content_freq (STEM, LANGUAGE_ID, SITE_ID, FREQ, TF) VALUES ('".$DB->ForSQL($ar["ID"])."', '".$sql_lang_id."', ".($site_id <> ''? "'".$sql_site_id."'": "NULL").", ".$FREQ.", ".number_format($ar["TF"], 2, ".", "").") "; $rsInsert = $DB->Query($strSql, true); } } } return $arResult; }