• Модуль: search
  • Путь к файлу: ~/bitrix/modules/search/classes/mysql/search.php
  • Класс: CSearchQuery
  • Вызов: CSearchQuery::BuildWhereClause
function BuildWhereClause($word)
{
	$DB = CDatabase::GetModuleConnection('search');

	$this->cnt++;
	if ($this->cnt > 10)
		return "1=1";

	if (isset($this->m_kav[$word]))
	{
		$word = $this->m_kav[$word];
		$bInQuotes = true;
	}
	else
	{
		$bInQuotes = false;
	}
	$this->m_words[] = $word;
	$word = $DB->ForSql($word, 100);

	if ($this->bTagsSearch)
	{
		if (mb_strpos($word, "%") === false)
		{
			//We can optimize query by doing range scan
			if (is_array($this->m_tags_words))
				$this->m_tags_words[] = $word;
			$op = "=";
		}
		else
		{
			//Optimization is not possible
			$this->m_tags_words = false;
			$op = "like";
		}
		return "(sum(stags.name ".$op." '".$word."')>0)";
	}
	elseif ($this->bStemming && !$bInQuotes)
	{
		$word = ToUpper($word);
		$this->m_stemmed_words[] = $word;

		if (BX_SEARCH_VERSION > 1)
		{
			$rs = $DB->Query("select ID from b_search_stem where STEM='".$DB->ForSQL($word)."'");
			$ar = $rs->Fetch();
			$this->m_stemmed_words_id[] = intval($ar["ID"]);

			return "(sum(st.stem = ".intval($ar["ID"]).")>0)";
		}
		else
		{
			return "(sum(st.stem = '".$word."')>0)";
		}
	}
	else
	{
		if (BX_SEARCH_VERSION > 1)
		{
			$this->bText = true;
			return "(sct.searchable_content LIKE '%".str_replace(array("%", "_"), array("\%", "\_"), ToUpper($word))."%')";
		}
		else
		{
			return "(sc.searchable_content LIKE '%".str_replace(array("%", "_"), array("\%", "\_"), ToUpper($word))."%')";
		}
	}
}