<commit_before><?
require_once (mnminclude.'sphinxapi.php');

function do_search($by_date = false, $start = 0, $count = 50, $proximity = true) {
	search_parse_query();
	if ($_REQUEST['w'] == 'links' && ($_REQUEST['p'] == 'site' || $_REQUEST['p'] == 'url_db')) {
		return db_get_search_links($by_date, $start, $count);
	} else {
		return sphinx_do_search($by_date, $start, $count);
	}

}

function sphinx_client() {
	global $globals, $db;

	static $cl = false;
	if (!$cl) {
		$cl = new SphinxClient ();
		$cl->SetServer ($globals['sphinx_server'], $globals['sphinx_port']);

		// Request for status values, it's used in other sites
		$globals['status_values'] = $db->get_enum_values('links', 'link_status');
	}
	return $cl;
}

function sphinx_do_search($by_date = false, $start = 0, $count = 10, $proximity = true) {
	global $globals;

	$response = array();
	$queries = array();
	$recorded = array();


	$start_time = microtime(true);

	$indices = $_REQUEST['w'].' '.$_REQUEST['w'].'_delta';

	$cl = sphinx_client();
	if (!$cl) return $response;

	$cl->SetLimits ( $start, $count );
	if ($_REQUEST['w'] == 'links') {
		$cl->SetFieldWeights(array('title' => 3, 'tags' => 2, 'url' => 2, 'content' => 1));
	} else {
		$cl->SetFieldWeights(array('content' => 1));
	}

	$response['rows'] = 0;
	$response['time'] = 0;

	if (empty($_REQUEST['words'])) return $response;


	$words_array = preg_split('/\s+/', $_REQUEST['words'], -1, PREG_SPLIT_NO_EMPTY);
	$words_count = count($words_array);
	$words = $_REQUEST['words'];


	if ($_REQUEST['t']) {
		$max_date = time();
		$min_date = intval($_REQUEST['t']);
		$cl->SetFilterRange('date', $min_date, $max_date);
	}

	if ($_REQUEST['h']) {
		$max_date = time();
		$min_date = $max_date - intval($_REQUEST['h']) * 3600;
		$cl->SetFilterRange('date', $min_date, $max_date);
	}

	if ($_REQUEST['w'] == 'links' && $_REQUEST['s']) {
		if (preg_match('/^ *! */', $_REQUEST['s'])) {
			// Allows to reject a status
			$_REQUEST['s'] = preg_replace('/^ *! */', '', $_REQUEST['s']);
			$s_reject = true;
		} else {
			$s_reject = false;
		}

		// Allow multiple statuses
		$statuses = preg_split('/\s+/', $_REQUEST['s'], -1, PREG_SPLIT_NO_EMPTY);

		$s_id = array();
		foreach ($statuses as $s) {
			if (isset($globals['status_values'][$s])) {
				array_push($s_id, $globals['status_values'][$s]);
			}
		}
		if (count($s_id) > 0) {
			$cl->SetFilter('status', $s_id, $s_reject);
		}
	}

	if ($_REQUEST['u']) {
		$u = new User();
		$u->username = $_REQUEST['u'];
		$u->read();
		$cl->SetFilterRange('user', $u->id, $u->id);
	}

	if ($_REQUEST['w'] == 'links' && $_REQUEST['p']) {
		$f = '@'.$_REQUEST['p'];
	} else {
		$f = '@*';
	}

	if ($by_date || $_REQUEST['o'] == 'date') {
		$cl->SetSortMode (SPH_SORT_ATTR_DESC, 'date');
	} elseif ($_REQUEST['o'] == 'pure') {
		$cl->SetSortMode (SPH_SORT_RELEVANCE);
	} else {
		//$cl->SetSortMode(SPH_SORT_EXPR, "@weight - (NOW() - date)/20000");

		// expressions to decrease weights logarimically
		if ($_REQUEST['w'] == 'links') {
			$p = $globals['status_values']['published'];
			$q = $globals['status_values']['queued'];

			$now = time();
			$b = log(0.9)/720;
			$fp = "@weight * max(0.5, exp($b*abs($now-date)/3600))";
			$b = log(0.6)/720;
			$fq = "@weight * max(0.4, exp($b*abs($now-date)/3600))";
			$b = log(0.2)/720;
			$fo = "@weight * max(0.1, exp($b*abs($now-date)/3600))";
			$exp = "if (status-$p = 0, $fp , if (status-$q = 0, $fq, $fo))";

			$cl->SetSortMode(SPH_SORT_EXPR, $exp);
		
		} else {
			$cl->SetSortMode(SPH_SORT_EXPR, "@weight - (@weight * log10(ceil((NOW()-date)/10000)) / 20)");
		}
	}
<commit_msg>Adopted the same exponential decay for comments and posts<commit_after><?
require_once (mnminclude.'sphinxapi.php');

function do_search($by_date = false, $start = 0, $count = 50, $proximity = true) {
	search_parse_query();
	if ($_REQUEST['w'] == 'links' && ($_REQUEST['p'] == 'site' || $_REQUEST['p'] == 'url_db')) {
		return db_get_search_links($by_date, $start, $count);
	} else {
		return sphinx_do_search($by_date, $start, $count);
	}

}

function sphinx_client() {
	global $globals, $db;

	static $cl = false;
	if (!$cl) {
		$cl = new SphinxClient ();
		$cl->SetServer ($globals['sphinx_server'], $globals['sphinx_port']);

		// Request for status values, it's used in other sites
		$globals['status_values'] = $db->get_enum_values('links', 'link_status');
	}
	return $cl;
}

function sphinx_do_search($by_date = false, $start = 0, $count = 10, $proximity = true) {
	global $globals;

	$response = array();
	$queries = array();
	$recorded = array();


	$start_time = microtime(true);

	$indices = $_REQUEST['w'].' '.$_REQUEST['w'].'_delta';

	$cl = sphinx_client();
	if (!$cl) return $response;

	$cl->SetLimits ( $start, $count );
	if ($_REQUEST['w'] == 'links') {
		$cl->SetFieldWeights(array('title' => 3, 'tags' => 2, 'url' => 2, 'content' => 1));
	} else {
		$cl->SetFieldWeights(array('content' => 1));
	}

	$response['rows'] = 0;
	$response['time'] = 0;

	if (empty($_REQUEST['words'])) return $response;


	$words_array = preg_split('/\s+/', $_REQUEST['words'], -1, PREG_SPLIT_NO_EMPTY);
	$words_count = count($words_array);
	$words = $_REQUEST['words'];


	if ($_REQUEST['t']) {
		$max_date = time();
		$min_date = intval($_REQUEST['t']);
		$cl->SetFilterRange('date', $min_date, $max_date);
	}

	if ($_REQUEST['h']) {
		$max_date = time();
		$min_date = $max_date - intval($_REQUEST['h']) * 3600;
		$cl->SetFilterRange('date', $min_date, $max_date);
	}

	if ($_REQUEST['w'] == 'links' && $_REQUEST['s']) {
		if (preg_match('/^ *! */', $_REQUEST['s'])) {
			// Allows to reject a status
			$_REQUEST['s'] = preg_replace('/^ *! */', '', $_REQUEST['s']);
			$s_reject = true;
		} else {
			$s_reject = false;
		}

		// Allow multiple statuses
		$statuses = preg_split('/\s+/', $_REQUEST['s'], -1, PREG_SPLIT_NO_EMPTY);

		$s_id = array();
		foreach ($statuses as $s) {
			if (isset($globals['status_values'][$s])) {
				array_push($s_id, $globals['status_values'][$s]);
			}
		}
		if (count($s_id) > 0) {
			$cl->SetFilter('status', $s_id, $s_reject);
		}
	}

	if ($_REQUEST['u']) {
		$u = new User();
		$u->username = $_REQUEST['u'];
		$u->read();
		$cl->SetFilterRange('user', $u->id, $u->id);
	}

	if ($_REQUEST['w'] == 'links' && $_REQUEST['p']) {
		$f = '@'.$_REQUEST['p'];
	} else {
		$f = '@*';
	}

	if ($by_date || $_REQUEST['o'] == 'date') {
		$cl->SetSortMode (SPH_SORT_ATTR_DESC, 'date');
	} elseif ($_REQUEST['o'] == 'pure') {
		$cl->SetSortMode (SPH_SORT_RELEVANCE);
	} else {
		// expressions to decrease weights logarimically
		$now = time();
		if ($_REQUEST['w'] == 'links') {
			$p = $globals['status_values']['published'];
			$q = $globals['status_values']['queued'];

			$b = log(0.9)/720;
			$fp = "@weight * max(0.5, exp($b*abs($now-date)/3600))";
			$b = log(0.6)/720;
			$fq = "@weight * max(0.4, exp($b*abs($now-date)/3600))";
			$b = log(0.2)/720;
			$fo = "@weight * max(0.1, exp($b*abs($now-date)/3600))";
			$exp = "if (status-$p = 0, $fp , if (status-$q = 0, $fq, $fo))";
		} else {
			$b = log(0.95)/720;
			$exp = "@weight * max(0.5, exp($b*abs($now-date)/3600))";
		}
		$cl->SetSortMode(SPH_SORT_EXPR, $exp);
	}

{'fraction_non_alphanumeric': 0.24937168388718234, 'fraction_numerical': 0.01898910918737783, 'mean_word_length': 4.236111111111111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3502832', 'n_tokens_mistral': 3184, 'n_tokens_neox': 2886, 'n_words': 547}