Commits

ajaxray committed fd8626d

Added basic files.

  • Participants

Comments (0)

Files changed (4)

+.idea

File classes/param.php

+<?php
+namespace Solr\QueryBuilder;
+
+
+class Param {
+
+    protected $_field;
+    protected $_terms = array();
+
+    private $_operators = array('+', '-');
+    private $_modifiers = array('~', '^');
+
+    const RANGE_QUERY = '#range#';
+
+    function __construct($value = null, $field = null)
+    {
+        if(! empty($value)) {
+            $this->addTerm($value);
+        }
+
+        if(! empty($field)){
+            $this->_field = strval($field);
+        }
+
+        return $this;
+    }
+
+    public function addTerm($value, array $metadata = null)
+    {
+        if($metadata) {
+            $metadata['value'] = strval($value);
+            $this->_terms[] = $metadata;
+        } else {
+            $this->_terms[] = strval($value);
+        }
+
+        return $this;
+    }
+
+    public function addRange($from = null, $to = null)
+    {
+        $rangeTerm = array(
+            'from' => (is_null($from))? '*' : strval($from),
+            'to'   => (is_null($to))? '*' : strval($to),
+        );
+
+        $this->addTerm(self::RANGE_QUERY, $rangeTerm);
+
+        return $this;
+    }
+
+    public function __toString()
+    {
+        $output = $this->_processTerms();
+
+        return (empty($this->_field))? $output : $this->_field . ':' . $output;
+    }
+
+    protected function _processTerms()
+    {
+        $termsOutput = (count($this->_terms) > 1)? '(' : '';
+
+        $termSegments = array();
+        foreach($this->_terms as $term){
+
+            if(is_array($term)){
+
+                if($term['value'] == self::RANGE_QUERY){
+                    $termSegments[] = "[{$term['from']} TO {$term['to']}]";
+                    continue;
+                }
+
+                $segmentOutput = self::_escapeToken($term['value']);
+
+                // Add operator, required or restricted
+                if(isset($term['operator']) && in_array($term['operator'], $this->_operators)) {
+                    $segmentOutput = $term['operator'] . $segmentOutput;
+                }
+                
+                // Add fuzzy, proximity and boost factor
+                if(isset($term['modifier']) && in_array($term['modifier'], $this->_modifiers)) {
+                    $segmentOutput .=  $term['modifier'];
+                    if(isset($term['factor']) && is_numeric($term['factor'])){
+                        $segmentOutput .= $term['factor'];
+                    }
+                }
+
+                $termSegments[] = $segmentOutput;
+            } else {
+                $termSegments[] = self::_escapeToken($term);
+            }
+        }
+        
+        $termsOutput .= implode(' ', $termSegments);
+        $termsOutput .= (count($this->_terms) > 1)? ')' : '';
+
+        return $termsOutput;
+    }
+
+  /**
+   * Add '"' chars if necessary to a token value.
+   * @return unknown_type
+   */
+  protected static function _escapeToken($token) {
+    if (preg_match("/[\., -:]/", $token)) {
+      return '"' . $token . '"';
+    }
+    else {
+      return $token;
+    }
+  }
+}

File classes/querystring.php

+<?php
+namespace Solr\QueryBuilder;
+
+
+class Querystring {
+
+    const INTERNAL_SEPARATOR = 'in';
+    const EXTERNAL_SEPARATOR = 'ex';
+
+  /**
+   * Queries storage.
+   *
+   * @var array
+   */
+   protected $_queries = array();
+
+   /**
+   * Sub-queries storage.
+   *
+   * @var array
+   */
+  protected $_subQueries = array();
+
+  /**
+   * Default internal operator. Can be 'AND' or 'OR'/' '.
+   *
+   * @var string
+   */
+  protected $_inOperator = ' ';
+
+  /**
+   * Default external operator. will be used for subqueries Can be 'AND' or 'OR'.
+   *
+   * @var string
+   */
+  protected $_exOperator = 'AND';
+
+  /**
+   * Set default operator.
+   *
+   * @param string $operator  Can be 'AND' or 'OR'.
+   * @param string $type      Can be INTERNAL_SEPARATOR or EXTERNAL_SEPARATOR.
+   */
+  public function setOperator($operator, $type = self::INTERNAL_SEPARATOR) {
+    if ($operator == 'AND' || $operator == 'OR') {
+        if($type == self::INTERNAL_SEPARATOR){
+            $this->_inOperator = $operator;
+        } else if($type == self::EXTERNAL_SEPARATOR) {
+            $this->_exOperator = $operator;
+        }
+    }
+  }
+
+  /**
+   * Add SubQuery to current query
+   *
+   * @param Querystring $query
+   */
+  public function addSubQuery(Querystring $query, $key = null) {
+      if($key){
+          $this->_subQueries[$key] = $query;
+      } else {
+          $this->_subQueries[] = $query;
+      }
+
+  }
+
+  public function removeSubQuery($key) {
+      unset($this->_subQueries[$key]);
+      return $this;
+  }
+
+  public function addQuery(Param $param, $key = null) {
+      if($key){
+          $this->_queries[$key] = $param;
+      } else {
+          $this->_queries[] = $param;
+      }
+
+      return $this;
+  }
+
+  public function removeQuery($key) {
+      unset($this->_queries[$key]);
+      return $this;
+  }
+
+
+  protected function _renderRawOutput() {
+    if (! empty($this->_queries)) {
+      return implode(' '. $this->_inOperator .' ', $this->_queries);
+    }
+
+    return "";
+  }
+
+  /**
+   * Build RAW query string
+   *
+   * @return string
+   */
+  public final function __toString() {
+    $output = trim($this->_renderRawOutput());
+    if (! empty($this->_subQueries)) {
+      $subOutput = implode(' ' . $this->_exOperator . ' ', $this->_subQueries);
+      if (! empty($output)) {
+        $output .= ' ' . $this->_exOperator . ' (' . $subOutput . ')';
+      }
+      else {
+        $output .= $subOutput;
+      }
+    }
+    return $output;
+  }
+}
+

