Skip to content

Instantly share code, notes, and snippets.

@elinaldosoft
Created October 23, 2013 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elinaldosoft/7123830 to your computer and use it in GitHub Desktop.
Save elinaldosoft/7123830 to your computer and use it in GitHub Desktop.
public function buildQuery($conditions = array()) {
$conditions_solr = array();
if (isset($conditions["count"]) && !empty($conditions["count"])) {
return "fl=id";
}
if (isset($conditions["fields"]) && !empty($conditions["fields"])) {
$conditions_solr["fl"] = "fl=" . implode("%2C+", $conditions["fields"]);
}
if (isset($conditions["order"]) && !empty($conditions["order"])) {
foreach ($conditions["order"] as $key => $order) {
$conditions["order"][$key] = preg_replace('#(\s+|%20)#is', "+", strtolower($conditions["order"][$key]));
}
$conditions_solr["sort"] = "sort=" . implode("%2C+", $conditions["order"]);
}
if (isset($conditions["limit"]) && !empty($conditions["limit"])) {
$this->pagination["limit"] = $conditions["limit"];
}
if (isset($conditions["conditions"]) && !empty($conditions["conditions"])) {
$fq = array();
foreach ($conditions["conditions"] as $k_c => $cond) {
if (!empty($cond)) {
$fq += array("{$k_c}" => implode("+OR+", $cond));
}
$i = 0;
$q_fq = null;
foreach ($fq as $k_fq => $v_fq) {
if (!empty($v_fq)) {
if ($i == 0) {
$q_fq .= "{$k_fq}%3A({$v_fq})";
} else {
$q_fq .= "+{$k_fq}%3A({$v_fq})";
}
}
$i++;
}
$i = 0;
if (!empty($q_fq)) {
$conditions_solr["fq"] = "fq=" . $q_fq;
}
}
}
if (isset($conditions["facete"]) && !empty($conditions["facete"])) {
foreach ($conditions["facete"] as $key => $order) {
$conditions["facete"][$key] = preg_replace('#(\s+|%20)#is', "+", strtolower($conditions["facete"][$key]));
}
$conditions_solr["facete"] = "facet.field=" . implode("%2C", $conditions["facete"]);
}
$conditions_solr["pagination"] = self::paginator();
return implode("&", $conditions_solr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment