Source

SolrQueryBuilder / README.md

Full commit

Solr Query Builder

A simple PHP 5.3 library that will help to make Solr Queries with an object oriented, dynamic approach. Primarily wrote for using with solr-php-client, but can be used with any solr client that works with simple query-string. Can be integrated with any PHP 5.3 Framework easily.

Features

  • Simple queries
  • Automatic escaping
  • Grouping terms
  • Term modifiers
    • Fuzzy Search
    • Required/Restricted
    • Boost factors
    • Proximity Search
  • Range Queries (Date/Numeric)
  • (Any number of) Subqueries

Usages Example

Two simple classes made everything here. Field and QueryString. __toString() of both classes just return the raw string query that you can use for searching solr. Subqueries are actually another full featured QueryString object. So, Any QueryString object can be used as subquery to another QueryString.

Here is a small snippet of example -

<?php
use Solr\QueryBuilder\Querystring;
use Solr\QueryBuilder\Field;

echo "--------- Solr Query Test ----------- \n";

$Query = new Querystring();
$Query->addField(new Field('name', 'Anis uddin Ahmad'));
$Query->addField(new Field('phone', '+880 173 0053053'), 'PHONE-an-optional-key-for-removing');

echo 'Simple query: '. $Query ;
// OUTPUT-> Simple query: name:"Anis uddin Ahmad" AND phone:"+880 173 0053053"

// Removing fields
$Query->removeField('PHONE-an-optional-key-for-removing');

echo "--------- Solr SubQuery Test ----------- \n";

$subQuery = new  Querystring();
$subQuery->addField(new Field(null, 'value for default field'));
$subQuery->addField(new Field('item_type', 'anything'));

$subQuery->setFieldSeparator('OR');

echo 'Subquery Output: '. $subQuery;
// OUTPUT-> Subquery Output: "value for default field" OR item_type:anything

// Add $subQuery in $Query
$Query->addSubQuery($subQuery, 'optional-key');

echo 'After adding sub-query: '. $Query;
// OUTPUT-> After adding sub-query: name:"Anis uddin Ahmad" AND ("value for default field" OR item_type:anything)
?>

For more examples, check examples/index.php or test files.