Exemple #1
0
	function Search($phrase = "", $nTopCount = 5, $arParams = array(), $bNotFilter = false, $order = "")
	{
		$DB = CDatabase::GetModuleConnection('search');
		$this->_arPhrase = stemming_split($phrase, LANGUAGE_ID);
		if(!empty($this->_arPhrase))
		{
			$nTopCount = intval($nTopCount);
			if($nTopCount <= 0)
				$nTopCount = 5;

			$arId = CSearchFullText::GetInstance()->searchTitle($phrase, $this->_arPhrase, $nTopCount, $arParams, $bNotFilter, $order);
			if (!is_array($arId))
			{
				return $this->searchTitle($phrase, $nTopCount, $arParams, $bNotFilter, $order);
			}
			elseif (!empty($arId))
			{
				$strSql = "
					SELECT
						sc.ID
						,sc.MODULE_ID
						,sc.ITEM_ID
						,sc.TITLE
						,sc.PARAM1
						,sc.PARAM2
						,sc.DATE_CHANGE
						,L.DIR
						,L.SERVER_NAME
						,sc.URL as URL
						,scsite.URL as SITE_URL
						,scsite.SITE_ID
						,if(locate('".$DB->ForSQL(ToUpper($phrase))."', upper(sc.TITLE)) > 0, 1, 0) RANK1
					FROM
						b_search_content sc
						INNER JOIN b_search_content_site scsite ON sc.ID = scsite.SEARCH_CONTENT_ID
						INNER JOIN b_lang L ON scsite.SITE_ID = L.LID
					WHERE
						sc.ID in (".implode(",", $arId).")
						and scsite.SITE_ID = '".SITE_ID."'
					ORDER BY ".(
						$bOrderByRank?
							"RANK1 DESC, TITLE":
							"DATE_CHANGE DESC, RANK1 DESC, TITLE"
					)."
				";

				$r = $DB->Query($DB->TopSql($strSql, $nTopCount+1));
				parent::CDBResult($r);
				return true;
			}
		}
		else
		{
			return false;
		}
	}
Exemple #2
0
 function Search($phrase = "", $nTopCount = 5, $arParams = array(), $bNotFilter = false, $order = "")
 {
     $DB = CDatabase::GetModuleConnection('search');
     $this->_arPhrase = stemming_split($phrase, LANGUAGE_ID);
     if (!empty($this->_arPhrase)) {
         $nTopCount = intval($nTopCount);
         if ($nTopCount <= 0) {
             $nTopCount = 5;
         }
         $arId = CSearchFullText::GetInstance()->searchTitle($phrase, $this->_arPhrase, $nTopCount, $arParams, $bNotFilter, $order);
         if (!is_array($arId)) {
             return $this->searchTitle($phrase, $nTopCount, $arParams, $bNotFilter, $order);
         } elseif (!empty($arId)) {
             $strSql = "\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tsc.ID\n\t\t\t\t\t\t,sc.MODULE_ID\n\t\t\t\t\t\t,sc.ITEM_ID\n\t\t\t\t\t\t,sc.TITLE\n\t\t\t\t\t\t,sc.PARAM1\n\t\t\t\t\t\t,sc.PARAM2\n\t\t\t\t\t\t,sc.DATE_CHANGE\n\t\t\t\t\t\t,L.DIR\n\t\t\t\t\t\t,L.SERVER_NAME\n\t\t\t\t\t\t,sc.URL as URL\n\t\t\t\t\t\t,scsite.URL as SITE_URL\n\t\t\t\t\t\t,scsite.SITE_ID\n\t\t\t\t\t\t," . $this->getRankFunction($phrase) . " RANK1\n\t\t\t\t\tFROM\n\t\t\t\t\t\tb_search_content sc\n\t\t\t\t\t\tINNER JOIN b_search_content_site scsite ON sc.ID = scsite.SEARCH_CONTENT_ID\n\t\t\t\t\t\tINNER JOIN b_lang L ON scsite.SITE_ID = L.LID\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tsc.ID in (" . implode(",", $arId) . ")\n\t\t\t\t\t\tand scsite.SITE_ID = '" . SITE_ID . "'\n\t\t\t\t\tORDER BY " . $this->getSqlOrder($bOrderByRank) . "\n\t\t\t\t";
             $r = $DB->Query($DB->TopSql($strSql, $nTopCount + 1));
             parent::CDBResult($r);
             return true;
         }
     } else {
         return false;
     }
 }