File examples/index.php

+<?php
+include('querystring.php');
+include('param.php');
+
+echo "--------- Solr Query Test ----------- <br />";
+
+$Query = new Solr\QueryBuilder\Querystring();
+$Query->addQuery(new Solr\QueryBuilder\Param('Emran-Ul-Hadi', 'name'));
+echo '<b>simple query:</b> '. $Query ."<br />";
+
+$Query->addQuery(new Solr\QueryBuilder\Param('0179483r50', 'phone'), 'phone');
+$Query->addQuery(new Solr\QueryBuilder\Param('yes', 'active'));
+echo '<b>miltiple fields query:</b> '. $Query ."<br />";
+
+$Query->removeQuery('phone');
+$Query->setOperator('AND', $Query::INTERNAL_SEPARATOR);
+echo '<b>after remove phone and internal separator AND:</b> '. $Query ."<br />";
+
+echo "<br /><br />--------- Sub-Solr Query Test ----------- <br />";
+$subQuery = new Solr\QueryBuilder\Querystring();
+$subQuery->addQuery(new Solr\QueryBuilder\Param('for-default-field'));
+$subQuery->addQuery(new Solr\QueryBuilder\Param('diapers', 'item_type'));
+
+echo '<b>sub query:</b> '. $subQuery ."<br />";
+
+$subQuery->setOperator('AND');
+$Query->addSubQuery($subQuery);
+
+echo '<b>after adding sub-query:</b> '. $Query ."<br />";
+
+echo "<br /><br />--------- Term grouping test ----------- <br />";
+$langParam = new Solr\QueryBuilder\Param('en', 'lang');
+$langParam->addTerm('bn-BD')->addTerm('fr');
+
+$Query->addQuery($langParam, 'lang');
+echo '<b>Added grouped field:</b> '. $Query ."<br />";
+
+echo "<br /><br />--------- Term modifiers test ----------- <br />";
+$category = new Solr\QueryBuilder\Param(null, 'category');
+$category->addTerm('diapers', array('modifier' => '~'));
+$category->addTerm('baby-oil', array('modifier' => '~', 'factor'=> 0.7));
+$Query->addQuery($category, 'category');
+echo '<b>Fuzzy Search:</b> '. $Query ."<br />";
+
+$category->addTerm('electronics', array('operator' => '-'));
+$category->addTerm('boy-cloths', array('operator' => '+', 'modifier' => '~', 'factor'=> 0.5));
+echo '<b>Required/Restricted:</b> '. $Query ."<br />";
+
+$category->addTerm('baby', array('modifier' => '^', 'factor'=> 3));
+$category->addTerm('food', array('modifier' => '^', 'factor'=> 5));
+echo '<b>Boost:</b> '. $Query ."<br />";
+
+echo "<br /><br />--------- Range Query test ----------- <br />";
+$Query->removeQuery('category');
+
+$date = new Solr\QueryBuilder\Param(null, 'date');
+$date->addRange(null, 'NOW');
+
+$age = new Solr\QueryBuilder\Param(null, 'age');
+$age->addRange(18, 60);
+
+$Query->addQuery($date, 'date');
+$Query->addQuery($age, 'age');
+echo '<b>Range:</b> '. $Query ."<br />";