Exemple #3
0
	function Search($arParams, $aSort=array(), $aParamsEx=array(), $bTagsCloud = false)
	{
		$DB = CDatabase::GetModuleConnection('search');

		if(!is_array($arParams))
			$arParams = array("QUERY"=>$arParams);

		if(!is_set($arParams, "SITE_ID") && is_set($arParams, "LID"))
		{
			$arParams["SITE_ID"] = $arParams["LID"];
			unset($arParams["LID"]);
		}

		if(array_key_exists("TAGS", $arParams))
		{
			$this->strTagsText = $arParams["TAGS"];
			$arTags = explode(",", $arParams["TAGS"]);
			foreach($arTags as $i => $strTag)
			{
				$strTag = trim($strTag);
				if(strlen($strTag))
					$arTags[$i] = str_replace("\"", "\\\"", $strTag);
				else
					unset($arTags[$i]);
			}

			if(count($arTags))
				$arParams["TAGS"] = '"'.implode('","', $arTags).'"';
			else
				unset($arParams["TAGS"]);
		}

		$this->strQueryText = $strQuery = trim($arParams["QUERY"]);
		$this->strTags = $strTags  = $arParams["TAGS"];

		if((strlen($strQuery) <= 0) && (strlen($strTags) > 0))
		{
			$strQuery = $strTags;
			$bTagsSearch = true;
		}
		else
		{
			if(strlen($strTags))
				$strQuery .= " ".$strTags;
			$strQuery = preg_replace_callback("/&#(\\d+);/", array($this, "chr"), $strQuery);
			$bTagsSearch = false;
		}

		$result = CSearchFullText::GetInstance()->search($arParams, $aSort, $aParamsEx, $bTagsCloud);
		if (is_array($result))
		{
			$this->error = CSearchFullText::GetInstance()->getErrorText();
			$this->errorno = CSearchFullText::GetInstance()->getErrorNumber();
			$this->formatter = CSearchFullText::GetInstance()->getRowFormatter();
			if ($this->errorno > 0)
				return;
		}
		else
		{
			if(!array_key_exists("STEMMING", $aParamsEx))
				$aParamsEx["STEMMING"] = COption::GetOptionString("search", "use_stemming", "N")=="Y";

			$this->Query = new CSearchQuery("and", "yes", 0, $arParams["SITE_ID"]);
			if($this->_opt_NO_WORD_LOGIC)
				$this->Query->no_bool_lang = true;

			$query = $this->Query->GetQueryString((BX_SEARCH_VERSION > 1? "sct": "sc").".SEARCHABLE_CONTENT", $strQuery, $bTagsSearch, $aParamsEx["STEMMING"], $this->_opt_ERROR_ON_EMPTY_STEM);
			if(!$query || strlen(trim($query))<=0)
			{
				if($bTagsCloud)
				{
					$query = "1=1";
				}
				else
				{
					$this->error = $this->Query->error;
					$this->errorno = $this->Query->errorno;
					return;
				}
			}

			if(strlen($query)>2000)
			{
				$this->error = GetMessage("SEARCH_ERROR4");
				$this->errorno = 4;
				return;
			}
		}

		foreach(GetModuleEvents("search", "OnSearch", true) as $arEvent)
		{
			$r = "";
			if($bTagsSearch)
			{
				if(strlen($strTags))
					$r = ExecuteModuleEventEx($arEvent, array("tags:".$strTags));
			}
			else
			{
				$r = ExecuteModuleEventEx($arEvent, array($strQuery));
			}
			if($r <> "")
				$this->url_add_params[] = $r;
		}

		if (is_array($result))
		{
			$r = new CDBResult;
			$r->InitFromArray($result);
		}
		elseif(
			BX_SEARCH_VERSION > 1
			&& count($this->Query->m_stemmed_words_id)
			&& array_sum($this->Query->m_stemmed_words_id) === 0
		)
		{
			$r = new CDBResult;
			$r->InitFromArray(array());
		}
		else
		{
			$this->strSqlWhere = "";
			$bIncSites = false;

			$arSqlWhere = array();
			if(is_array($aParamsEx) && !empty($aParamsEx))
			{
				foreach($aParamsEx as $aParamEx)
				{
					$strSqlWhere = CSearch::__PrepareFilter($aParamEx, $bIncSites);
					if($strSqlWhere != "")
						$arSqlWhere[] = $strSqlWhere;
				}
			}
			if (!empty($arSqlWhere))
			{
				$arSqlWhere = array(
					"\n\t\t\t\t(".implode(")\n\t\t\t\t\tOR(",$arSqlWhere)."\n\t\t\t\t)",
				);
			}

			$strSqlWhere = CSearch::__PrepareFilter($arParams, $bIncSites);
			if($strSqlWhere != "")
				array_unshift($arSqlWhere, $strSqlWhere);

			$strSqlOrder = $this->__PrepareSort($aSort, "sc.", $bTagsCloud);

			if(!array_key_exists("USE_TF_FILTER", $aParamsEx))
				$aParamsEx["USE_TF_FILTER"] = COption::GetOptionString("search", "use_tf_cache") == "Y";

			$bStem = !$bTagsSearch && count($this->Query->m_stemmed_words)>0;
			//calculate freq of the word on the whole site_id
			if($bStem && count($this->Query->m_stemmed_words))
			{
				$arStat = $this->GetFreqStatistics($this->Query->m_lang, $this->Query->m_stemmed_words, $arParams["SITE_ID"]);
				$this->tf_hwm_site_id = (strlen($arParams["SITE_ID"]) > 0? $arParams["SITE_ID"]: "");

				//we'll make filter by it's contrast
				if(!$bTagsCloud && $aParamsEx["USE_TF_FILTER"])
				{
					$hwm = false;
					foreach($this->Query->m_stemmed_words as $i => $stem)
					{
						if(!array_key_exists($stem, $arStat))
						{
							$hwm = 0;
							break;
						}
						elseif($hwm === false)
						{
							$hwm = $arStat[$stem]["TF"];
						}
						elseif($hwm > $arStat[$stem]["TF"])
						{
							$hwm = $arStat[$stem]["TF"];
						}
					}

					if($hwm > 0)
					{
						$arSqlWhere[] = "st.TF >= ".number_format($hwm, 2, ".", "");
						$this->tf_hwm = $hwm;
					}
				}
			}

			if(!empty($arSqlWhere))
			{
				$this->strSqlWhere = "\n\t\t\t\tAND (\n\t\t\t\t\t(".implode(")\n\t\t\t\t\tAND(",$arSqlWhere).")\n\t\t\t\t)";
			}

			if($bTagsCloud)
				$strSql = $this->tagsMakeSQL($query, $this->strSqlWhere, $strSqlOrder, $bIncSites, $bStem, $aParamsEx["LIMIT"]);
			else
				$strSql = $this->MakeSQL($query, $this->strSqlWhere, $strSqlOrder, $bIncSites, $bStem);

			$r = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		}
		parent::CDBResult($r);
	}